@@ -23,13 +23,13 @@ describe('Hooks watch', () => {
23
23
a,
24
24
} ;
25
25
} ,
26
- } ) ;
27
- expect ( spy ) . toHaveBeenCalled ( ) ;
26
+ template : `<div>{{a}}</div>` ,
27
+ } ) . $mount ( ) ;
28
28
expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
29
29
expect ( spy ) . toHaveBeenLastCalledWith ( 1 , undefined ) ;
30
30
vm . a = 2 ;
31
31
expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
32
- waitForWatcherFlush ( ( ) => {
32
+ waitForUpdate ( ( ) => {
33
33
expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
34
34
expect ( spy ) . toHaveBeenLastCalledWith ( 2 , 1 ) ;
35
35
} ) . then ( done ) ;
@@ -45,37 +45,96 @@ describe('Hooks watch', () => {
45
45
a,
46
46
} ;
47
47
} ,
48
- } ) ;
49
- expect ( spy ) . toHaveBeenCalled ( ) ;
48
+ template : `<div>{{a}}</div>` ,
49
+ } ) . $mount ( ) ;
50
50
expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
51
51
expect ( spy ) . toHaveBeenLastCalledWith ( 1 , undefined ) ;
52
52
vm . a = 2 ;
53
53
expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
54
- waitForWatcherFlush ( ( ) => {
54
+ waitForUpdate ( ( ) => {
55
55
expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
56
56
expect ( spy ) . toHaveBeenLastCalledWith ( 2 , 1 ) ;
57
57
} ) . then ( done ) ;
58
58
} ) ;
59
59
60
- it ( 'basic usage(multiple sources)' , done => {
60
+ it ( 'basic usage(multiple sources, lazy=false, flush=none-sync)' , done => {
61
+ const vm = new Vue ( {
62
+ setup ( ) {
63
+ const a = value ( 1 ) ;
64
+ const b = value ( 1 ) ;
65
+ watch ( [ a , b ] , spy , { lazy : false , flush : 'post' } ) ;
66
+
67
+ return {
68
+ a,
69
+ b,
70
+ } ;
71
+ } ,
72
+ template : `<div>{{a}} {{b}}</div>` ,
73
+ } ) . $mount ( ) ;
74
+ expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
75
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 1 , 1 ] , [ undefined , undefined ] ) ;
76
+ vm . a = 2 ;
77
+ expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
78
+ waitForUpdate ( ( ) => {
79
+ expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
80
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 2 , 1 ] , [ 1 , undefined ] ) ;
81
+ vm . a = 3 ;
82
+ vm . b = 3 ;
83
+ } )
84
+ . then ( ( ) => {
85
+ expect ( spy . mock . calls . length ) . toBe ( 3 ) ;
86
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 3 , 3 ] , [ 2 , 1 ] ) ;
87
+ } )
88
+ . then ( done ) ;
89
+ } ) ;
90
+
91
+ it ( 'basic usage(multiple sources, lazy=true, flush=none-sync)' , done => {
92
+ const vm = new Vue ( {
93
+ setup ( ) {
94
+ const a = value ( 1 ) ;
95
+ const b = value ( 1 ) ;
96
+ watch ( [ a , b ] , spy , { lazy : true , flush : 'post' } ) ;
97
+
98
+ return {
99
+ a,
100
+ b,
101
+ } ;
102
+ } ,
103
+ template : `<div>{{a}} {{b}}</div>` ,
104
+ } ) . $mount ( ) ;
105
+ vm . a = 2 ;
106
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
107
+ waitForUpdate ( ( ) => {
108
+ expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
109
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 2 , 1 ] , [ 1 , undefined ] ) ;
110
+ vm . a = 3 ;
111
+ vm . b = 3 ;
112
+ } )
113
+ . then ( ( ) => {
114
+ expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
115
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 3 , 3 ] , [ 2 , 1 ] ) ;
116
+ } )
117
+ . then ( done ) ;
118
+ } ) ;
119
+
120
+ it ( 'basic usage(multiple sources, lazy=false, flush=sync)' , done => {
61
121
const vm = new Vue ( {
62
122
setup ( ) {
63
123
const a = value ( 1 ) ;
64
124
const b = value ( 1 ) ;
65
- watch ( [ a , b ] , spy ) ;
125
+ watch ( [ a , b ] , spy , { lazy : false , flush : 'sync' } ) ;
66
126
67
127
return {
68
128
a,
69
129
b,
70
130
} ;
71
131
} ,
72
132
} ) ;
73
- expect ( spy ) . toHaveBeenCalled ( ) ;
74
133
expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
75
134
expect ( spy ) . toHaveBeenLastCalledWith ( [ 1 , 1 ] , [ undefined , undefined ] ) ;
76
135
vm . a = 2 ;
77
136
expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
78
- waitForWatcherFlush ( ( ) => {
137
+ waitForUpdate ( ( ) => {
79
138
expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
80
139
expect ( spy ) . toHaveBeenLastCalledWith ( [ 2 , 1 ] , [ 1 , undefined ] ) ;
81
140
vm . a = 3 ;
@@ -88,21 +147,79 @@ describe('Hooks watch', () => {
88
147
. then ( done ) ;
89
148
} ) ;
90
149
150
+ it ( 'basic usage(multiple sources, lazy=true, flush=sync)' , done => {
151
+ const vm = new Vue ( {
152
+ setup ( ) {
153
+ const a = value ( 1 ) ;
154
+ const b = value ( 1 ) ;
155
+ watch ( [ a , b ] , spy , { lazy : true , flush : 'sync' } ) ;
156
+
157
+ return {
158
+ a,
159
+ b,
160
+ } ;
161
+ } ,
162
+ } ) ;
163
+ vm . a = 2 ;
164
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
165
+ waitForUpdate ( ( ) => {
166
+ expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
167
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 2 , 1 ] , [ 1 , undefined ] ) ;
168
+ vm . a = 3 ;
169
+ vm . b = 3 ;
170
+ } )
171
+ . then ( ( ) => {
172
+ expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
173
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 3 , 3 ] , [ 2 , 1 ] ) ;
174
+ } )
175
+ . then ( done ) ;
176
+ } ) ;
177
+
178
+ it ( 'out of setup' , done => {
179
+ const obj = state ( { a : 1 } ) ;
180
+ watch ( ( ) => obj . a , spy ) ;
181
+ expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
182
+ expect ( spy ) . toHaveBeenLastCalledWith ( 1 , undefined ) ;
183
+ obj . a = 2 ;
184
+ waitForUpdate ( ( ) => {
185
+ expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
186
+ expect ( spy ) . toHaveBeenCalledWith ( 2 , 1 ) ;
187
+ } ) . then ( done ) ;
188
+ } ) ;
189
+
190
+ it ( 'out of setup(multiple sources)' , done => {
191
+ const obj1 = state ( { a : 1 } ) ;
192
+ const obj2 = state ( { a : 2 } ) ;
193
+ watch ( [ ( ) => obj1 . a , ( ) => obj2 . a ] , spy ) ;
194
+ expect ( spy . mock . calls . length ) . toBe ( 1 ) ;
195
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 1 , 2 ] , [ undefined , undefined ] ) ;
196
+ obj1 . a = 2 ;
197
+ obj2 . a = 3 ;
198
+ waitForUpdate ( ( ) => {
199
+ expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
200
+ expect ( spy ) . toHaveBeenLastCalledWith ( [ 2 , 3 ] , [ 1 , 2 ] ) ;
201
+ } ) . then ( done ) ;
202
+ } ) ;
203
+
91
204
it ( 'multiple cbs (after option merge)' , done => {
92
205
const spy1 = jest . fn ( ) ;
93
- const obj = state ( { a : 1 } ) ;
206
+ const a = value ( 1 ) ;
94
207
const Test = Vue . extend ( {
95
208
setup ( ) {
96
- watch ( ( ) => obj . a , spy1 ) ;
209
+ watch ( a , spy1 ) ;
97
210
} ,
98
211
} ) ;
99
212
new Test ( {
100
213
setup ( ) {
101
- watch ( ( ) => obj . a , spy ) ;
214
+ watch ( a , spy ) ;
215
+ return {
216
+ a,
217
+ } ;
102
218
} ,
103
- } ) ;
104
- obj . a = 2 ;
105
- waitForWatcherFlush ( ( ) => {
219
+ template : `<div>{{a}}</div>` ,
220
+ } ) . $mount ( ) ;
221
+ a . value = 2 ;
222
+ waitForUpdate ( ( ) => {
106
223
expect ( spy1 ) . toHaveBeenCalledWith ( 2 , 1 ) ;
107
224
expect ( spy ) . toHaveBeenCalledWith ( 2 , 1 ) ;
108
225
} ) . then ( done ) ;
@@ -118,10 +235,11 @@ describe('Hooks watch', () => {
118
235
a,
119
236
} ;
120
237
} ,
121
- } ) ;
238
+ template : `<div>{{a}}</div>` ,
239
+ } ) . $mount ( ) ;
122
240
expect ( spy ) . not . toHaveBeenCalled ( ) ;
123
241
vm . a = 2 ;
124
- waitForWatcherFlush ( ( ) => {
242
+ waitForUpdate ( ( ) => {
125
243
expect ( spy ) . toHaveBeenCalledWith ( 2 , 1 ) ;
126
244
} ) . then ( done ) ;
127
245
} ) ;
@@ -136,12 +254,13 @@ describe('Hooks watch', () => {
136
254
a,
137
255
} ;
138
256
} ,
139
- } ) ;
257
+ template : `<div>{{a}}</div>` ,
258
+ } ) . $mount ( ) ;
140
259
const oldA = vm . a ;
141
260
expect ( spy ) . not . toHaveBeenCalled ( ) ;
142
261
vm . a . b = 2 ;
143
262
expect ( spy ) . not . toHaveBeenCalled ( ) ;
144
- waitForWatcherFlush ( ( ) => {
263
+ waitForUpdate ( ( ) => {
145
264
expect ( spy ) . toHaveBeenCalledWith ( vm . a , vm . a ) ;
146
265
vm . a = { b : 3 } ;
147
266
} )
@@ -238,11 +357,14 @@ describe('Hooks watch', () => {
238
357
数据 : a ,
239
358
} ;
240
359
} ,
241
- } ) ;
360
+ render ( h ) {
361
+ return h ( 'div' , this [ '数据' ] ) ;
362
+ } ,
363
+ } ) . $mount ( ) ;
242
364
expect ( spy ) . not . toHaveBeenCalled ( ) ;
243
365
vm [ '数据' ] = 2 ;
244
366
expect ( spy ) . not . toHaveBeenCalled ( ) ;
245
- waitForWatcherFlush ( ( ) => {
367
+ waitForUpdate ( ( ) => {
246
368
expect ( spy ) . toHaveBeenCalledWith ( 2 , 1 ) ;
247
369
} ) . then ( done ) ;
248
370
} ) ;
0 commit comments