Skip to content

Commit d6487a8

Browse files
author
takumi-n
committed
fix: set default lazy option to true in watch api
1 parent 7c8386e commit d6487a8

File tree

3 files changed

+42
-45
lines changed

3 files changed

+42
-45
lines changed

src/apis/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export function watch(
291291
callback = null;
292292
}
293293

294-
const opts = getWatcherOption(options);
294+
const opts = getWatcherOption({ lazy: true, ...options });
295295
const vm = getWatcherVM();
296296

297297
return createWatcher(vm, source, callback, opts);

test/apis/state.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('api/ref', () => {
2727
watch(a, () => {
2828
dummy = a.value;
2929
});
30-
expect(dummy).toBe(1);
30+
expect(dummy).toBeUndefined();
3131
a.value = 2;
3232
waitForUpdate(() => {
3333
expect(dummy).toBe(2);
@@ -46,7 +46,7 @@ describe('api/ref', () => {
4646
},
4747
{ deep: true }
4848
);
49-
expect(dummy).toBe(1);
49+
expect(dummy).toBeUndefined();
5050
a.value.count = 2;
5151
waitForUpdate(() => {
5252
expect(dummy).toBe(2);
@@ -105,7 +105,7 @@ describe('api/toRefs', () => {
105105
}
106106
);
107107
const stateAsRefs = toRefs(state);
108-
expect(dummy).toBe(1);
108+
expect(dummy).toBeUndefined();
109109
expect(stateAsRefs.foo.value).toBe(1);
110110
expect(stateAsRefs.bar.value).toBe(2);
111111
state.foo++;
@@ -157,7 +157,7 @@ describe('unwrapping', () => {
157157
},
158158
{ deep: true, flush: 'sync' }
159159
);
160-
expect(dummy).toBe(0);
160+
expect(dummy).toBeUndefined();
161161
expect(obj.a).toBe(0);
162162
expect(objWrapper.value.a).toBe(0);
163163
obj.a++;
@@ -222,8 +222,8 @@ describe('unwrapping', () => {
222222
},
223223
{ deep: true, flush: 'sync' }
224224
);
225-
expect(dummy1).toBe(1);
226-
expect(dummy2).toBe(1);
225+
expect(dummy1).toBeUndefined();
226+
expect(dummy2).toBeUndefined();
227227
a.value++;
228228
expect(dummy1).toBe(2);
229229
expect(dummy2).toBe(2);
@@ -252,8 +252,8 @@ describe('unwrapping', () => {
252252
},
253253
{ deep: true, flush: 'sync' }
254254
);
255-
expect(dummy1).toBe(1);
256-
expect(dummy2).toBe(1);
255+
expect(dummy1).toBeUndefined();
256+
expect(dummy2).toBe(undefined);
257257
expect(obj.a).toBe(1);
258258
expect(obj.b.c).toBe(1);
259259
obj.a++;

test/apis/watch.spec.js

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ describe('api/watch', () => {
2323
},
2424
template: `<div>{{a}}</div>`,
2525
}).$mount();
26-
expect(spy).toBeCalledTimes(1);
27-
expect(spy).toHaveBeenLastCalledWith(1, undefined, anyFn);
26+
expect(spy).toBeCalledTimes(0);
2827
vm.a = 2;
2928
vm.a = 3;
30-
expect(spy).toBeCalledTimes(1);
29+
expect(spy).toBeCalledTimes(0);
3130
waitForUpdate(() => {
32-
expect(spy).toBeCalledTimes(2);
31+
expect(spy).toBeCalledTimes(1);
3332
expect(spy).toHaveBeenLastCalledWith(3, 1, anyFn);
3433
}).then(done);
3534
});
@@ -46,12 +45,11 @@ describe('api/watch', () => {
4645
},
4746
template: `<div>{{a}}</div>`,
4847
}).$mount();
49-
expect(spy).toBeCalledTimes(1);
50-
expect(spy).toHaveBeenLastCalledWith(1, undefined);
48+
expect(spy).toBeCalledTimes(0);
5149
vm.a = 2;
52-
expect(spy).toBeCalledTimes(1);
50+
expect(spy).toBeCalledTimes(0);
5351
waitForUpdate(() => {
54-
expect(spy).toBeCalledTimes(2);
52+
expect(spy).toBeCalledTimes(1);
5553
expect(spy).toHaveBeenLastCalledWith(2, 1);
5654
}).then(done);
5755
});
@@ -68,12 +66,11 @@ describe('api/watch', () => {
6866
},
6967
template: `<div>{{a}}</div>`,
7068
}).$mount();
71-
expect(spy).toBeCalledTimes(1);
72-
expect(spy).toHaveBeenLastCalledWith(1, undefined);
69+
expect(spy).toBeCalledTimes(0);
7370
vm.a = 2;
74-
expect(spy).toBeCalledTimes(1);
71+
expect(spy).toBeCalledTimes(0);
7572
waitForUpdate(() => {
76-
expect(spy).toBeCalledTimes(2);
73+
expect(spy).toBeCalledTimes(1);
7774
expect(spy).toHaveBeenLastCalledWith(2, 1);
7875
}).then(done);
7976
});
@@ -196,12 +193,11 @@ describe('api/watch', () => {
196193
return h('div', this.a);
197194
},
198195
}).$mount();
199-
expect(spy).toBeCalledTimes(1);
200-
expect(spy).toHaveBeenLastCalledWith(1, undefined);
196+
expect(spy).toBeCalledTimes(0);
201197
vm.a = 2;
202198
waitForUpdate(() => {
203199
expect(rerenderedText).toBe('2');
204-
expect(spy).toBeCalledTimes(2);
200+
expect(spy).toBeCalledTimes(1);
205201
expect(spy).toHaveBeenLastCalledWith(2, 1);
206202
}).then(done);
207203
});
@@ -286,9 +282,8 @@ describe('api/watch', () => {
286282
count.value++;
287283
},
288284
});
289-
expect(spy).toBeCalledTimes(2);
290-
expect(spy).toHaveBeenNthCalledWith(1, 0, undefined);
291-
expect(spy).toHaveBeenNthCalledWith(2, 1, 0);
285+
expect(spy).toBeCalledTimes(1);
286+
expect(spy).toHaveBeenNthCalledWith(1, 1, 0);
292287
});
293288

294289
it('should run in a expected order', done => {
@@ -321,7 +316,7 @@ describe('api/watch', () => {
321316
},
322317
template: `<div>{{x}}</div>`,
323318
}).$mount();
324-
expect(result).toEqual(['sync effect', 'sync callback', 'pre callback', 'post callback']);
319+
expect(result).toEqual(['sync effect']);
325320
result.length = 0;
326321

327322
waitForUpdate(() => {
@@ -419,21 +414,20 @@ describe('api/watch', () => {
419414
},
420415
template: `<div>{{obj1.a}} {{obj2.a}}</div>`,
421416
}).$mount();
422-
expect(spy).toBeCalledTimes(1);
423-
expect(spy).toHaveBeenLastCalledWith([1, 2], undefined);
417+
expect(spy).toBeCalledTimes(0);
424418
obj1.a = 2;
425419
obj2.a = 3;
426420

427421
obj1.a = 3;
428422
obj2.a = 4;
429423
waitForUpdate(() => {
430-
expect(spy).toBeCalledTimes(2);
424+
expect(spy).toBeCalledTimes(1);
431425
expect(spy).toHaveBeenLastCalledWith([3, 4], [1, 2]);
432426
obj2.a = 5;
433427
obj2.a = 6;
434428
})
435429
.then(() => {
436-
expect(spy).toBeCalledTimes(3);
430+
expect(spy).toBeCalledTimes(2);
437431
expect(spy).toHaveBeenLastCalledWith([3, 6], [3, 4]);
438432
})
439433
.then(done);
@@ -553,10 +547,9 @@ describe('api/watch', () => {
553547
it('should work', done => {
554548
const obj = reactive({ a: 1 });
555549
watch(() => obj.a, (n, o) => spy(n, o));
556-
expect(spy).toHaveBeenLastCalledWith(1, undefined);
557550
obj.a = 2;
558551
waitForUpdate(() => {
559-
expect(spy).toBeCalledTimes(2);
552+
expect(spy).toBeCalledTimes(1);
560553
expect(spy).toHaveBeenLastCalledWith(2, 1);
561554
}).then(done);
562555
});
@@ -634,7 +627,7 @@ describe('api/watch', () => {
634627
.then(done);
635628
});
636629

637-
it('run cleanup when watch stops', () => {
630+
it('run cleanup when watch stops', done => {
638631
const id = ref(1);
639632
const spy = jest.fn();
640633
const cleanup = jest.fn();
@@ -643,9 +636,16 @@ describe('api/watch', () => {
643636
onCleanup(cleanup);
644637
});
645638

646-
expect(spy).toHaveBeenCalledWith(1);
647-
stop();
648-
expect(cleanup).toHaveBeenCalled();
639+
id.value = 2;
640+
641+
waitForUpdate(() => {
642+
expect(spy).toHaveBeenLastCalledWith(2);
643+
stop();
644+
})
645+
.then(() => {
646+
expect(cleanup).toHaveBeenCalled();
647+
})
648+
.then(done);
649649
});
650650

651651
it('should not collect reactive in onCleanup', done => {
@@ -675,19 +675,16 @@ describe('api/watch', () => {
675675

676676
it('work with callback ', done => {
677677
const id = ref(1);
678-
const promises = [];
678+
let promise;
679679
watch(id, (newVal, oldVal, onCleanup) => {
680680
const val = getAsyncValue(newVal);
681-
promises.push(val);
682-
onCleanup(() => {
683-
val.cancel();
684-
});
681+
promise = val;
685682
});
686683
id.value = 2;
687684
waitForUpdate()
688685
.thenWaitFor(async next => {
689-
const values = await Promise.all(promises);
690-
expect(values).toEqual(['canceled', 2]);
686+
const value = await promise;
687+
expect(value).toEqual(2);
691688
next();
692689
})
693690
.then(done);

0 commit comments

Comments
 (0)