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

subscribable.test.tsx

100 lines3.0 KB

1import { describe, expect, it, vi } from 'vitest'
2import { Subscribable } from '../subscribable'
3​
4type Listener = () => void
5​
6class SubscribableTest extends Subscribable<Listener> {
7 onSubscribeSpy = vi.fn()
8 onUnsubscribeSpy = vi.fn()
9​
10 protected override onSubscribe(): void {
11 this.onSubscribeSpy()
12 }
13​
14 protected override onUnsubscribe(): void {
15 this.onUnsubscribeSpy()
16 }
18​
19describe('Subscribable', () => {
20 it('should call onSubscribe when a listener subscribes', () => {
21 const subscribable = new SubscribableTest()
22​
23 subscribable.subscribe(() => undefined)
24 expect(subscribable.onSubscribeSpy).toHaveBeenCalledTimes(1)
25 expect(subscribable.onUnsubscribeSpy).toHaveBeenCalledTimes(0)
26 })
27​
28 it('should call onSubscribe once per subscribe call', () => {
29 const subscribable = new SubscribableTest()
30​
31 subscribable.subscribe(() => undefined)
32 subscribable.subscribe(() => undefined)
33 expect(subscribable.onSubscribeSpy).toHaveBeenCalledTimes(2)
34 })
35​
36 it('should call onUnsubscribe when a listener unsubscribes', () => {
37 const subscribable = new SubscribableTest()
38​
39 const unsubscribe = subscribable.subscribe(() => undefined)
40 unsubscribe()
41 expect(subscribable.onUnsubscribeSpy).toHaveBeenCalledTimes(1)
42 })
43​
44 it('should return `false` from hasListeners when there are no listeners', () => {
45 const subscribable = new SubscribableTest()
46​
47 expect(subscribable.hasListeners()).toBe(false)
48 })
49​
50 it('should return `true` from hasListeners while a listener is subscribed', () => {
51 const subscribable = new SubscribableTest()
52​
53 subscribable.subscribe(() => undefined)
54 expect(subscribable.hasListeners()).toBe(true)
55 })
56​
57 it('should return `false` from hasListeners after the last listener unsubscribes', () => {
58 const subscribable = new SubscribableTest()
59​
60 const unsubscribe = subscribable.subscribe(() => undefined)
61 unsubscribe()
62 expect(subscribable.hasListeners()).toBe(false)
63 })
64​
65 it('should still have listeners while at least one remains subscribed', () => {
66 const subscribable = new SubscribableTest()
67​
68 const unsubscribe1 = subscribable.subscribe(() => undefined)
69 subscribable.subscribe(() => undefined)
70​
71 unsubscribe1()
72 expect(subscribable.hasListeners()).toBe(true)
73 })
74​
75 it('should deduplicate the same listener reference', () => {
76 const subscribable = new SubscribableTest()
77 const listener = () => undefined
78​
79 subscribable.subscribe(listener)
80 subscribable.subscribe(listener)
81 expect(subscribable.hasListeners()).toBe(true)
82​
83 const unsubscribe = subscribable.subscribe(listener)
84 unsubscribe()
85 expect(subscribable.hasListeners()).toBe(false)
86 })
87​
88 it('should keep a stable subscribe reference when destructured', () => {
89 const subscribable = new SubscribableTest()
90 const { subscribe } = subscribable
91​
92 const unsubscribe = subscribe(() => undefined)
93​
94 expect(subscribable.onSubscribeSpy).toHaveBeenCalledTimes(1)
95 expect(subscribable.hasListeners()).toBe(true)
96​
97 unsubscribe()
98 expect(subscribable.hasListeners()).toBe(false)
99 })
100})