|
5 | 5 | ref,
|
6 | 6 | WritableComputedRef,
|
7 | 7 | isReadonly,
|
8 |
| - setComputedScheduler, |
9 | 8 | DebuggerEvent,
|
10 | 9 | toRaw,
|
11 | 10 | TrackOpTypes,
|
@@ -273,218 +272,4 @@ describe('reactivity/computed', () => {
|
273 | 272 | oldValue: 2
|
274 | 273 | })
|
275 | 274 | })
|
276 |
| - |
277 |
| - describe('with scheduler', () => { |
278 |
| - // a simple scheduler similar to the main Vue scheduler |
279 |
| - const tick = Promise.resolve() |
280 |
| - const queue: any[] = [] |
281 |
| - let queued = false |
282 |
| - |
283 |
| - const schedule = (fn: any) => { |
284 |
| - queue.push(fn) |
285 |
| - if (!queued) { |
286 |
| - queued = true |
287 |
| - tick.then(flush) |
288 |
| - } |
289 |
| - } |
290 |
| - |
291 |
| - const flush = () => { |
292 |
| - for (let i = 0; i < queue.length; i++) { |
293 |
| - queue[i]() |
294 |
| - } |
295 |
| - queue.length = 0 |
296 |
| - queued = false |
297 |
| - } |
298 |
| - |
299 |
| - beforeEach(() => { |
300 |
| - setComputedScheduler(schedule) |
301 |
| - }) |
302 |
| - |
303 |
| - afterEach(() => { |
304 |
| - setComputedScheduler(undefined) |
305 |
| - }) |
306 |
| - |
307 |
| - test('should only trigger once on multiple mutations', async () => { |
308 |
| - const src = ref(0) |
309 |
| - const c = computed(() => src.value) |
310 |
| - const spy = jest.fn() |
311 |
| - effect(() => { |
312 |
| - spy(c.value) |
313 |
| - }) |
314 |
| - expect(spy).toHaveBeenCalledTimes(1) |
315 |
| - src.value = 1 |
316 |
| - src.value = 2 |
317 |
| - src.value = 3 |
318 |
| - // not called yet |
319 |
| - expect(spy).toHaveBeenCalledTimes(1) |
320 |
| - await tick |
321 |
| - // should only trigger once |
322 |
| - expect(spy).toHaveBeenCalledTimes(2) |
323 |
| - expect(spy).toHaveBeenCalledWith(c.value) |
324 |
| - }) |
325 |
| - |
326 |
| - test('should not trigger if value did not change', async () => { |
327 |
| - const src = ref(0) |
328 |
| - const c = computed(() => src.value % 2) |
329 |
| - const spy = jest.fn() |
330 |
| - effect(() => { |
331 |
| - spy(c.value) |
332 |
| - }) |
333 |
| - expect(spy).toHaveBeenCalledTimes(1) |
334 |
| - src.value = 1 |
335 |
| - src.value = 2 |
336 |
| - |
337 |
| - await tick |
338 |
| - // should not trigger |
339 |
| - expect(spy).toHaveBeenCalledTimes(1) |
340 |
| - |
341 |
| - src.value = 3 |
342 |
| - src.value = 4 |
343 |
| - src.value = 5 |
344 |
| - await tick |
345 |
| - // should trigger because latest value changes |
346 |
| - expect(spy).toHaveBeenCalledTimes(2) |
347 |
| - }) |
348 |
| - |
349 |
| - test('chained computed trigger', async () => { |
350 |
| - const effectSpy = jest.fn() |
351 |
| - const c1Spy = jest.fn() |
352 |
| - const c2Spy = jest.fn() |
353 |
| - |
354 |
| - const src = ref(0) |
355 |
| - const c1 = computed(() => { |
356 |
| - c1Spy() |
357 |
| - return src.value % 2 |
358 |
| - }) |
359 |
| - const c2 = computed(() => { |
360 |
| - c2Spy() |
361 |
| - return c1.value + 1 |
362 |
| - }) |
363 |
| - |
364 |
| - effect(() => { |
365 |
| - effectSpy(c2.value) |
366 |
| - }) |
367 |
| - |
368 |
| - expect(c1Spy).toHaveBeenCalledTimes(1) |
369 |
| - expect(c2Spy).toHaveBeenCalledTimes(1) |
370 |
| - expect(effectSpy).toHaveBeenCalledTimes(1) |
371 |
| - |
372 |
| - src.value = 1 |
373 |
| - await tick |
374 |
| - expect(c1Spy).toHaveBeenCalledTimes(2) |
375 |
| - expect(c2Spy).toHaveBeenCalledTimes(2) |
376 |
| - expect(effectSpy).toHaveBeenCalledTimes(2) |
377 |
| - }) |
378 |
| - |
379 |
| - test('chained computed avoid re-compute', async () => { |
380 |
| - const effectSpy = jest.fn() |
381 |
| - const c1Spy = jest.fn() |
382 |
| - const c2Spy = jest.fn() |
383 |
| - |
384 |
| - const src = ref(0) |
385 |
| - const c1 = computed(() => { |
386 |
| - c1Spy() |
387 |
| - return src.value % 2 |
388 |
| - }) |
389 |
| - const c2 = computed(() => { |
390 |
| - c2Spy() |
391 |
| - return c1.value + 1 |
392 |
| - }) |
393 |
| - |
394 |
| - effect(() => { |
395 |
| - effectSpy(c2.value) |
396 |
| - }) |
397 |
| - |
398 |
| - expect(effectSpy).toHaveBeenCalledTimes(1) |
399 |
| - src.value = 2 |
400 |
| - src.value = 4 |
401 |
| - src.value = 6 |
402 |
| - await tick |
403 |
| - // c1 should re-compute once. |
404 |
| - expect(c1Spy).toHaveBeenCalledTimes(2) |
405 |
| - // c2 should not have to re-compute because c1 did not change. |
406 |
| - expect(c2Spy).toHaveBeenCalledTimes(1) |
407 |
| - // effect should not trigger because c2 did not change. |
408 |
| - expect(effectSpy).toHaveBeenCalledTimes(1) |
409 |
| - }) |
410 |
| - |
411 |
| - test('chained computed value invalidation', async () => { |
412 |
| - const effectSpy = jest.fn() |
413 |
| - const c1Spy = jest.fn() |
414 |
| - const c2Spy = jest.fn() |
415 |
| - |
416 |
| - const src = ref(0) |
417 |
| - const c1 = computed(() => { |
418 |
| - c1Spy() |
419 |
| - return src.value % 2 |
420 |
| - }) |
421 |
| - const c2 = computed(() => { |
422 |
| - c2Spy() |
423 |
| - return c1.value + 1 |
424 |
| - }) |
425 |
| - |
426 |
| - effect(() => { |
427 |
| - effectSpy(c2.value) |
428 |
| - }) |
429 |
| - |
430 |
| - expect(effectSpy).toHaveBeenCalledTimes(1) |
431 |
| - expect(effectSpy).toHaveBeenCalledWith(1) |
432 |
| - expect(c2.value).toBe(1) |
433 |
| - |
434 |
| - expect(c1Spy).toHaveBeenCalledTimes(1) |
435 |
| - expect(c2Spy).toHaveBeenCalledTimes(1) |
436 |
| - |
437 |
| - src.value = 1 |
438 |
| - // value should be available sync |
439 |
| - expect(c2.value).toBe(2) |
440 |
| - expect(c2Spy).toHaveBeenCalledTimes(2) |
441 |
| - }) |
442 |
| - |
443 |
| - test('sync access of invalidated chained computed should not prevent final effect from running', async () => { |
444 |
| - const effectSpy = jest.fn() |
445 |
| - const c1Spy = jest.fn() |
446 |
| - const c2Spy = jest.fn() |
447 |
| - |
448 |
| - const src = ref(0) |
449 |
| - const c1 = computed(() => { |
450 |
| - c1Spy() |
451 |
| - return src.value % 2 |
452 |
| - }) |
453 |
| - const c2 = computed(() => { |
454 |
| - c2Spy() |
455 |
| - return c1.value + 1 |
456 |
| - }) |
457 |
| - |
458 |
| - effect(() => { |
459 |
| - effectSpy(c2.value) |
460 |
| - }) |
461 |
| - expect(effectSpy).toHaveBeenCalledTimes(1) |
462 |
| - |
463 |
| - src.value = 1 |
464 |
| - // sync access c2 |
465 |
| - c2.value |
466 |
| - await tick |
467 |
| - expect(effectSpy).toHaveBeenCalledTimes(2) |
468 |
| - }) |
469 |
| - |
470 |
| - test('should not compute if deactivated before scheduler is called', async () => { |
471 |
| - const c1Spy = jest.fn() |
472 |
| - const src = ref(0) |
473 |
| - const c1 = computed(() => { |
474 |
| - c1Spy() |
475 |
| - return src.value % 2 |
476 |
| - }) |
477 |
| - effect(() => c1.value) |
478 |
| - expect(c1Spy).toHaveBeenCalledTimes(1) |
479 |
| - |
480 |
| - // schedule stop |
481 |
| - schedule(() => { |
482 |
| - c1.effect.stop() |
483 |
| - }) |
484 |
| - // trigger |
485 |
| - src.value++ |
486 |
| - await tick |
487 |
| - expect(c1Spy).toHaveBeenCalledTimes(1) |
488 |
| - }) |
489 |
| - }) |
490 | 275 | })
|
0 commit comments