Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { createDelayedActivationController } from '../src/bridge/fields/delayedActivation'
3β
4afterEach(() => {
5 vi.useRealTimers()
6})
7β
8describe('delayed activation', () => {
9 it('batches due keys without restarting repeated observations', async () => {
10 vi.useFakeTimers()
11 const changes: Array<[string, boolean]> = []
12 const controller = createDelayedActivationController<string>({
13 delayMs: 300,
14 canActivate: () => true,
15 onChange: (key, active) => changes.push([key, active]),
16 })
17β
18 controller.observe('parent', true)
19 controller.observe('child', true)
20 expect(vi.getTimerCount()).toBe(1)
21β
22 await vi.advanceTimersByTimeAsync(150)
23 controller.observe('child', true)
24 await vi.advanceTimersByTimeAsync(149)
25 expect(changes).toEqual([])
26β
27 await vi.advanceTimersByTimeAsync(1)
28 expect(changes).toEqual([
29 ['parent', true],
30 ['child', true],
31 ])
32β
33 controller.observe('child', false)
34 expect(changes.at(-1)).toEqual(['child', false])
35 expect(controller.isActive('child')).toBe(false)
36 controller.dispose()
37 })
38β
39 it('silently cancels pending keys during lifecycle cleanup', async () => {
40 vi.useFakeTimers()
41 const changes: Array<[string, boolean]> = []
42 const controller = createDelayedActivationController<string>({
43 delayMs: 300,
44 canActivate: (key) => key !== 'ineligible',
45 onChange: (key, active) => changes.push([key, active]),
46 })
47β
48 controller.observe('removed', true)
49 controller.remove('removed')
50 expect(vi.getTimerCount()).toBe(0)
51β
52 controller.observe('form-a', true)
53 controller.observe('form-b', true)
54 controller.removeWhere((key) => key === 'form-a')
55 expect(vi.getTimerCount()).toBe(1)
56β
57 controller.observe('ineligible', true)
58 await vi.advanceTimersByTimeAsync(300)
59 expect(changes).toEqual([['form-b', true]])
60β
61 controller.observe('pending', true)
62 expect(vi.getTimerCount()).toBe(1)
63 controller.dispose()
64 expect(vi.getTimerCount()).toBe(0)
65 expect(changes).toEqual([['form-b', true]])
66 })
67})