Skip to content

Commit

Permalink
Merge pull request #9 from CodingZeal/features/whitelist-actions-for-…
Browse files Browse the repository at this point in the history
…fsa-check

Allow whitelisting actions for flux-standard-action check
  • Loading branch information
randycoulman authored May 12, 2017
2 parents 2b88660 + d9e2abc commit 777acda
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.9.1
6.10.3
23 changes: 23 additions & 0 deletions src/__tests__/createReducer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
10 changes: 5 additions & 5 deletions src/createReducer.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 777acda

Please sign in to comment.