Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('saga race between array of effects handling', () => {
let actual = []
const middleware = sagaMiddleware()
const store = applyMiddleware(middleware)(createStore)(() => {})
const timeout = deferred()
function* genFn() {
actual.push(yield io.race([io.take('action'), timeout.promise]))
}
const task = middleware.run(genFn)
// eslint-disable-next-line no-sparse-arrays
const expected = [[, 1]]
return Promise.resolve()
.then(() => timeout.resolve(1))
.then(() =>
store.dispatch({
type: 'action',
}),
)
.then(() => task.toPromise())
const store = createStore(rootReducer, undefined, applyMiddleware(sagaMiddleware))
function* subTask() {
try {
// eslint-disable-next-line no-constant-condition
while (true) {
actual.push('subTask taking END')
yield io.take('NEXT')
actual.push('should not get here')
}
} finally {
actual.push('auto ended')
}
}
const def = deferred()
const rootSaga = sagaMiddleware.run(function*() {
// eslint-disable-next-line no-constant-condition
while (true) {
yield io.take('START')
actual.push('start taken')
yield def.promise
actual.push('non-take effect resolved')
yield io.fork(subTask)
actual.push('subTask forked')
}
})
Promise.resolve()
.then(() => {
store.dispatch({
type: 'START',
})
test('saga cancellation: take effect', () => {
let actual = []
let startDef = deferred()
let cancelDef = deferred()
const middleware = sagaMiddleware()
const store = applyMiddleware(middleware)(createStore)(() => {})
function* main() {
actual.push(yield startDef.promise)
try {
actual.push(yield io.take('action'))
} finally {
if (yield io.cancelled()) actual.push(yield 'cancelled')
}
}
const task = middleware.run(main)
cancelDef.promise.then(v => {
test('saga cancellation: nested task cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let start = deferred(),
stop = deferred(),
subtaskDefs = arrayOfDeferred(2),
nestedTask1Defs = arrayOfDeferred(2),
nestedTask2Defs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => start.resolve('start'))
.then(() => subtaskDefs[0].resolve('subtask_1'))
.then(() => nestedTask1Defs[0].resolve('nested_task_1_1'))
.then(() => nestedTask2Defs[0].resolve('nested_task_2_1'))
.then(() => stop.resolve('stop'))
.then(() => nestedTask1Defs[1].resolve('nested_task_1_2'))
.then(() => nestedTask2Defs[1].resolve('nested_task_2_2'))
.then(() => subtaskDefs[1].resolve('subtask_2'))
function* nestedTask1() {
try {
test('saga cancellation: forked children', () => {
const actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let cancelDef = deferred()
const rootDef = deferred()
const childAdef = deferred()
const childBdef = deferred()
const neverDef = deferred()
const defs = arrayOfDeferred(4)
Promise.resolve()
.then(() => childAdef.resolve('childA resolve'))
.then(() => rootDef.resolve('root resolve'))
.then(() => defs[0].resolve('leaf 0 resolve'))
.then(() => childBdef.resolve('childB resolve')) //
.then(() => cancelDef.resolve('cancel'))
.then(() => defs[3].resolve('leaf 3 resolve'))
.then(() => defs[2].resolve('leaf 2 resolve'))
.then(() => defs[1].resolve('leaf 1 resolve'))
function* main() {
try {
yield io.fork(childA)
actual.push(yield rootDef.promise)
yield io.fork(childB)
test('saga cancellation: forked children', () => {
const actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let cancelDef = deferred()
const rootDef = deferred()
const childAdef = deferred()
const childBdef = deferred()
const neverDef = deferred()
const defs = arrayOfDeferred(4)
Promise.resolve()
.then(() => childAdef.resolve('childA resolve'))
.then(() => rootDef.resolve('root resolve'))
.then(() => defs[0].resolve('leaf 0 resolve'))
.then(() => childBdef.resolve('childB resolve')) //
.then(() => cancelDef.resolve('cancel'))
.then(() => defs[3].resolve('leaf 3 resolve'))
.then(() => defs[2].resolve('leaf 2 resolve'))
.then(() => defs[1].resolve('leaf 1 resolve'))
function* main() {
try {
test('saga cancellation: forked children', () => {
const actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let cancelDef = deferred()
const rootDef = deferred()
const childAdef = deferred()
const childBdef = deferred()
const neverDef = deferred()
const defs = arrayOfDeferred(4)
Promise.resolve()
.then(() => childAdef.resolve('childA resolve'))
.then(() => rootDef.resolve('root resolve'))
.then(() => defs[0].resolve('leaf 0 resolve'))
.then(() => childBdef.resolve('childB resolve')) //
.then(() => cancelDef.resolve('cancel'))
.then(() => defs[3].resolve('leaf 3 resolve'))
.then(() => defs[2].resolve('leaf 2 resolve'))
.then(() => defs[1].resolve('leaf 1 resolve'))
function* main() {
try {
yield io.fork(childA)
test('saga cancellation: nested task cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let start = deferred(),
stop = deferred(),
subtaskDefs = arrayOfDeferred(2),
nestedTask1Defs = arrayOfDeferred(2),
nestedTask2Defs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => start.resolve('start'))
.then(() => subtaskDefs[0].resolve('subtask_1'))
.then(() => nestedTask1Defs[0].resolve('nested_task_1_1'))
.then(() => nestedTask2Defs[0].resolve('nested_task_2_1'))
.then(() => stop.resolve('stop'))
.then(() => nestedTask1Defs[1].resolve('nested_task_1_2'))
.then(() => nestedTask2Defs[1].resolve('nested_task_2_2'))
.then(() => subtaskDefs[1].resolve('subtask_2'))
function* nestedTask1() {
try {
actual.push(yield nestedTask1Defs[0].promise)
test('saga cancellation: join effect (joining from a different task)', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let cancelDef = deferred()
let subroutineDef = deferred()
Promise.resolve(1)
.then(() => cancelDef.resolve('cancel'))
.then(() => subroutineDef.resolve('subroutine'))
function* main() {
actual.push('start')
let task = yield io.fork(subroutine)
yield io.fork(callerOfJoiner1, task)
yield io.fork(joiner2, task)
actual.push(yield cancelDef.promise)
yield io.cancel(task)
}
function* subroutine() {
actual.push('subroutine start')
function toPromise() {
if (deferredEnd) {
return deferredEnd.promise
}
deferredEnd = deferred()
if (status === ABORTED) {
deferredEnd.reject(taskError)
} else if (status !== RUNNING) {
deferredEnd.resolve(taskResult)
}
return deferredEnd.promise
}