|
| 1 | +import type { Draft } from 'immer'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | + |
| 4 | +import { BaseController } from './BaseControllerV2'; |
| 5 | + |
| 6 | +interface MockControllerState { |
| 7 | + count: number; |
| 8 | +} |
| 9 | + |
| 10 | +class MockController extends BaseController<MockControllerState> { |
| 11 | + update(callback: (state: Draft<MockControllerState>) => void | MockControllerState) { |
| 12 | + super.update(callback); |
| 13 | + } |
| 14 | + |
| 15 | + destroy() { |
| 16 | + super.destroy(); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe('BaseController', () => { |
| 21 | + it('should set initial state', () => { |
| 22 | + const controller = new MockController({ count: 0 }); |
| 23 | + |
| 24 | + expect(controller.state).toEqual({ count: 0 }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should not allow mutating state directly', () => { |
| 28 | + const controller = new MockController({ count: 0 }); |
| 29 | + |
| 30 | + expect(() => { |
| 31 | + controller.state = { count: 1 }; |
| 32 | + }).toThrow(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should allow updating state by modifying draft', () => { |
| 36 | + const controller = new MockController({ count: 0 }); |
| 37 | + |
| 38 | + controller.update((draft) => { |
| 39 | + draft.count += 1; |
| 40 | + }); |
| 41 | + |
| 42 | + expect(controller.state).toEqual({ count: 1 }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should allow updating state by return a value', () => { |
| 46 | + const controller = new MockController({ count: 0 }); |
| 47 | + |
| 48 | + controller.update(() => { |
| 49 | + return { count: 1 }; |
| 50 | + }); |
| 51 | + |
| 52 | + expect(controller.state).toEqual({ count: 1 }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should throw an error if update callback modifies draft and returns value', () => { |
| 56 | + const controller = new MockController({ count: 0 }); |
| 57 | + |
| 58 | + expect(() => { |
| 59 | + controller.update((draft) => { |
| 60 | + draft.count += 1; |
| 61 | + return { count: 10 }; |
| 62 | + }); |
| 63 | + }).toThrow(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should inform subscribers of state changes', () => { |
| 67 | + const controller = new MockController({ count: 0 }); |
| 68 | + const listener1 = sinon.stub(); |
| 69 | + const listener2 = sinon.stub(); |
| 70 | + |
| 71 | + controller.subscribe(listener1); |
| 72 | + controller.subscribe(listener2); |
| 73 | + controller.update(() => { |
| 74 | + return { count: 1 }; |
| 75 | + }); |
| 76 | + |
| 77 | + expect(listener1.callCount).toEqual(1); |
| 78 | + expect(listener1.firstCall.args).toEqual([{ count: 1 }]); |
| 79 | + expect(listener2.callCount).toEqual(1); |
| 80 | + expect(listener2.firstCall.args).toEqual([{ count: 1 }]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should inform a subscriber of each state change once even after multiple subscriptions', () => { |
| 84 | + const controller = new MockController({ count: 0 }); |
| 85 | + const listener1 = sinon.stub(); |
| 86 | + |
| 87 | + controller.subscribe(listener1); |
| 88 | + controller.subscribe(listener1); |
| 89 | + controller.update(() => { |
| 90 | + return { count: 1 }; |
| 91 | + }); |
| 92 | + |
| 93 | + expect(listener1.callCount).toEqual(1); |
| 94 | + expect(listener1.firstCall.args).toEqual([{ count: 1 }]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should no longer inform a subscriber about state changes after unsubscribing', () => { |
| 98 | + const controller = new MockController({ count: 0 }); |
| 99 | + const listener1 = sinon.stub(); |
| 100 | + |
| 101 | + controller.subscribe(listener1); |
| 102 | + controller.unsubscribe(listener1); |
| 103 | + controller.update(() => { |
| 104 | + return { count: 1 }; |
| 105 | + }); |
| 106 | + |
| 107 | + expect(listener1.callCount).toEqual(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should no longer inform a subscriber about state changes after unsubscribing once, even if they subscribed many times', () => { |
| 111 | + const controller = new MockController({ count: 0 }); |
| 112 | + const listener1 = sinon.stub(); |
| 113 | + |
| 114 | + controller.subscribe(listener1); |
| 115 | + controller.subscribe(listener1); |
| 116 | + controller.unsubscribe(listener1); |
| 117 | + controller.update(() => { |
| 118 | + return { count: 1 }; |
| 119 | + }); |
| 120 | + |
| 121 | + expect(listener1.callCount).toEqual(0); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should allow unsubscribing listeners who were never subscribed', () => { |
| 125 | + const controller = new MockController({ count: 0 }); |
| 126 | + const listener1 = sinon.stub(); |
| 127 | + |
| 128 | + expect(() => { |
| 129 | + controller.unsubscribe(listener1); |
| 130 | + }).not.toThrow(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should no longer update subscribers after being destroyed', () => { |
| 134 | + const controller = new MockController({ count: 0 }); |
| 135 | + const listener1 = sinon.stub(); |
| 136 | + const listener2 = sinon.stub(); |
| 137 | + |
| 138 | + controller.subscribe(listener1); |
| 139 | + controller.subscribe(listener2); |
| 140 | + controller.destroy(); |
| 141 | + controller.update(() => { |
| 142 | + return { count: 1 }; |
| 143 | + }); |
| 144 | + |
| 145 | + expect(listener1.callCount).toEqual(0); |
| 146 | + expect(listener2.callCount).toEqual(0); |
| 147 | + }); |
| 148 | +}); |
0 commit comments