|
| 1 | +import {afterEach, describe, expect, it, mock} from 'bun:test'; |
| 2 | +import {createClassyStore} from '../../core/core'; |
| 3 | +import {devtools} from './devtools'; |
| 4 | + |
| 5 | +/** Flush the queueMicrotask-based batching. */ |
| 6 | +const tick = () => new Promise<void>((resolve) => setTimeout(resolve, 0)); |
| 7 | + |
| 8 | +// ── Mock DevTools Extension ────────────────────────────────────────────────── |
| 9 | + |
| 10 | +type MockConnection = { |
| 11 | + init: ReturnType<typeof mock>; |
| 12 | + send: ReturnType<typeof mock>; |
| 13 | + subscribe: ReturnType<typeof mock>; |
| 14 | + _listener: ((message: unknown) => void) | null; |
| 15 | +}; |
| 16 | + |
| 17 | +function createMockConnection(): MockConnection { |
| 18 | + const conn: MockConnection = { |
| 19 | + init: mock(() => {}), |
| 20 | + send: mock(() => {}), |
| 21 | + subscribe: mock((listener: (message: unknown) => void) => { |
| 22 | + conn._listener = listener; |
| 23 | + return () => { |
| 24 | + conn._listener = null; |
| 25 | + }; |
| 26 | + }), |
| 27 | + _listener: null, |
| 28 | + }; |
| 29 | + return conn; |
| 30 | +} |
| 31 | + |
| 32 | +function createMockExtension(conn: MockConnection) { |
| 33 | + return { |
| 34 | + connect: mock(() => conn), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +/** Helper to set the mock extension on window. */ |
| 39 | +function setExtension(ext: ReturnType<typeof createMockExtension>) { |
| 40 | + (window as unknown as Record<string, unknown>).__REDUX_DEVTOOLS_EXTENSION__ = |
| 41 | + ext; |
| 42 | +} |
| 43 | + |
| 44 | +/** Helper to delete the extension from window. */ |
| 45 | +function deleteExtension() { |
| 46 | + if (typeof window !== 'undefined') { |
| 47 | + delete (window as unknown as Record<string, unknown>) |
| 48 | + .__REDUX_DEVTOOLS_EXTENSION__; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// ── Tests ──────────────────────────────────────────────────────────────────── |
| 53 | + |
| 54 | +describe('devtools', () => { |
| 55 | + afterEach(() => { |
| 56 | + deleteExtension(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns a noop if __REDUX_DEVTOOLS_EXTENSION__ is not available', () => { |
| 60 | + deleteExtension(); |
| 61 | + |
| 62 | + class Store { |
| 63 | + count = 0; |
| 64 | + } |
| 65 | + |
| 66 | + const store = createClassyStore(new Store()); |
| 67 | + const dispose = devtools(store); |
| 68 | + |
| 69 | + // Should not throw and return a function |
| 70 | + expect(typeof dispose).toBe('function'); |
| 71 | + dispose(); // noop |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns a noop when enabled is false', () => { |
| 75 | + const conn = createMockConnection(); |
| 76 | + const ext = createMockExtension(conn); |
| 77 | + setExtension(ext); |
| 78 | + |
| 79 | + class Store { |
| 80 | + count = 0; |
| 81 | + } |
| 82 | + |
| 83 | + const store = createClassyStore(new Store()); |
| 84 | + const dispose = devtools(store, {enabled: false}); |
| 85 | + |
| 86 | + expect(ext.connect).not.toHaveBeenCalled(); |
| 87 | + dispose(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('connects with a custom name and sends init state', () => { |
| 91 | + const conn = createMockConnection(); |
| 92 | + const ext = createMockExtension(conn); |
| 93 | + setExtension(ext); |
| 94 | + |
| 95 | + class Store { |
| 96 | + count = 0; |
| 97 | + } |
| 98 | + |
| 99 | + const store = createClassyStore(new Store()); |
| 100 | + devtools(store, {name: 'MyStore'}); |
| 101 | + |
| 102 | + expect(ext.connect).toHaveBeenCalledWith({name: 'MyStore'}); |
| 103 | + expect(conn.init).toHaveBeenCalledTimes(1); |
| 104 | + |
| 105 | + // Init should receive a snapshot of the store |
| 106 | + const initState = conn.init.mock.calls[0][0] as Record<string, unknown>; |
| 107 | + expect(initState.count).toBe(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it('sends state updates to DevTools on store mutation', async () => { |
| 111 | + const conn = createMockConnection(); |
| 112 | + const ext = createMockExtension(conn); |
| 113 | + setExtension(ext); |
| 114 | + |
| 115 | + class Store { |
| 116 | + count = 0; |
| 117 | + } |
| 118 | + |
| 119 | + const store = createClassyStore(new Store()); |
| 120 | + devtools(store); |
| 121 | + |
| 122 | + store.count = 5; |
| 123 | + await tick(); |
| 124 | + |
| 125 | + expect(conn.send).toHaveBeenCalledTimes(1); |
| 126 | + const [action, state] = conn.send.mock.calls[0] as [ |
| 127 | + {type: string}, |
| 128 | + Record<string, unknown>, |
| 129 | + ]; |
| 130 | + expect(action.type).toBe('STORE_UPDATE'); |
| 131 | + expect(state.count).toBe(5); |
| 132 | + }); |
| 133 | + |
| 134 | + it('handles JUMP_TO_STATE time-travel', async () => { |
| 135 | + const conn = createMockConnection(); |
| 136 | + const ext = createMockExtension(conn); |
| 137 | + setExtension(ext); |
| 138 | + |
| 139 | + class Store { |
| 140 | + count = 0; |
| 141 | + name = 'hello'; |
| 142 | + } |
| 143 | + |
| 144 | + const store = createClassyStore(new Store()); |
| 145 | + devtools(store); |
| 146 | + |
| 147 | + store.count = 10; |
| 148 | + store.name = 'world'; |
| 149 | + await tick(); |
| 150 | + |
| 151 | + // Simulate time-travel back to initial state |
| 152 | + conn._listener?.({ |
| 153 | + type: 'DISPATCH', |
| 154 | + payload: {type: 'JUMP_TO_STATE'}, |
| 155 | + state: JSON.stringify({count: 0, name: 'hello'}), |
| 156 | + }); |
| 157 | + await tick(); |
| 158 | + |
| 159 | + expect(store.count).toBe(0); |
| 160 | + expect(store.name).toBe('hello'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('skips getters during time-travel restore', async () => { |
| 164 | + const conn = createMockConnection(); |
| 165 | + const ext = createMockExtension(conn); |
| 166 | + setExtension(ext); |
| 167 | + |
| 168 | + class Store { |
| 169 | + count = 5; |
| 170 | + |
| 171 | + get doubled() { |
| 172 | + return this.count * 2; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + const store = createClassyStore(new Store()); |
| 177 | + devtools(store); |
| 178 | + |
| 179 | + // Simulate time-travel with a getter key included |
| 180 | + conn._listener?.({ |
| 181 | + type: 'DISPATCH', |
| 182 | + payload: {type: 'JUMP_TO_STATE'}, |
| 183 | + state: JSON.stringify({count: 3, doubled: 999}), |
| 184 | + }); |
| 185 | + await tick(); |
| 186 | + |
| 187 | + expect(store.count).toBe(3); |
| 188 | + // Getter should recompute, not be overwritten |
| 189 | + expect(store.doubled).toBe(6); |
| 190 | + }); |
| 191 | + |
| 192 | + it('disposes correctly (unsubscribes from store and devtools)', async () => { |
| 193 | + const conn = createMockConnection(); |
| 194 | + const ext = createMockExtension(conn); |
| 195 | + setExtension(ext); |
| 196 | + |
| 197 | + class Store { |
| 198 | + count = 0; |
| 199 | + } |
| 200 | + |
| 201 | + const store = createClassyStore(new Store()); |
| 202 | + const dispose = devtools(store); |
| 203 | + |
| 204 | + store.count = 1; |
| 205 | + await tick(); |
| 206 | + expect(conn.send).toHaveBeenCalledTimes(1); |
| 207 | + |
| 208 | + dispose(); |
| 209 | + |
| 210 | + store.count = 2; |
| 211 | + await tick(); |
| 212 | + // No additional send after dispose |
| 213 | + expect(conn.send).toHaveBeenCalledTimes(1); |
| 214 | + // DevTools listener should be removed |
| 215 | + expect(conn._listener).toBeNull(); |
| 216 | + }); |
| 217 | +}); |
0 commit comments