import type { Store } from './createStore' import { createStore } from './createStore' import reducer from './reducer' import { INITIAL_STATE } from '.' jest.mock('./reducer') describe('createStore', () => { let store: Store beforeEach(() => { store = createStore() }) it('should notify when state changes', () => { const listener = jest.fn() store.subscribe(listener) jest.mocked(reducer).mockReturnValue({ ...INITIAL_STATE }) store.dispatch({ type: 'updateSelection', payload: new Set(['1', '2']), }) expect(listener).toHaveBeenCalledTimes(1) }) it('should not notify when state changes if unsubscribed', () => { const listener = jest.fn() const unsubscribe = store.subscribe(listener) unsubscribe() jest.mocked(reducer).mockReturnValue({ ...INITIAL_STATE }) store.dispatch({ type: 'updateSelection', payload: new Set(['1', '2']), }) expect(listener).not.toHaveBeenCalled() }) it('should not notify if state does not change', () => { const listener = jest.fn() store.subscribe(listener) jest.mocked(reducer).mockReturnValue(store.getState()) store.dispatch({ type: 'updateSelection', payload: new Set(['1', '2']), }) expect(listener).not.toHaveBeenCalled() }) it('should return state when getState is called', () => { expect(store.getState()).toEqual(INITIAL_STATE) }) })