diff --git a/README.md b/README.md index d5f0006..9b058e7 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ The `options` object passed to these action creators may contain the following a The `redux-sessions` enhancer is what allows the session state to persist across page refreshes. It can receive the following options: - `persist (default=true)`: A flag indicating whether or not to persist session state. +- `debounce (default=true)`: A flag indicating whether or not to debounce writing to `localStorage`. +- `debounceInTestMode (default=false)`: A flag indicating whether or not to debounce writing to `localStorage` when `NODE_ENV === 'test'`. - `debounceInterval (default=500)`: The debounce interval used when writing session state to `localStorage` (ms). ### Selectors diff --git a/package.json b/package.json index d6db49f..f939806 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redux-sessions", - "version": "1.0.1", + "version": "1.1.0", "description": "Redux action creators for storing session state", "main": "lib/index.js", "scripts": { diff --git a/src/enhancer.js b/src/enhancer.js index 1d831b6..a1d09bf 100644 --- a/src/enhancer.js +++ b/src/enhancer.js @@ -1,10 +1,16 @@ import { saveSessionState } from './persistenceHelpers' import { throttle } from 'lodash' +import { isTestMode } from './utils' const DEFAULT_DEBOUNCE_INTERVAL = 500 // Adds store subscription that persists session state in local storage -function enhancer ({ persist=true, debounceInterval=DEFAULT_DEBOUNCE_INTERVAL }={}) { +function enhancer ({ + persist=true, + debounce=true, + debounceInTestMode=false, + debounceInterval=DEFAULT_DEBOUNCE_INTERVAL, +}={}) { return function enhance (createStore) { return function newCreateStore (...args) { const store = createStore(...args) @@ -15,7 +21,12 @@ function enhancer ({ persist=true, debounceInterval=DEFAULT_DEBOUNCE_INTERVAL }= if (!state.sessions) throw new Error('redux-sessions: error when attempting to save state. Did you remember to attach the reducer at key `sessions`?') return saveSessionState(state.sessions) } - store.subscribe(throttle(persistState, debounceInterval)) + // Don't debounce in test mode, unless otherwise specified + const doDebounce = isTestMode() + ? debounceInTestMode + : debounce + const subscription = doDebounce ? throttle(persistState, debounceInterval) : persistState + store.subscribe(subscription) return store } } diff --git a/src/utils/index.js b/src/utils/index.js index ab506b6..88c11c4 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1 +1,2 @@ +export isTestMode from './isTestMode' export * as storage from './storage' \ No newline at end of file diff --git a/src/utils/isTestMode.js b/src/utils/isTestMode.js new file mode 100644 index 0000000..5c59c78 --- /dev/null +++ b/src/utils/isTestMode.js @@ -0,0 +1,13 @@ +import { get } from 'lodash/fp' + +function getEnv () { + return (typeof process === 'undefined') + ? null + : get('env.NODE_ENV', process) +} + +function isTestMode () { + return getEnv() === 'test' +} + +export default isTestMode \ No newline at end of file diff --git a/test/enhancer.test.js b/test/enhancer.test.js index 41598f2..31e688a 100644 --- a/test/enhancer.test.js +++ b/test/enhancer.test.js @@ -1,6 +1,4 @@ import { enhancer } from '../src' -// Mock debounce: https://github.com/facebook/jest/issues/3465 -jest.mock('lodash/debounce', () => func => func) function mockCreateStore (initialState) { const mockStore = {}