Skip to content

Commit d4bcccd

Browse files
committed
chore(action-middleware): add error handling tests
1 parent 9dd4523 commit d4bcccd

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

packages/action-listener-middleware/src/tests/listenerMiddleware.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ describe('createActionListenerMiddleware', () => {
6060
const testAction3 = createAction<string>('testAction3')
6161
type TestAction3 = ReturnType<typeof testAction3>
6262

63+
beforeAll(() => {
64+
jest.spyOn(console, 'error').mockImplementation(noop)
65+
})
66+
6367
beforeEach(() => {
6468
middleware = createActionListenerMiddleware()
6569
reducer = jest.fn(() => ({}))
@@ -461,7 +465,7 @@ describe('createActionListenerMiddleware', () => {
461465
expect(reducer.mock.calls).toEqual([[{}, testAction1('a')]])
462466
})
463467

464-
test('Continues running other listeners if there is an error', () => {
468+
test('Continues running other listeners if one of them raises an error', () => {
465469
const matcher = (action: any) => true
466470

467471
middleware.addListener(matcher, () => {
@@ -475,6 +479,47 @@ describe('createActionListenerMiddleware', () => {
475479
expect(listener.mock.calls).toEqual([[testAction1('a'), middlewareApi]])
476480
})
477481

482+
test('Continues running other listeners if a predicate raises an error', () => {
483+
const matcher = (action: any) => true
484+
const firstListener = jest.fn(() => {})
485+
const secondListener = jest.fn(() => {})
486+
487+
middleware.addListener(() => {
488+
throw new Error('Predicate Panic!')
489+
}, firstListener);
490+
491+
middleware.addListener(matcher, secondListener)
492+
493+
store.dispatch(testAction1('a'))
494+
expect(firstListener).not.toHaveBeenCalled();
495+
expect(secondListener.mock.calls).toEqual([
496+
[testAction1('a'), middlewareApi],
497+
])
498+
})
499+
500+
test('Notifies listener errors to `onError`, if provided', () => {
501+
const onError = jest.fn();
502+
middleware = createActionListenerMiddleware({
503+
onError
504+
})
505+
reducer = jest.fn(() => ({}))
506+
store = configureStore({
507+
reducer,
508+
middleware: (gDM) => gDM().prepend(middleware),
509+
})
510+
511+
const listenerError = new Error('Boom!');
512+
513+
const matcher = (action: any) => true
514+
515+
middleware.addListener(matcher, () => {
516+
throw listenerError;
517+
});
518+
519+
store.dispatch(testAction1('a'))
520+
expect(onError).toBeCalledWith(listenerError);
521+
})
522+
478523
test('condition method resolves promise when the predicate succeeds', async () => {
479524
const store = configureStore({
480525
reducer: counterSlice.reducer,

0 commit comments

Comments
 (0)