From 2e4b6e53663c5780b334ebbd1b0a886e476510f5 Mon Sep 17 00:00:00 2001 From: Tim Buckley Date: Fri, 3 Feb 2017 10:42:12 -0500 Subject: [PATCH 1/3] Create tests for isNestedPut --- tests/index.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/index.js b/tests/index.js index eccb54e..90d3d81 100644 --- a/tests/index.js +++ b/tests/index.js @@ -5,6 +5,7 @@ const { sagaTestEngine, isPut, isNestedArray, + isNestedPut, } = require('../src') const { favSagaWorker, @@ -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)) From d7ae364500f5760f66c5fe5c03fe9b6aa884979b Mon Sep 17 00:00:00 2001 From: Tim Buckley Date: Fri, 3 Feb 2017 10:43:50 -0500 Subject: [PATCH 2/3] Create isNestedPut. Use in sagaTestEngine assert --- src/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 554c5e4..0d8ff27 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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)}`) @@ -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 } From 0676a315cda56557334f3da45c62386e354b2b88 Mon Sep 17 00:00:00 2001 From: Tim Buckley Date: Fri, 3 Feb 2017 11:04:54 -0500 Subject: [PATCH 3/3] Test that sagaTestEngine correctly handles nested Puts --- tests/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/index.js b/tests/index.js index 90d3d81..b88e2cd 100644 --- a/tests/index.js +++ b/tests/index.js @@ -124,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)) })