@@ -3,9 +3,11 @@ import {
3
3
closest ,
4
4
contains ,
5
5
getAttr ,
6
+ getShadowRootOrRoot ,
6
7
getStyle ,
7
8
hasAttr ,
8
9
hasClass ,
10
+ isConnectedToDOM ,
9
11
isDisabled ,
10
12
isElement ,
11
13
matches ,
@@ -25,28 +27,30 @@ const template = `
25
27
</div>
26
28
</div>
27
29
`
28
- const App = { template}
30
+ let App
31
+ let wrapper
29
32
30
33
describe ( 'utils/dom' , ( ) => {
31
- it ( 'isElement() works' , async ( ) => {
32
- const wrapper = mount ( App , {
34
+ beforeEach ( ( ) => {
35
+ App = { template}
36
+ wrapper = mount ( App , {
33
37
attachTo :document . body
34
38
} )
39
+ } )
40
+
41
+ afterEach ( ( ) => {
42
+ wrapper . destroy ( )
43
+ } )
35
44
45
+ it ( 'isElement() works' , async ( ) => {
36
46
expect ( wrapper ) . toBeDefined ( )
37
47
expect ( wrapper . find ( 'div.foo' ) . exists ( ) ) . toBe ( true )
38
48
expect ( isElement ( wrapper . element ) ) . toBe ( true )
39
49
expect ( isElement ( null ) ) . toBe ( false )
40
50
expect ( isElement ( App ) ) . toBe ( false )
41
-
42
- wrapper . destroy ( )
43
51
} )
44
52
45
53
it ( 'isDisabled() works' , async ( ) => {
46
- const wrapper = mount ( App , {
47
- attachTo :document . body
48
- } )
49
-
50
54
expect ( wrapper ) . toBeDefined ( )
51
55
52
56
const $btns = wrapper . findAll ( 'div.baz > button' )
@@ -55,15 +59,28 @@ describe('utils/dom', () => {
55
59
expect ( isDisabled ( $btns . at ( 0 ) . element ) ) . toBe ( false )
56
60
expect ( isDisabled ( $btns . at ( 1 ) . element ) ) . toBe ( false )
57
61
expect ( isDisabled ( $btns . at ( 2 ) . element ) ) . toBe ( true )
62
+ } )
58
63
59
- wrapper . destroy ( )
64
+ // NOTE: Need to figure out how to test against shadowDOM
65
+ it ( 'isConnectedToDOM() Regular DOM' , async ( ) => {
66
+ expect ( wrapper ) . toBeDefined ( )
67
+
68
+ const $barspan = wrapper . findAll ( 'span.barspan' )
69
+ expect ( $barspan ) . toBeDefined ( )
70
+ expect ( $barspan . length ) . toBe ( 1 )
71
+ expect ( isConnectedToDOM ( $barspan . at ( 0 ) . element ) ) . toBe ( true )
60
72
} )
61
73
62
- it ( 'hasClass() works' , async ( ) => {
63
- const wrapper = mount ( App , {
64
- attachTo :document . body
65
- } )
74
+ it ( 'getShadowRootOrRoot() Regular DOM' , async ( ) => {
75
+ expect ( wrapper ) . toBeDefined ( )
76
+
77
+ const $baz = wrapper . find ( 'div.baz' )
78
+ const $documentBody = getShadowRootOrRoot ( $baz . element )
79
+ expect ( $documentBody ) . toBeDefined ( )
80
+ expect ( $documentBody . toString ( ) ) . toBe ( '[object HTMLBodyElement]' )
81
+ } )
66
82
83
+ it ( 'hasClass() works' , async ( ) => {
67
84
expect ( wrapper ) . toBeDefined ( )
68
85
69
86
const $span = wrapper . find ( 'span.barspan' )
@@ -73,15 +90,9 @@ describe('utils/dom', () => {
73
90
expect ( hasClass ( $span . element , 'foobar' ) ) . toBe ( true )
74
91
expect ( hasClass ( $span . element , 'fizzle-rocks' ) ) . toBe ( false )
75
92
expect ( hasClass ( null , 'foobar' ) ) . toBe ( false )
76
-
77
- wrapper . destroy ( )
78
93
} )
79
94
80
95
it ( 'contains() works' , async ( ) => {
81
- const wrapper = mount ( App , {
82
- attachTo :document . body
83
- } )
84
-
85
96
expect ( wrapper ) . toBeDefined ( )
86
97
87
98
const $span = wrapper . find ( 'span.barspan' )
@@ -95,15 +106,9 @@ describe('utils/dom', () => {
95
106
expect ( contains ( wrapper . element , $btn1 . element ) ) . toBe ( true )
96
107
expect ( contains ( $span . element , $btn1 . element ) ) . toBe ( false )
97
108
expect ( contains ( null , $btn1 . element ) ) . toBe ( false )
98
-
99
- wrapper . destroy ( )
100
109
} )
101
110
102
111
it ( 'closest() works' , async ( ) => {
103
- const wrapper = mount ( App , {
104
- attachTo :document . body
105
- } )
106
-
107
112
expect ( wrapper ) . toBeDefined ( )
108
113
109
114
const $btns = wrapper . findAll ( 'div.baz > button' )
@@ -122,15 +127,9 @@ describe('utils/dom', () => {
122
127
expect ( closest ( 'div.not-here' , $btns . at ( 0 ) . element ) ) . toBe ( null )
123
128
expect ( closest ( 'div.baz' , $baz . element ) ) . toBe ( null )
124
129
expect ( closest ( 'div.baz' , $baz . element , true ) ) . toBe ( $baz . element )
125
-
126
- wrapper . destroy ( )
127
130
} )
128
131
129
132
it ( 'matches() works' , async ( ) => {
130
- const wrapper = mount ( App , {
131
- attachTo :document . body
132
- } )
133
-
134
133
expect ( wrapper ) . toBeDefined ( )
135
134
136
135
const $btns = wrapper . findAll ( 'div.baz > button' )
@@ -148,15 +147,9 @@ describe('utils/dom', () => {
148
147
expect ( matches ( $btns . at ( 0 ) . element , 'div.bar > button' ) ) . toBe ( false )
149
148
expect ( matches ( $btns . at ( 0 ) . element , 'button#button1' ) ) . toBe ( true )
150
149
expect ( matches ( null , 'div.foo' ) ) . toBe ( false )
151
-
152
- wrapper . destroy ( )
153
150
} )
154
151
155
152
it ( 'hasAttr() works' , async ( ) => {
156
- const wrapper = mount ( App , {
157
- attachTo :document . body
158
- } )
159
-
160
153
expect ( wrapper ) . toBeDefined ( )
161
154
162
155
const $btns = wrapper . findAll ( 'div.baz > button' )
@@ -169,15 +162,9 @@ describe('utils/dom', () => {
169
162
expect ( hasAttr ( $btns . at ( 2 ) . element , 'disabled' ) ) . toBe ( true )
170
163
expect ( hasAttr ( $btns . at ( 2 ) . element , 'role' ) ) . toBe ( false )
171
164
expect ( hasAttr ( null , 'role' ) ) . toBe ( null )
172
-
173
- wrapper . destroy ( )
174
165
} )
175
166
176
167
it ( 'getAttr() works' , async ( ) => {
177
- const wrapper = mount ( App , {
178
- attachTo :document . body
179
- } )
180
-
181
168
expect ( wrapper ) . toBeDefined ( )
182
169
183
170
const $btns = wrapper . findAll ( 'div.baz > button' )
@@ -193,15 +180,9 @@ describe('utils/dom', () => {
193
180
expect ( getAttr ( null , 'role' ) ) . toBe ( null )
194
181
expect ( getAttr ( $btns . at ( 0 ) . element , '' ) ) . toBe ( null )
195
182
expect ( getAttr ( $btns . at ( 0 ) . element , undefined ) ) . toBe ( null )
196
-
197
- wrapper . destroy ( )
198
183
} )
199
184
200
185
it ( 'getStyle() works' , async ( ) => {
201
- const wrapper = mount ( App , {
202
- attachTo :document . body
203
- } )
204
-
205
186
expect ( wrapper ) . toBeDefined ( )
206
187
207
188
const $span = wrapper . find ( 'span.barspan' )
@@ -210,15 +191,9 @@ describe('utils/dom', () => {
210
191
expect ( getStyle ( $span . element , 'color' ) ) . toBe ( 'red' )
211
192
expect ( getStyle ( $span . element , 'width' ) ) . toBe ( null )
212
193
expect ( getStyle ( null , 'color' ) ) . toBe ( null )
213
-
214
- wrapper . destroy ( )
215
194
} )
216
195
217
196
it ( 'select() works' , async ( ) => {
218
- const wrapper = mount ( App , {
219
- attachTo :document . body
220
- } )
221
-
222
197
expect ( wrapper ) . toBeDefined ( )
223
198
224
199
const $btns = wrapper . findAll ( 'div.baz > button' )
@@ -230,22 +205,15 @@ describe('utils/dom', () => {
230
205
expect ( select ( 'button#button3' , wrapper . element ) ) . toBe ( $btns . at ( 2 ) . element )
231
206
expect ( select ( 'span.not-here' , wrapper . element ) ) . toBe ( null )
232
207
233
- // Note: It appears that `vue-test-utils` is not detaching previous
234
- // app instances and elements once the test is complete!
208
+ // Without root element specified
235
209
expect ( select ( 'button' ) ) . not . toBe ( null )
236
210
expect ( select ( 'button' ) ) . toBe ( $btns . at ( 0 ) . element )
237
211
expect ( select ( 'button#button3' ) ) . not . toBe ( null )
238
212
expect ( select ( 'button#button3' ) ) . toBe ( $btns . at ( 2 ) . element )
239
213
expect ( select ( 'span.not-here' ) ) . toBe ( null )
240
-
241
- wrapper . destroy ( )
242
214
} )
243
215
244
216
it ( 'selectAll() works' , async ( ) => {
245
- const wrapper = mount ( App , {
246
- attachTo :document . body
247
- } )
248
-
249
217
expect ( wrapper ) . toBeDefined ( )
250
218
251
219
const $btns = wrapper . findAll ( 'div.baz > button' )
@@ -268,8 +236,6 @@ describe('utils/dom', () => {
268
236
expect ( selectAll ( 'div.baz button' , wrapper . element ) [ 2 ] ) . toBe ( $btns . at ( 2 ) . element )
269
237
270
238
// Without root element specified (assumes document as root)
271
- // Note: It appears that `vue-test-utils` is not detaching previous
272
- // app instances and elements once the test is complete!
273
239
expect ( Array . isArray ( selectAll ( 'button' ) ) ) . toBe ( true )
274
240
expect ( selectAll ( 'button' ) ) . not . toEqual ( [ ] )
275
241
expect ( selectAll ( 'button' ) . length ) . toBe ( 3 )
@@ -285,7 +251,5 @@ describe('utils/dom', () => {
285
251
expect ( selectAll ( 'div.baz button' ) [ 0 ] ) . toBe ( $btns . at ( 0 ) . element )
286
252
expect ( selectAll ( 'div.baz button' ) [ 1 ] ) . toBe ( $btns . at ( 1 ) . element )
287
253
expect ( selectAll ( 'div.baz button' ) [ 2 ] ) . toBe ( $btns . at ( 2 ) . element )
288
-
289
- wrapper . destroy ( )
290
254
} )
291
255
} )