Skip to content

Commit d495102

Browse files
committed
feat(exists): add exists method
1 parent c245771 commit d495102

File tree

7 files changed

+63
-0
lines changed

7 files changed

+63
-0
lines changed

docs/en/api/wrapper/exists.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# exists()
2+
3+
- **Returns:** `{boolean}`
4+
5+
- **Usage:**
6+
7+
Assert `Wrapper` or `WrapperArray` exists.
8+
9+
Returns false if called on an empty `Wrapper` or `WrapperArray`.
10+
11+
```js
12+
import { mount } from 'vue-test-utils'
13+
import { expect } from 'chai'
14+
import Foo from './Foo.vue'
15+
16+
const wrapper = mount(Foo)
17+
expect(wrapper.exists()).to.equal(true)
18+
expect(wrapper.find('does-not-exist').exists()).to.equal(false)
19+
expect(wrapper.findAll('div').exists()).to.equal(true)
20+
expect(wrapper.findAll('does-not-exist').exists()).to.equal(false)
21+
```

flow/wrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type WrapperArray from '~src/WrapperArray'
66
declare interface BaseWrapper { // eslint-disable-line no-undef
77
at(index: number): Wrapper | void,
88
contains(selector: String | Component): boolean | void,
9+
exists(): boolean,
910
hasAttribute(attribute: string, value: string): boolean | void,
1011
hasClass(className: string): boolean | void,
1112
hasProp(prop: string, value: string): boolean | void,

src/wrappers/error-wrapper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export default class ErrorWrapper implements BaseWrapper {
1616
throwError(`find did not return ${this.selector}, cannot call contains() on empty Wrapper`)
1717
}
1818

19+
exists (): boolean {
20+
return false
21+
}
22+
1923
hasAttribute (): void {
2024
throwError(`find did not return ${this.selector}, cannot call hasAttribute() on empty Wrapper`)
2125
}

src/wrappers/wrapper-array.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export default class WrapperArray implements BaseWrapper {
2626
return this.wrappers.every(wrapper => wrapper.contains(selector))
2727
}
2828

29+
exists (): boolean {
30+
return this.wrappers.length > 0
31+
}
32+
2933
hasAttribute (attribute: string, value: string): boolean {
3034
this.throwErrorIfWrappersIsEmpty('hasAttribute')
3135

src/wrappers/wrapper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ export default class Wrapper implements BaseWrapper {
4747
return false
4848
}
4949

50+
/**
51+
* Utility to check wrapper exists. Returns true as Wrapper always exists
52+
*/
53+
exists (): boolean {
54+
return true
55+
}
56+
5057
/**
5158
* Checks if wrapper has an attribute with matching value
5259
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { compileToFunctions } from 'vue-template-compiler'
2+
import mount from '~src/mount'
3+
4+
describe('exists', () => {
5+
it('returns true if called on Wrapper', () => {
6+
const compiled = compileToFunctions('<div />')
7+
const wrapper = mount(compiled)
8+
expect(wrapper.exists()).to.equal(true)
9+
})
10+
11+
it('returns false if called on an ErrorWrapper', () => {
12+
const compiled = compileToFunctions('<div />')
13+
const wrapper = mount(compiled)
14+
expect(wrapper.find('does-not-exist').exists()).to.equal(false)
15+
})
16+
})

test/unit/specs/wrappers/wrapper-array.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ describe('WrapperArray', () => {
4848
expect(wrapperArray.contains()).to.equal(false)
4949
})
5050

51+
it('exists returns true if length is greater then 0', () => {
52+
const wrapperArray = new WrapperArray([{}])
53+
expect(wrapperArray.exists()).to.equal(true)
54+
})
55+
56+
it('exists returns false if length is 0', () => {
57+
const wrapperArray = new WrapperArray([])
58+
expect(wrapperArray.exists()).to.equal(false)
59+
})
60+
5161
it('hasAttribute returns true if every wrapper.hasAttribute() returns true', () => {
5262
const attribute = 'attribute'
5363
const value = 'value'

0 commit comments

Comments
 (0)