1
1
import { compileToFunctions } from 'vue-template-compiler'
2
2
import ComponentWithVIf from '~resources/components/component-with-v-if.vue'
3
3
import ComponentWithWatch from '~resources/components/component-with-watch.vue'
4
- import { describeWithShallowAndMount } from '~resources/test-utils'
4
+ import {
5
+ describeWithShallowAndMount ,
6
+ vueVersion
7
+ } from '~resources/test-utils'
5
8
6
- describeWithShallowAndMount ( 'setData' , ( method ) => {
9
+ describeWithShallowAndMount ( 'setData' , ( mountingMethod ) => {
7
10
let info
8
11
9
12
beforeEach ( ( ) => {
@@ -15,7 +18,7 @@ describeWithShallowAndMount('setData', (method) => {
15
18
} )
16
19
17
20
it ( 'sets component data and updates nested vm nodes when called on Vue instance' , ( ) => {
18
- const wrapper = method ( ComponentWithVIf )
21
+ const wrapper = mountingMethod ( ComponentWithVIf )
19
22
expect ( wrapper . findAll ( '.child.ready' ) . length ) . to . equal ( 0 )
20
23
wrapper . setData ( { ready : true } )
21
24
expect ( wrapper . findAll ( '.child.ready' ) . length ) . to . equal ( 1 )
@@ -30,35 +33,57 @@ describeWithShallowAndMount('setData', (method) => {
30
33
}
31
34
}
32
35
}
33
- const wrapper = method ( Component )
36
+ const wrapper = mountingMethod ( Component )
34
37
wrapper . setData ( { show : true } )
35
38
wrapper . update ( )
36
39
expect ( wrapper . element ) . to . equal ( wrapper . vm . $el )
37
40
expect ( wrapper . hasClass ( 'some-class' ) ) . to . be . true
38
41
} )
39
42
40
43
it ( 'runs watch function when data is updated' , ( ) => {
41
- const wrapper = method ( ComponentWithWatch )
44
+ const wrapper = mountingMethod ( ComponentWithWatch )
42
45
const data1 = 'testest'
43
46
wrapper . setData ( { data1 } )
44
47
expect ( wrapper . vm . data2 ) . to . equal ( data1 )
45
48
} )
46
49
47
50
it ( 'runs watch function after all props are updated' , ( ) => {
48
- const wrapper = method ( ComponentWithWatch )
51
+ const wrapper = mountingMethod ( ComponentWithWatch )
49
52
const data1 = 'testest'
50
53
wrapper . setData ( { data2 : 'newProp' , data1 } )
51
54
expect ( info . args [ 0 ] [ 0 ] ) . to . equal ( data1 )
52
55
} )
53
56
54
- it ( 'throws an error if node is not a Vue instance' , ( ) => {
57
+ it ( 'throws error if node is not a Vue instance' , ( ) => {
55
58
const message = 'wrapper.setData() can only be called on a Vue instance'
56
59
const compiled = compileToFunctions ( '<div><p></p></div>' )
57
- const wrapper = method ( compiled )
60
+ const wrapper = mountingMethod ( compiled )
58
61
const p = wrapper . find ( 'p' )
59
62
expect ( ( ) => p . setData ( { ready : true } ) ) . throw ( Error , message )
60
63
} )
61
64
65
+ it ( 'throws error when called on functional vnode' , ( ) => {
66
+ const AFunctionalComponent = {
67
+ render : ( h , context ) => h ( 'div' , context . prop1 ) ,
68
+ functional : true
69
+ }
70
+ const message = '[vue-test-utils]: wrapper.setData() canot be called on a functional component'
71
+ const fn = ( ) => mountingMethod ( AFunctionalComponent ) . setData ( { data1 : 'data' } )
72
+ expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
73
+ // find on functional components isn't supported in Vue < 2.3
74
+ if ( vueVersion < 2.3 ) {
75
+ return
76
+ }
77
+ const TestComponent = {
78
+ template : '<div><a-functional-component /></div>' ,
79
+ components : {
80
+ AFunctionalComponent
81
+ }
82
+ }
83
+ const fn2 = ( ) => mountingMethod ( TestComponent ) . find ( AFunctionalComponent ) . setData ( { data1 : 'data' } )
84
+ expect ( fn2 ) . to . throw ( ) . with . property ( 'message' , message )
85
+ } )
86
+
62
87
it ( 'should not run watchers if data updated is null' , ( ) => {
63
88
const TestComponent = {
64
89
template : `
@@ -76,7 +101,7 @@ describeWithShallowAndMount('setData', (method) => {
76
101
}
77
102
}
78
103
}
79
- const wrapper = method ( TestComponent )
104
+ const wrapper = mountingMethod ( TestComponent )
80
105
wrapper . setData ( { message : null } )
81
106
expect ( wrapper . text ( ) ) . to . equal ( 'There is no message yet' )
82
107
} )
0 commit comments