forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontains.spec.js
147 lines (132 loc) · 5.08 KB
/
contains.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { compileToFunctions } from 'vue-template-compiler'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import Component from '~resources/components/component.vue'
import FunctionalComponent from '~resources/components/functional-component.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import {
functionalSFCsSupported,
describeWithShallowAndMount,
isRunningPhantomJS
} from '~resources/utils'
import { itSkipIf } from 'conditional-specs'
import ComponentWithoutName from '~resources/components/component-without-name.vue'
describeWithShallowAndMount('contains', (mountingMethod) => {
it('returns true if wrapper contains element', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.contains('input')).to.equal(true)
})
it('returns true if wrapper contains Vue component', () => {
const wrapper = mountingMethod(ComponentWithChild)
expect(wrapper.contains(Component)).to.equal(true)
})
it('returns true if wrapper contains functional Vue component', () => {
if (!functionalSFCsSupported) {
return false
}
const TestComponent = {
template: `
<div>
<functional-component />
</div>
`,
components: {
FunctionalComponent
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.contains(FunctionalComponent)).to.equal(true)
})
itSkipIf(
isRunningPhantomJS,
'returns true if wrapper contains Vue class component', () => {
const TestComponent = {
template: `
<div>
<component-as-a-class />
</div>
`,
components: {
ComponentAsAClass
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.contains(ComponentAsAClass)).to.equal(true)
})
it('returns true if wrapper contains element specified by ref selector', () => {
const compiled = compileToFunctions('<div><input ref="foo" /></div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.contains({ ref: 'foo' })).to.equal(true)
})
it('throws an error when ref selector is called on a wrapper that is not a Vue component', () => {
const compiled = compileToFunctions('<div><a href="/"></a></div>')
const wrapper = mountingMethod(compiled)
const a = wrapper.find('a')
const message = '[vue-test-utils]: $ref selectors can only be used on Vue component wrappers'
const fn = () => a.contains({ ref: 'foo' })
expect(fn).to.throw().with.property('message', message)
})
it('returns true when wrapper contains root element', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.contains('doesntexist')).to.equal(false)
})
it('returns true if wrapper root element matches contains', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.contains('doesntexist')).to.equal(false)
})
it('returns true if wrapper root Component matches selector', () => {
const TestComponent = {
template: `
<div>
<component-without-name />
</div>
`,
components: {
ComponentWithoutName
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.contains(ComponentWithoutName)).to.equal(true)
})
it('returns true if wrapper root Component matches selector', () => {
const wrapper = mountingMethod(Component)
expect(wrapper.contains(Component)).to.equal(true)
})
it('returns false if wrapper does not contain element', () => {
const compiled = compileToFunctions('<div></div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.contains('div')).to.equal(true)
})
it('returns false if wrapper does not contain element specified by ref selector', () => {
const compiled = compileToFunctions('<div><input ref="bar" /></div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.contains({ ref: 'foo' })).to.equal(false)
})
it('works correctly with innerHTML', () => {
const TestComponent = {
render (createElement) {
return createElement('div', {
domProps: {
innerHTML: '<svg></svg>'
}
})
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.contains('svg')).to.equal(true)
expect(wrapper.find('svg').contains('svg')).to.equal(true)
})
it('throws an error if selector is not a valid selector', () => {
const wrapper = mountingMethod(Component)
const invalidSelectors = [
undefined, null, NaN, 0, 2, true, false, () => {}, {}, { name: undefined }, { ref: 'foo', nope: true }, []
]
invalidSelectors.forEach((invalidSelector) => {
const message = '[vue-test-utils]: wrapper.contains() must be passed a valid CSS selector, Vue constructor, or valid find option object'
const fn = () => wrapper.contains(invalidSelector)
expect(fn).to.throw().with.property('message', message)
})
})
})