Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createCacheSubscriptionRegistry.test.ts

40 lines1.2 KB

1import { describe, expect, it } from 'vitest'
2import { createRoot, createSignal } from 'solid-js'
3import { createCacheSubscriptionRegistry } from '../createCacheSubscriptionRegistry'
4​
5describe('createCacheSubscriptionRegistry', () => {
6 it('should skip disposed subscriptions without dropping other queued updates', () => {
7 const registry = createCacheSubscriptionRegistry<number, undefined>()
8 const queued: Array<() => void> = []
9 const [firstValue, setFirstValue] = createSignal(0)
10 const [secondValue, setSecondValue] = createSignal(0)
11 const firstDispose = createRoot((dispose) => {
12 registry.register((cache) => cache(), setFirstValue)
13 return dispose
14 })
15 const secondDispose = createRoot((dispose) => {
16 registry.register((cache) => cache(), setSecondValue)
17 return dispose
18 })
19​
20 try {
21 registry.notify(
22 () => 1,
23 undefined,
24 (update) => queued.push(update),
25 )
26 firstDispose()
27 queued.forEach((update) => update())
28​
29 expect(firstValue()).toBe(0)
30 expect(secondValue()).toBe(1)
31​
32 registry.notify(() => 2, undefined)
33 expect(firstValue()).toBe(0)
34 expect(secondValue()).toBe(2)
35 } finally {
36 firstDispose()
37 secondDispose()
38 }
39 })
40})