feat: add CompositeAsyncHandler to support multiple handlers

This commit is contained in:
Joachim Van Herwegen
2020-05-28 10:55:29 +02:00
parent 57405f3e26
commit 4229932a3a
14 changed files with 275 additions and 23 deletions

View File

@@ -0,0 +1,30 @@
import { AsyncHandler } from '../../../src/util/AsyncHandler';
import { StaticAsyncHandler } from '../../util/StaticAsyncHandler';
describe('An AsyncHandler', (): void => {
it('calls canHandle and handle when handleSafe is called.', async (): Promise<void> => {
const handlerTrue: AsyncHandler<any, any> = new StaticAsyncHandler(true, null);
const canHandleFn = jest.fn(async (input: any): Promise<void> => input);
const handleFn = jest.fn(async (input: any): Promise<any> => input);
handlerTrue.canHandle = canHandleFn;
handlerTrue.handle = handleFn;
await expect(handlerTrue.handleSafe('test')).resolves.toBe('test');
expect(canHandleFn).toHaveBeenCalledTimes(1);
expect(handleFn).toHaveBeenCalledTimes(1);
});
it('does not call handle when canHandle errors during a handleSafe call.', async (): Promise<void> => {
const handlerFalse: AsyncHandler<any, any> = new StaticAsyncHandler(false, null);
const canHandleFn = jest.fn(async (): Promise<void> => {
throw new Error('test');
});
const handleFn = jest.fn(async (input: any): Promise<any> => input);
handlerFalse.canHandle = canHandleFn;
handlerFalse.handle = handleFn;
await expect(handlerFalse.handleSafe('test')).rejects.toThrow(Error);
expect(canHandleFn).toHaveBeenCalledTimes(1);
expect(handleFn).toHaveBeenCalledTimes(0);
});
});