-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We encountered an issue on an app using 12.12.x where we needed the payloads from `repo.parallel` when it completed. This commit upstreams that change.
- Loading branch information
Showing
3 changed files
with
80 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Microcosm } from 'microcosm' | ||
|
||
function delay(payload, n) { | ||
return new Promise(resolve => setTimeout(() => resolve(payload), n)) | ||
} | ||
|
||
const cancels = () => { | ||
return action => action.cancel() | ||
} | ||
|
||
const rejects = () => { | ||
return action => action.reject() | ||
} | ||
|
||
describe('Action link', function() { | ||
it('remembers and returns the results of each child action', async function() { | ||
const repo = new Microcosm() | ||
|
||
const action = repo.parallel([ | ||
repo.push(() => delay(1, 20)), | ||
repo.push(() => delay(2, 10)), | ||
repo.push(() => delay(3, 15)) | ||
]) | ||
|
||
let payload = await action | ||
|
||
expect(payload).toEqual([1, 2, 3]) | ||
}) | ||
|
||
it('resolves empty', async function() { | ||
const repo = new Microcosm() | ||
|
||
const action = repo.parallel([]) | ||
|
||
let payload = await action | ||
|
||
expect(payload).toEqual([]) | ||
}) | ||
|
||
it('handles cancelled actions by just returning undefined in that position', function() { | ||
const repo = new Microcosm() | ||
|
||
const action = repo.parallel([ | ||
repo.push(() => 1), | ||
repo.push(cancels), | ||
repo.push(() => 3) | ||
]) | ||
|
||
expect(action.payload).toEqual([1, undefined, 3]) | ||
}) | ||
|
||
it('rejects if any of the actions reject', function() { | ||
const repo = new Microcosm() | ||
|
||
const action = repo.parallel([ | ||
repo.push(rejects), | ||
repo.push(() => 2), | ||
repo.push(() => 3) | ||
]) | ||
|
||
expect(action.status).toEqual('reject') | ||
}) | ||
}) |