2020-03-20 16:46:24 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { emitEvent, emitEventWithAbort } from '../emitters';
|
|
|
|
|
|
|
|
describe( 'Testing emitters', () => {
|
|
|
|
let observerMocks = {};
|
2020-04-02 09:27:54 +00:00
|
|
|
let observerA;
|
|
|
|
let observerB;
|
|
|
|
let observerPromiseWithResolvedValue;
|
2020-03-20 16:46:24 +00:00
|
|
|
beforeEach( () => {
|
2020-04-02 09:27:54 +00:00
|
|
|
observerA = jest.fn().mockReturnValue( true );
|
|
|
|
observerB = jest.fn().mockReturnValue( true );
|
|
|
|
observerPromiseWithResolvedValue = jest.fn().mockResolvedValue( 10 );
|
|
|
|
observerMocks = new Map( [
|
2020-04-03 11:50:54 +00:00
|
|
|
[ 'observerA', { priority: 10, callback: observerA } ],
|
|
|
|
[ 'observerB', { priority: 10, callback: observerB } ],
|
|
|
|
[
|
|
|
|
'observerReturnValue',
|
|
|
|
{ priority: 10, callback: jest.fn().mockReturnValue( 10 ) },
|
|
|
|
],
|
2020-04-02 09:27:54 +00:00
|
|
|
[
|
|
|
|
'observerPromiseWithReject',
|
2020-04-03 11:50:54 +00:00
|
|
|
{
|
|
|
|
priority: 10,
|
|
|
|
callback: jest.fn().mockRejectedValue( 'an error' ),
|
|
|
|
},
|
2020-04-02 09:27:54 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'observerPromiseWithResolvedValue',
|
2020-04-03 11:50:54 +00:00
|
|
|
{ priority: 10, callback: observerPromiseWithResolvedValue },
|
2020-04-02 09:27:54 +00:00
|
|
|
],
|
2021-05-07 20:39:28 +00:00
|
|
|
[
|
|
|
|
'observerSuccessType',
|
|
|
|
{
|
|
|
|
priority: 10,
|
|
|
|
callback: jest.fn().mockReturnValue( { type: 'success' } ),
|
|
|
|
},
|
|
|
|
],
|
2020-04-02 09:27:54 +00:00
|
|
|
] );
|
2020-03-20 16:46:24 +00:00
|
|
|
} );
|
|
|
|
describe( 'Testing emitEvent()', () => {
|
|
|
|
it( 'invokes all observers', async () => {
|
2020-03-23 15:32:52 +00:00
|
|
|
const observers = { test: observerMocks };
|
2020-03-20 16:46:24 +00:00
|
|
|
const response = await emitEvent( observers, 'test', 'foo' );
|
|
|
|
expect( console ).toHaveErroredWith( 'an error' );
|
2020-04-02 09:27:54 +00:00
|
|
|
expect( observerA ).toHaveBeenCalledTimes( 1 );
|
|
|
|
expect( observerB ).toHaveBeenCalledWith( 'foo' );
|
2021-05-07 20:39:28 +00:00
|
|
|
expect( response ).toEqual( [ { type: 'success' } ] );
|
2020-03-20 16:46:24 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
describe( 'Testing emitEventWithAbort()', () => {
|
2021-05-07 20:39:28 +00:00
|
|
|
it( 'does not abort on any return value other than an object with an error or fail type property', async () => {
|
2020-04-21 02:33:16 +00:00
|
|
|
observerMocks.delete( 'observerPromiseWithReject' );
|
|
|
|
const observers = { test: observerMocks };
|
|
|
|
const response = await emitEventWithAbort(
|
|
|
|
observers,
|
|
|
|
'test',
|
|
|
|
'foo'
|
|
|
|
);
|
|
|
|
expect( console ).not.toHaveErrored();
|
|
|
|
expect( observerB ).toHaveBeenCalledTimes( 1 );
|
|
|
|
expect( observerPromiseWithResolvedValue ).toHaveBeenCalled();
|
2021-05-07 20:39:28 +00:00
|
|
|
expect( response ).toEqual( [ { type: 'success' } ] );
|
2020-04-21 02:33:16 +00:00
|
|
|
} );
|
2021-05-07 20:39:28 +00:00
|
|
|
it( 'Aborts on a return value with an object that has a a fail type property', async () => {
|
2020-04-21 02:33:16 +00:00
|
|
|
const validObjectResponse = jest
|
|
|
|
.fn()
|
2021-05-07 20:39:28 +00:00
|
|
|
.mockReturnValue( { type: 'failure' } );
|
2020-04-21 02:33:16 +00:00
|
|
|
observerMocks.set( 'observerValidObject', {
|
|
|
|
priority: 5,
|
|
|
|
callback: validObjectResponse,
|
|
|
|
} );
|
|
|
|
const observers = { test: observerMocks };
|
|
|
|
const response = await emitEventWithAbort(
|
|
|
|
observers,
|
|
|
|
'test',
|
|
|
|
'foo'
|
|
|
|
);
|
|
|
|
expect( console ).not.toHaveErrored();
|
|
|
|
expect( validObjectResponse ).toHaveBeenCalledTimes( 1 );
|
|
|
|
expect( observerPromiseWithResolvedValue ).not.toHaveBeenCalled();
|
2021-05-07 20:39:28 +00:00
|
|
|
expect( response ).toEqual( [ { type: 'failure' } ] );
|
2020-04-21 02:33:16 +00:00
|
|
|
} );
|
|
|
|
it( 'throws an error on an object returned from observer without a type property', async () => {
|
|
|
|
const failingObjectResponse = jest.fn().mockReturnValue( {} );
|
|
|
|
observerMocks.set( 'observerInvalidObject', {
|
|
|
|
priority: 5,
|
|
|
|
callback: failingObjectResponse,
|
|
|
|
} );
|
|
|
|
const observers = { test: observerMocks };
|
|
|
|
const response = await emitEventWithAbort(
|
|
|
|
observers,
|
|
|
|
'test',
|
|
|
|
'foo'
|
|
|
|
);
|
|
|
|
expect( console ).toHaveErrored();
|
|
|
|
expect( failingObjectResponse ).toHaveBeenCalledTimes( 1 );
|
|
|
|
expect( observerPromiseWithResolvedValue ).not.toHaveBeenCalled();
|
2021-05-07 20:39:28 +00:00
|
|
|
expect( response ).toEqual( [ { type: 'error' } ] );
|
2020-04-21 02:33:16 +00:00
|
|
|
} );
|
2020-03-20 16:46:24 +00:00
|
|
|
} );
|
2020-04-03 11:50:54 +00:00
|
|
|
describe( 'Test Priority', () => {
|
|
|
|
it( 'executes observers in expected order by priority', async () => {
|
|
|
|
const a = jest.fn();
|
2020-04-21 02:33:16 +00:00
|
|
|
const b = jest.fn().mockReturnValue( { type: 'error' } );
|
2020-04-03 11:50:54 +00:00
|
|
|
const observers = {
|
|
|
|
test: new Map( [
|
|
|
|
[ 'observerA', { priority: 200, callback: a } ],
|
|
|
|
[ 'observerB', { priority: 10, callback: b } ],
|
|
|
|
] ),
|
|
|
|
};
|
|
|
|
await emitEventWithAbort( observers, 'test', 'foo' );
|
|
|
|
expect( console ).not.toHaveErrored();
|
2020-04-08 16:36:04 +00:00
|
|
|
expect( b ).toHaveBeenCalledTimes( 1 );
|
|
|
|
expect( a ).not.toHaveBeenCalled();
|
2020-04-03 11:50:54 +00:00
|
|
|
} );
|
|
|
|
} );
|
2020-03-20 16:46:24 +00:00
|
|
|
} );
|