|
| 1 | +import { applyMiddleware, createStore } from 'redux'; |
| 2 | + |
| 3 | +import { actionBuffer } from './actionBuffer'; |
| 4 | + |
| 5 | +const trivialReducer = (state = {}, _action: any) => state; |
| 6 | + |
| 7 | +const makeStore = () => createStore(trivialReducer, applyMiddleware(actionBuffer())); |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + (global as any).__reduxActions = undefined; |
| 11 | +}); |
| 12 | + |
| 13 | +describe('actionBuffer middleware', () => { |
| 14 | + it('records the dispatched action type in global.__reduxActions', () => { |
| 15 | + const store = makeStore(); |
| 16 | + store.dispatch({ type: 'TEST_ACTION' }); |
| 17 | + const buf: any[] = (global as any).__reduxActions; |
| 18 | + expect(buf).toBeDefined(); |
| 19 | + expect(buf[buf.length - 1].type).toBe('TEST_ACTION'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('caps the buffer at 100 and drops the oldest entry (FIFO)', () => { |
| 23 | + const store = makeStore(); |
| 24 | + for (let i = 0; i < 110; i++) { |
| 25 | + store.dispatch({ type: `ACTION_${i}` }); |
| 26 | + } |
| 27 | + const buf: any[] = (global as any).__reduxActions; |
| 28 | + expect(buf.length).toBe(100); |
| 29 | + expect(buf[0].type).toBe('ACTION_10'); |
| 30 | + expect(buf[99].type).toBe('ACTION_109'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('writes nothing to the console', () => { |
| 34 | + const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 35 | + const infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {}); |
| 36 | + const groupSpy = jest.spyOn(console, 'group').mockImplementation(() => {}); |
| 37 | + const groupEndSpy = jest.spyOn(console, 'groupEnd').mockImplementation(() => {}); |
| 38 | + |
| 39 | + const store = makeStore(); |
| 40 | + store.dispatch({ type: 'SILENT_ACTION' }); |
| 41 | + |
| 42 | + expect(logSpy).not.toHaveBeenCalled(); |
| 43 | + expect(infoSpy).not.toHaveBeenCalled(); |
| 44 | + expect(groupSpy).not.toHaveBeenCalled(); |
| 45 | + expect(groupEndSpy).not.toHaveBeenCalled(); |
| 46 | + |
| 47 | + logSpy.mockRestore(); |
| 48 | + infoSpy.mockRestore(); |
| 49 | + groupSpy.mockRestore(); |
| 50 | + groupEndSpy.mockRestore(); |
| 51 | + }); |
| 52 | +}); |
0 commit comments