diff --git a/.travis.yml b/.travis.yml index c5c2b2b..bd75318 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,6 @@ language: node_js node_js: - node - - 14 - - 13 - - 12 cache: yarn script: diff --git a/packages/redux-pauseable-store/tests/dispatch.ts b/packages/redux-pauseable-store/tests/dispatch.ts new file mode 100644 index 0000000..c07c8b3 --- /dev/null +++ b/packages/redux-pauseable-store/tests/dispatch.ts @@ -0,0 +1,114 @@ +/* eslint-env jest */ +// import '@testing-library/jest-dom/extend-expect'; +import { Store } from 'redux'; + +import { createDevHelperStore, incrementAction } from 'react-hibernate-dev-helpers'; + +import { createPauseableStore } from '../src'; + +describe('dispatch', () => { + let rootStore: Store; + beforeEach(() => { + rootStore = createDevHelperStore(); + }); + + it('dispatches when allowed', () => { + const myStore = createPauseableStore(rootStore, { canDispatch: true }); + + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 1, + }); + + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 2, + }); + }); + + it('does not dispatch when not allowed', () => { + const myStore = createPauseableStore(rootStore, { canDispatch: false }); + + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 0, + }); + + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 0, + }); + }); + + it('warns on dispatch when paused, by default', () => { + const myStore = createPauseableStore(rootStore, { isPaused: true }); + const consoleWarnSpy = jest.spyOn(global.console, 'warn').mockImplementationOnce(() => null); + + myStore.dispatch(incrementAction()); + + expect(console.warn).toHaveBeenCalledTimes(1); + expect( + console.warn, + ).toHaveBeenLastCalledWith( + 'Something is trying to dispatch an action to a paused PauseableStore. Set `canDispatch` to true or false to disable this warning.', + { action: incrementAction(), pauseableStore: myStore }, + ); + consoleWarnSpy.mockRestore(); + + // but the dispatch still happened + expect(rootStore.getState()).toEqual({ + count: 1, + }); + }); + + it('disables and then enables dispatch', () => { + const myStore = createPauseableStore(rootStore); + + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 1, + }); + + myStore.setDispatch(false); + + myStore.dispatch(incrementAction()); + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 1, + }); + + myStore.setDispatch(true); + + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 2, + }); + }); + + it('enables and then re-disables dispatch', () => { + const myStore = createPauseableStore(rootStore, { + canDispatch: false, + }); + + myStore.dispatch(incrementAction()); + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 0, + }); + + myStore.setDispatch(true); + + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 1, + }); + + myStore.setDispatch(false); + + myStore.dispatch(incrementAction()); + myStore.dispatch(incrementAction()); + expect(rootStore.getState()).toEqual({ + count: 1, + }); + }); +});