From b49f163caed2daff75a80a23df8e79c7d75cec79 Mon Sep 17 00:00:00 2001 From: Randy Coulman Date: Thu, 11 May 2017 16:43:57 -0700 Subject: [PATCH 1/2] Update to latest version of node 6 --- .nvmrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nvmrc b/.nvmrc index dc3829f..3c02432 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -6.9.1 +6.10.3 From d9e2abc17c9f97104bcfdb3edb0e868d3a74a256 Mon Sep 17 00:00:00 2001 From: Randy Coulman Date: Thu, 11 May 2017 17:08:07 -0700 Subject: [PATCH 2/2] Allow bypassing FSA check for some actions Some libraries that dispatch actions through our reducers do not conform to the flux-standard-action spec, but `createReducer` will throw a `NonStandardAction` when encountering those actions. This PR adds allows `createReducer` to take an `allowNonStandardActionIf` predicate that is applied to each action. If the predicate returns true, we no longer check to see if it conforms to the FSA spec. --- src/__tests__/createReducer-spec.js | 23 +++++++++++++++++++++++ src/createReducer.js | 10 +++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/__tests__/createReducer-spec.js b/src/__tests__/createReducer-spec.js index 4903852..7bde81d 100644 --- a/src/__tests__/createReducer-spec.js +++ b/src/__tests__/createReducer-spec.js @@ -33,4 +33,27 @@ describe('createReducer', () => { () => reducer(state, invalidAction) ).toThrowError(NonStandardAction) }) + + describe('whitelisting non-flux standard actions', () => { + const reducerWithWhitelist = createReducer({}, {}, { + allowNonStandardActionIf: action => action.type === 'WHITELISTED' + }) + const state = reducerWithWhitelist(undefined, nullAction) + + test('does not complain about whitelisted actions', () => { + const whitelistedAction = { type: 'WHITELISTED', notPayload: {} } + + expect( + () => reducerWithWhitelist(state, whitelistedAction) + ).not.toThrowError(NonStandardAction) + }) + + test('continues to complain about non-whitelisted actions', () => { + const invalidAction = { type: 'NOT WHITELISTED', notPayload: {} } + + expect( + () => reducer(state, invalidAction) + ).toThrowError(NonStandardAction) + }) + }) }) diff --git a/src/createReducer.js b/src/createReducer.js index 72fa969..e20d4d2 100644 --- a/src/createReducer.js +++ b/src/createReducer.js @@ -1,5 +1,5 @@ import { isFSA } from 'flux-standard-action' -import { identity, propOr } from 'ramda' +import { always, identity, propOr } from 'ramda' export class NonStandardAction { constructor(action) { @@ -11,8 +11,8 @@ export class NonStandardAction { } } -const throwIfNotFSA = action => { - if (isFSA(action)) return +const throwIfNotFSA = (action, isWhitelisted = always(false)) => { + if (isWhitelisted(action) || isFSA(action)) return throw new NonStandardAction(action) } @@ -21,9 +21,9 @@ const throwIfNotFSA = action => { const isProduction = process.env.NODE_ENV === 'production' const ensureIsFSA = isProduction ? identity : throwIfNotFSA -export default function createReducer(initialState, handlers) { +export default function createReducer(initialState, handlers, options = {}) { return (state = initialState, action) => { - ensureIsFSA(action) + ensureIsFSA(action, options.allowNonStandardActionIf) return propOr(identity, action.type, handlers)(state, action) }