-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at enqueueItemAndWaitForResults.
- Loading branch information
1 parent
043da74
commit e36d90e
Showing
5 changed files
with
174 additions
and
5 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
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,83 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import store from '../store'; | ||
import { | ||
registerProcessor, | ||
enqueueItemAndWaitForResults, | ||
processBatch, | ||
} from '../store/actions'; | ||
import { STATE_ERROR, STATE_SUCCESS } from '../'; | ||
|
||
const TEST_QUEUE = 'TEST_QUEUE'; | ||
const TEST_CONTEXT = 'default'; | ||
|
||
async function processor( requests, transaction ) { | ||
if ( transaction.state === STATE_ERROR ) { | ||
throw { | ||
code: 'transaction_failed', | ||
data: { status: 500 }, | ||
message: 'Transaction failed.', | ||
}; | ||
} | ||
|
||
return await Promise.resolve( | ||
requests.map( ( request ) => ( { | ||
done: true, | ||
name: request.name, | ||
} ) ) | ||
); | ||
} | ||
|
||
async function testItem( name ) { | ||
const item = { name }; | ||
const { wait } = await store.dispatch( | ||
enqueueItemAndWaitForResults( TEST_QUEUE, TEST_CONTEXT, item ) | ||
); | ||
|
||
const expected = { done: true, name }; | ||
|
||
// We can't await this until the batch is processed. | ||
// eslint-disable-next-line jest/valid-expect | ||
const promise = expect( wait ).resolves.toEqual( expected ); | ||
|
||
return { expected, promise }; | ||
} | ||
|
||
describe( 'waitForResults', function () { | ||
store.dispatch( registerProcessor( TEST_QUEUE, processor ) ); | ||
|
||
it( 'works', async () => { | ||
expect.assertions( 4 ); | ||
|
||
const { expected: i1, promise: p1 } = await testItem( 'i1' ); | ||
const { expected: i2, promise: p2 } = await testItem( 'i2' ); | ||
|
||
const resolves = [ p1, p2 ]; | ||
const batch = await store.dispatch( | ||
processBatch( TEST_QUEUE, TEST_CONTEXT ) | ||
); | ||
|
||
expect( batch.state ).toEqual( STATE_SUCCESS ); | ||
expect( Object.values( batch.results ) ).toEqual( [ i1, i2 ] ); | ||
|
||
await Promise.all( resolves ); | ||
} ); | ||
|
||
it( 'can use the same context more than once', async () => { | ||
expect.assertions( 4 ); | ||
|
||
const { promise: p1 } = await testItem( 'i1' ); | ||
await store.dispatch( processBatch( TEST_QUEUE, TEST_CONTEXT ) ); | ||
await p1; | ||
|
||
const { expected: i2, promise: p2 } = await testItem( 'i2' ); | ||
const batch = await store.dispatch( | ||
processBatch( TEST_QUEUE, TEST_CONTEXT ) | ||
); | ||
|
||
expect( batch.state ).toEqual( STATE_SUCCESS ); | ||
expect( Object.values( batch.results ) ).toEqual( [ i2 ] ); | ||
await p2; | ||
} ); | ||
} ); |