forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis.spec.js
137 lines (120 loc) · 4.52 KB
/
is.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
import { compileToFunctions } from 'vue-template-compiler'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import Component from '~resources/components/component.vue'
import ComponentWithoutName from '~resources/components/component-without-name.vue'
import FunctionalComponent from '~resources/components/functional-component.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import {
functionalSFCsSupported,
describeWithShallowAndMount
} from '~resources/utils'
describeWithShallowAndMount('is', mountingMethod => {
it('returns true if root node matches tag selector', () => {
const compiled = compileToFunctions('<input />')
const wrapper = mountingMethod(compiled)
expect(wrapper.is('input')).to.equal(true)
})
it('returns true if root node matches class selector', () => {
const compiled = compileToFunctions('<div class="div" />')
const wrapper = mountingMethod(compiled)
expect(wrapper.is('.div')).to.equal(true)
})
it('returns true if root node matches id selector', () => {
const compiled = compileToFunctions('<div id="div" />')
const wrapper = mountingMethod(compiled)
expect(wrapper.is('#div')).to.equal(true)
})
it('returns true if root node matches Vue Component selector', () => {
const wrapper = mountingMethod(ComponentWithChild)
const component = wrapper.findAll(Component).at(0)
expect(component.is(Component)).to.equal(true)
})
it('returns true if root node matches Component', () => {
const wrapper = mountingMethod(Component)
expect(wrapper.is(Component)).to.equal(true)
})
it('returns true if root node matches Component without a name', () => {
const wrapper = mountingMethod(ComponentWithoutName)
expect(wrapper.is(ComponentWithoutName)).to.equal(true)
})
it('works correctly with innerHTML', () => {
const TestComponent = {
render (createElement) {
return createElement('div', {
domProps: {
innerHTML: '<svg></svg>'
}
})
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.find('svg').is('svg')).to.equal(true)
})
it('returns true if root node matches functional Component', () => {
if (!functionalSFCsSupported) {
return
}
const wrapper = mountingMethod(FunctionalComponent)
expect(wrapper.is(FunctionalComponent)).to.equal(true)
})
it('returns true if root node matches Component extending class component', () => {
const wrapper = mountingMethod(ComponentAsAClass)
expect(wrapper.is(ComponentAsAClass)).to.equal(true)
})
it('returns false if root node is not a Vue Component', () => {
const wrapper = mountingMethod(ComponentWithChild)
const input = wrapper.findAll('span').at(0)
expect(input.is(Component)).to.equal(false)
})
it('returns false if root node does not match tag selector', () => {
const compiled = compileToFunctions('<div />')
const wrapper = mountingMethod(compiled)
expect(wrapper.is('p')).to.equal(false)
})
it('returns false if root node does not match class selector', () => {
const compiled = compileToFunctions('<div />')
const wrapper = mountingMethod(compiled)
expect(wrapper.is('.p')).to.equal(false)
})
it('returns false if root node does not match id selector', () => {
const compiled = compileToFunctions('<div />')
const wrapper = mountingMethod(compiled)
expect(wrapper.is('#p')).to.equal(false)
})
it('throws error if ref options object is passed', () => {
const compiled = compileToFunctions('<div />')
const wrapper = mountingMethod(compiled)
const message =
'[vue-test-utils]: $ref selectors can not be used with wrapper.is()'
const fn = () => wrapper.is({ ref: 'foo' })
expect(fn)
.to.throw()
.with.property('message', message)
})
it('throws an error if selector is not a valid selector', () => {
const compiled = compileToFunctions('<div />')
const wrapper = mountingMethod(compiled)
const invalidSelectors = [
undefined,
null,
NaN,
0,
2,
true,
false,
() => {},
{},
{ name: undefined },
{ ref: 'foo', nope: true },
[]
]
invalidSelectors.forEach(invalidSelector => {
const message =
'[vue-test-utils]: wrapper.is() must be passed a valid CSS selector, Vue constructor, or valid find option object'
const fn = () => wrapper.is(invalidSelector)
expect(fn)
.to.throw()
.with.property('message', message)
})
})
})