@@ -17,6 +17,7 @@ import {
17
17
getCheckedEvent ,
18
18
isPhantomJS ,
19
19
nextTick ,
20
+ warn ,
20
21
warnDeprecated
21
22
} from 'shared/util'
22
23
import { isElementVisible } from 'shared/is-visible'
@@ -82,14 +83,27 @@ export default class Wrapper implements BaseWrapper {
82
83
}
83
84
}
84
85
86
+ /**
87
+ * Prints warning if component is destroyed
88
+ */
89
+ __warnIfDestroyed ( ) {
90
+ if ( ! this . exists ( ) ) {
91
+ warn ( 'Operations on destroyed component are discouraged' )
92
+ }
93
+ }
94
+
85
95
at ( ) : void {
96
+ this. __warnIfDestroyed ( )
97
+
86
98
throwError ( 'at() must be called on a WrapperArray' )
87
99
}
88
100
89
101
/**
90
102
* Returns an Object containing all the attribute/value pairs on the element.
91
103
*/
92
104
attributes ( key ? : string ) : { [ name : string ] : string } | string {
105
+ this . __warnIfDestroyed ( )
106
+
93
107
const attributes = this . element . attributes
94
108
const attributeMap = { }
95
109
for ( let i = 0 ; i < attributes . length ; i ++ ) {
@@ -104,6 +118,8 @@ export default class Wrapper implements BaseWrapper {
104
118
* Returns an Array containing all the classes on the element
105
119
*/
106
120
classes ( className ? : string ) : Array < string > | boolean {
121
+ this . __warnIfDestroyed ( )
122
+
107
123
const classAttribute = this . element . getAttribute ( 'class' )
108
124
let classes = classAttribute ? classAttribute . split ( ' ' ) : [ ]
109
125
// Handle converting cssmodules identifiers back to the original class name
@@ -134,6 +150,9 @@ export default class Wrapper implements BaseWrapper {
134
150
'contains' ,
135
151
'Use `wrapper.find`, `wrapper.findComponent` or `wrapper.get` instead'
136
152
)
153
+
154
+ this . __warnIfDestroyed ( )
155
+
137
156
const selector = getSelector ( rawSelector , 'contains' )
138
157
const nodes = find ( this . rootNode , this . vm , selector )
139
158
return nodes . length > 0
@@ -209,6 +228,8 @@ export default class Wrapper implements BaseWrapper {
209
228
* matches the provided selector.
210
229
*/
211
230
get ( rawSelector : Selector ) : Wrapper {
231
+ this . __warnIfDestroyed ( )
232
+
212
233
const found = this . find ( rawSelector )
213
234
if ( found instanceof ErrorWrapper ) {
214
235
throw new Error ( `Unable to find ${ rawSelector } within: ${ this . html ( ) } ` )
@@ -221,6 +242,8 @@ export default class Wrapper implements BaseWrapper {
221
242
* matches the provided selector.
222
243
*/
223
244
find ( rawSelector : Selector ) : Wrapper | ErrorWrapper {
245
+ this . __warnIfDestroyed ( )
246
+
224
247
const selector = getSelector ( rawSelector , 'find' )
225
248
if ( selector . type !== DOM_SELECTOR ) {
226
249
warnDeprecated (
@@ -237,6 +260,8 @@ export default class Wrapper implements BaseWrapper {
237
260
* matches the provided selector.
238
261
*/
239
262
findComponent ( rawSelector : Selector ) : Wrapper | ErrorWrapper {
263
+ this . __warnIfDestroyed ( )
264
+
240
265
const selector = getSelector ( rawSelector , 'findComponent' )
241
266
if ( ! this . vm && ! this . isFunctionalComponent ) {
242
267
throwError (
@@ -270,6 +295,8 @@ export default class Wrapper implements BaseWrapper {
270
295
* the provided selector.
271
296
*/
272
297
findAll ( rawSelector : Selector ) : WrapperArray {
298
+ this . __warnIfDestroyed ( )
299
+
273
300
const selector = getSelector ( rawSelector , 'findAll' )
274
301
if ( selector . type !== DOM_SELECTOR ) {
275
302
warnDeprecated (
@@ -285,6 +312,8 @@ export default class Wrapper implements BaseWrapper {
285
312
* the provided selector.
286
313
*/
287
314
findAllComponents ( rawSelector : Selector ) : WrapperArray {
315
+ this . __warnIfDestroyed ( )
316
+
288
317
const selector = getSelector ( rawSelector , 'findAll' )
289
318
if ( ! this . vm ) {
290
319
throwError (
@@ -318,13 +347,17 @@ export default class Wrapper implements BaseWrapper {
318
347
* Returns HTML of element as a string
319
348
*/
320
349
html ( ) : string {
350
+ this . __warnIfDestroyed ( )
351
+
321
352
return pretty ( this . element . outerHTML )
322
353
}
323
354
324
355
/**
325
356
* Checks if node matches selector or component definition
326
357
*/
327
358
is ( rawSelector : Selector ) : boolean {
359
+ this . __warnIfDestroyed ( )
360
+
328
361
const selector = getSelector ( rawSelector , 'is' )
329
362
330
363
if ( selector . type === DOM_SELECTOR ) {
@@ -351,6 +384,8 @@ export default class Wrapper implements BaseWrapper {
351
384
'Consider a custom matcher such as those provided in jest-dom: https://github.com/testing-library/jest-dom#tobeempty. ' +
352
385
'When using with findComponent, access the DOM element with findComponent(Comp).element'
353
386
)
387
+ this . __warnIfDestroyed ( )
388
+
354
389
if ( ! this . vnode ) {
355
390
return this . element . innerHTML === ''
356
391
}
@@ -375,6 +410,8 @@ export default class Wrapper implements BaseWrapper {
375
410
* Checks if node is visible
376
411
*/
377
412
isVisible ( ) : boolean {
413
+ this . __warnIfDestroyed ( )
414
+
378
415
return isElementVisible ( this . element )
379
416
}
380
417
@@ -384,6 +421,8 @@ export default class Wrapper implements BaseWrapper {
384
421
*/
385
422
isVueInstance ( ) : boolean {
386
423
warnDeprecated ( `isVueInstance` )
424
+ this . __warnIfDestroyed ( )
425
+
387
426
return ! ! this . vm
388
427
}
389
428
@@ -393,6 +432,7 @@ export default class Wrapper implements BaseWrapper {
393
432
*/
394
433
name ( ) : string {
395
434
warnDeprecated ( `name` )
435
+ this . __warnIfDestroyed ( )
396
436
397
437
if ( this . vm ) {
398
438
return (
@@ -416,6 +456,7 @@ export default class Wrapper implements BaseWrapper {
416
456
*/
417
457
overview ( ) : void {
418
458
warnDeprecated ( `overview` )
459
+ this . __warnIfDestroyed ( )
419
460
420
461
if ( ! this . vm ) {
421
462
throwError ( `wrapper.overview() can only be called on a Vue instance` )
@@ -495,6 +536,7 @@ export default class Wrapper implements BaseWrapper {
495
536
if ( ! this . vm ) {
496
537
throwError ( 'wrapper.props() must be called on a Vue instance' )
497
538
}
539
+ this . __warnIfDestroyed ( )
498
540
499
541
const props = { }
500
542
const keys = this . vm && this . vm . $options . _propKeys
@@ -519,6 +561,8 @@ export default class Wrapper implements BaseWrapper {
519
561
* @deprecated
520
562
*/
521
563
setChecked ( checked : boolean = true ) : Promise < * > {
564
+ this . __warnIfDestroyed ( )
565
+
522
566
if ( typeof checked !== 'boolean' ) {
523
567
throwError ( 'wrapper.setChecked() must be passed a boolean' )
524
568
}
@@ -568,6 +612,8 @@ export default class Wrapper implements BaseWrapper {
568
612
* @deprecated
569
613
*/
570
614
setSelected ( ) : Promise < void > {
615
+ this . __warnIfDestroyed ( )
616
+
571
617
const tagName = this . element . tagName
572
618
573
619
if ( tagName === 'SELECT' ) {
@@ -613,6 +659,8 @@ export default class Wrapper implements BaseWrapper {
613
659
throwError ( `wrapper.setData() can only be called on a Vue instance` )
614
660
}
615
661
662
+ this . __warnIfDestroyed ( )
663
+
616
664
recursivelySetData ( this . vm , this . vm , data )
617
665
return nextTick ( )
618
666
}
@@ -630,6 +678,8 @@ export default class Wrapper implements BaseWrapper {
630
678
if ( ! this . vm ) {
631
679
throwError ( `wrapper.setMethods() can only be called on a Vue instance` )
632
680
}
681
+ this . __warnIfDestroyed ( )
682
+
633
683
Object . keys ( methods ) . forEach ( key => {
634
684
// $FlowIgnore : Problem with possibly null this.vm
635
685
this . vm [ key ] = methods [ key ]
@@ -657,6 +707,7 @@ export default class Wrapper implements BaseWrapper {
657
707
if ( ! this . vm ) {
658
708
throwError ( `wrapper.setProps() can only be called on a Vue instance` )
659
709
}
710
+ this . __warnIfDestroyed ( )
660
711
661
712
// Save the original "silent" config so that we can directly mutate props
662
713
const originalConfig = Vue . config . silent
@@ -730,6 +781,7 @@ export default class Wrapper implements BaseWrapper {
730
781
const tagName = this . element . tagName
731
782
// $FlowIgnore
732
783
const type = this . attributes ( ) . type
784
+ this . __warnIfDestroyed ( )
733
785
734
786
if ( tagName === 'OPTION' ) {
735
787
throwError (
@@ -782,13 +834,17 @@ export default class Wrapper implements BaseWrapper {
782
834
* Return text of wrapper element
783
835
*/
784
836
text ( ) : string {
837
+ this . __warnIfDestroyed ( )
838
+
785
839
return this . element . textContent . trim ( )
786
840
}
787
841
788
842
/**
789
843
* Dispatches a DOM event on wrapper
790
844
*/
791
845
trigger ( type : string , options : Object = { } ) : Promise < void > {
846
+ this . __warnIfDestroyed ( )
847
+
792
848
if ( typeof type !== 'string' ) {
793
849
throwError ( 'wrapper.trigger() must be passed a string' )
794
850
}
0 commit comments