Skip to content

Commit

Permalink
Merge pull request #5 from DNAinfo/improvement/handle-put-arrays
Browse files Browse the repository at this point in the history
Improvement/handle put arrays
  • Loading branch information
timbuckley authored Feb 3, 2017
2 parents 770877e + 4429f7b commit c69b324
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const bool = o => !!o

const isPut = obj => bool(obj && Object.keys(obj).includes('PUT'))

const isNestedPut = arr => bool(
arr &&
arr.every &&
arr.length > 0 &&
arr.every(element => isPut(element))
)

const isNestedArray = arr => bool(
arr &&
arr.every && // Is an array
Expand Down Expand Up @@ -60,7 +67,7 @@ function sagaTestEngine(genFunc, envMapping, ...initialArgs) {
const isFirstLoop = counter === 0
const nextValFound = nextVal !== undefined
const yieldedUndefined = val === undefined
const yieldedEffectIsPut = isPut(val)
const yieldedEffectIsPut = isPut(val) || isNestedPut(val)
assert(
(isFirstLoop || nextValFound || yieldedUndefined || yieldedEffectIsPut),
`Env Mapping is missing a value for ${JSON.stringify(val, null, 2)}`)
Expand All @@ -78,4 +85,4 @@ function sagaTestEngine(genFunc, envMapping, ...initialArgs) {
return puts
}

module.exports = { sagaTestEngine, isPut, isNestedArray, getNextVal, assert }
module.exports = { sagaTestEngine, isPut, isNestedPut, isNestedArray, getNextVal, assert }
35 changes: 35 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
sagaTestEngine,
isPut,
isNestedArray,
isNestedPut,
} = require('../src')
const {
favSagaWorker,
Expand All @@ -31,6 +32,28 @@ test('isPut correctly identifies a PUT Saga Effect', t => {
})


test('isNestedPut correctly identifies an array of PUT Saga Effects', t => {
t.false(isNestedPut())
t.false(isNestedPut({}))
t.false(isNestedPut([]))
t.false(isNestedPut(put))
t.false(isNestedPut(call))
t.false(isNestedPut(select))
t.false(isNestedPut(call(() => 'call')))
t.false(isNestedPut(select(() => 'select')))
t.false(isNestedPut({CALL: 'someting'}))
t.false(isNestedPut(put({})))
t.false(isNestedPut({PUT: 'someting'}))

t.true(isNestedPut([{PUT: 'someting'}]))
t.true(isNestedPut([put({})]))
t.true(isNestedPut([put({}), put({}), put({})]))

t.false(isNestedPut([call(() => 1)]))
t.false(isNestedPut([put({}), select(() => 1), put({})]))
})


test('isNestedArray correctly identifies a nested array', t => {
t.false(isNestedArray())
t.false(isNestedArray(1))
Expand Down Expand Up @@ -101,6 +124,18 @@ test('sagaTestEngine throws under bad conditions', t => {
const goodMapping3 = [[undefined, undefined]]
t.notThrows(() => sagaTestEngine(f3, goodMapping3))

const f4 = function*() {
yield [put({a: 1})]
}
t.notThrows(
() => sagaTestEngine(f4, goodMapping3),
'Correctly handles nested array of puts'
)

const f5 = function*() {
yield [select(() => 1)]
}
t.throws(() => sagaTestEngine(f5, goodMapping3))
})


Expand Down

0 comments on commit c69b324

Please sign in to comment.