1 | import brcast from './index'
|
2 |
|
3 | test('default export is a function', () => {
|
4 | expect(typeof brcast).toBe('function')
|
5 | })
|
6 |
|
7 | test('exposes the public API', () => {
|
8 | const broadcast = brcast()
|
9 | const methods = Object.keys(broadcast)
|
10 |
|
11 | expect(methods.length).toBe(4)
|
12 | expect(methods).toContain('subscribe')
|
13 | expect(methods).toContain('unsubscribe')
|
14 | expect(methods).toContain('getState')
|
15 | expect(methods).toContain('setState')
|
16 | })
|
17 |
|
18 | test('throws if listener is not a function', () => {
|
19 | const broadcast = brcast()
|
20 | expect(() => broadcast.subscribe()).toThrow()
|
21 | expect(() => broadcast.subscribe('throw')).toThrow()
|
22 | expect(() => broadcast.subscribe({})).toThrow()
|
23 | expect(() => broadcast.subscribe(() => {})).not.toThrow()
|
24 | })
|
25 |
|
26 | test('is able to start with an undefined state and update it accordingly', () => {
|
27 | const broadcast = brcast()
|
28 | expect(broadcast.getState()).toBeUndefined()
|
29 | broadcast.setState(2)
|
30 | expect(broadcast.getState()).toBe(2)
|
31 | })
|
32 |
|
33 | test('it updates the state', () => {
|
34 | const handler = jest.fn()
|
35 | const broadcast = brcast()
|
36 | broadcast.subscribe(handler)
|
37 | broadcast.setState(2)
|
38 | expect(handler.mock.calls.length).toBe(1)
|
39 | expect(handler.mock.calls[0][0]).toBe(2)
|
40 | })
|
41 |
|
42 | test('it unsubscribes only relevant listeners', () => {
|
43 | const handler = jest.fn()
|
44 | const handler1 = jest.fn()
|
45 | const broadcast = brcast(1)
|
46 | const subscriptionId = broadcast.subscribe(handler)
|
47 | broadcast.subscribe(handler1)
|
48 | broadcast.unsubscribe(subscriptionId)
|
49 | broadcast.setState(2)
|
50 | broadcast.setState(3)
|
51 | expect(handler.mock.calls.length).toBe(0)
|
52 | expect(handler1.mock.calls.length).toBe(2)
|
53 | })
|
54 |
|
55 | test('removes listeners only once when unsubscribing more than once', () => {
|
56 | const handler = jest.fn()
|
57 | const broadcast = brcast(1)
|
58 | const subscriptionId = broadcast.subscribe(handler)
|
59 |
|
60 | broadcast.unsubscribe(subscriptionId)
|
61 | broadcast.unsubscribe(subscriptionId)
|
62 | broadcast.setState(2)
|
63 | expect(handler.mock.calls.length).toBe(0)
|
64 | })
|
65 |
|
66 | test('supports removing a subscription within a subscription', () => {
|
67 | const broadcast = brcast(1)
|
68 | const handler = jest.fn()
|
69 | const handler1 = jest.fn()
|
70 | const handler2 = jest.fn()
|
71 |
|
72 | broadcast.subscribe(handler)
|
73 | const sub1Id = broadcast.subscribe(() => {
|
74 | handler1()
|
75 | broadcast.unsubscribe(sub1Id)
|
76 | })
|
77 | broadcast.subscribe(handler2)
|
78 |
|
79 | broadcast.setState(2)
|
80 | broadcast.setState(3)
|
81 | expect(handler.mock.calls.length).toBe(2)
|
82 | expect(handler1.mock.calls.length).toBe(1)
|
83 | expect(handler2.mock.calls.length).toBe(2)
|
84 | })
|
85 |
|
86 | test('do not notify subscribers getting unsubscribed in the middle of a setState', () => {
|
87 | const broadcast = brcast()
|
88 |
|
89 | const unsubscribeIds = []
|
90 | const doUnsubscribeAll = () =>
|
91 | unsubscribeIds.forEach(id => broadcast.unsubscribe(id))
|
92 |
|
93 | const handler = jest.fn()
|
94 | const handler1 = jest.fn()
|
95 | const handler2 = jest.fn()
|
96 |
|
97 | unsubscribeIds.push(broadcast.subscribe(handler))
|
98 | unsubscribeIds.push(
|
99 | broadcast.subscribe(() => {
|
100 | handler1()
|
101 | doUnsubscribeAll()
|
102 | })
|
103 | )
|
104 | unsubscribeIds.push(broadcast.subscribe(handler2))
|
105 |
|
106 | broadcast.setState(2)
|
107 | expect(handler.mock.calls.length).toBe(1)
|
108 | expect(handler1.mock.calls.length).toBe(1)
|
109 | expect(handler2.mock.calls.length).toBe(0)
|
110 |
|
111 | broadcast.setState(3)
|
112 | expect(handler.mock.calls.length).toBe(1)
|
113 | expect(handler1.mock.calls.length).toBe(1)
|
114 | expect(handler2.mock.calls.length).toBe(0)
|
115 | })
|