Open History to see the last commit
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 }
17}
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})