|
| 1 | +import ConditionStore from "ConditionStore"; |
| 2 | + |
1 | 3 | describe('#ConditionStore', () => { |
2 | | - it('xxx', () => { |
3 | | - }); |
| 4 | + it('stores initial values', () => { |
| 5 | + const store1 = new ConditionStore(false); |
| 6 | + expect(store1.getSnapshot()).toBe(false); |
| 7 | + |
| 8 | + const store2 = new ConditionStore(true); |
| 9 | + expect(store2.getSnapshot()).toBe(true); |
4 | 10 | }); |
| 11 | + |
| 12 | + it('updates value on `set`', () => { |
| 13 | + const store = new ConditionStore(false); |
| 14 | + store.set(); |
| 15 | + expect(store.getSnapshot()).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('updates value on `clear`', () => { |
| 19 | + const store = new ConditionStore(true); |
| 20 | + store.clear(); |
| 21 | + expect(store.getSnapshot()).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + it('notifies listeners on `set` or `clear`', () => { |
| 25 | + const listener = jest.fn(); |
| 26 | + |
| 27 | + const store = new ConditionStore(false); |
| 28 | + store.subscribe(listener); |
| 29 | + expect(listener.mock.calls.length).toBe(0); |
| 30 | + |
| 31 | + store.set(); |
| 32 | + expect(listener.mock.calls.length).toBe(1); |
| 33 | + |
| 34 | + store.clear(); |
| 35 | + expect(listener.mock.calls.length).toBe(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('does not notify listeners that have been unsubscribed', () => { |
| 39 | + const listener1 = jest.fn(); |
| 40 | + const listener2 = jest.fn(); |
| 41 | + |
| 42 | + const store = new ConditionStore(false); |
| 43 | + const unsubscribeListener1 = store.subscribe(listener1); |
| 44 | + const unsubscribeListener2 = store.subscribe(listener2); |
| 45 | + expect(listener1.mock.calls.length).toBe(0); |
| 46 | + expect(listener2.mock.calls.length).toBe(0); |
| 47 | + |
| 48 | + store.set(); |
| 49 | + expect(listener1.mock.calls.length).toBe(1); |
| 50 | + expect(listener2.mock.calls.length).toBe(1); |
| 51 | + |
| 52 | + unsubscribeListener2(); |
| 53 | + |
| 54 | + store.clear(); |
| 55 | + expect(listener1.mock.calls.length).toBe(2); |
| 56 | + expect(listener2.mock.calls.length).toBe(1); |
| 57 | + |
| 58 | + unsubscribeListener1(); |
| 59 | + |
| 60 | + store.set(); |
| 61 | + expect(listener1.mock.calls.length).toBe(2); |
| 62 | + expect(listener2.mock.calls.length).toBe(1); |
| 63 | + }); |
| 64 | + |
| 65 | + it('notifies listeners on `set` or `clear` only if value changes', () => { |
| 66 | + const listener = jest.fn(); |
| 67 | + |
| 68 | + const store = new ConditionStore(false); |
| 69 | + store.subscribe(listener); |
| 70 | + expect(listener.mock.calls.length).toBe(0); |
| 71 | + |
| 72 | + store.set(); |
| 73 | + expect(listener.mock.calls.length).toBe(1); |
| 74 | + store.set(); |
| 75 | + expect(listener.mock.calls.length).toBe(1); |
| 76 | + |
| 77 | + store.clear(); |
| 78 | + expect(listener.mock.calls.length).toBe(2); |
| 79 | + store.clear(); |
| 80 | + expect(listener.mock.calls.length).toBe(2); |
| 81 | + }); |
| 82 | +}); |
0 commit comments