|
| 1 | +import {RequisitionRunner} from './requisition-runners/requisition-runner'; |
| 2 | +import {NotificationEmitter} from './notifications/notification-emitter'; |
| 3 | +import {EnqueuerAsNodeChildRunner} from "./enqueuer-as-node-child-runner"; |
| 4 | +import {Notifications} from "./notifications/notifications"; |
| 5 | + |
| 6 | + |
| 7 | +jest.mock('./requisition-runners/requisition-runner'); |
| 8 | +jest.mock('./notifications/notification-emitter'); |
| 9 | + |
| 10 | +const processOnMock = jest.fn(); |
| 11 | +process.on = processOnMock; |
| 12 | + |
| 13 | +const processSendMock = jest.fn(); |
| 14 | +process.send = processSendMock; |
| 15 | + |
| 16 | +const runMock = jest.fn(); |
| 17 | +const requisitionRunnerConstructorMock = jest.fn(() => ({run: runMock})); |
| 18 | +// @ts-ignore |
| 19 | +RequisitionRunner.mockImplementation(requisitionRunnerConstructorMock); |
| 20 | + |
| 21 | +let notificationEmitterOnMock = jest.fn(); |
| 22 | +// @ts-ignore |
| 23 | +NotificationEmitter.on.mockImplementation(notificationEmitterOnMock); |
| 24 | + |
| 25 | +describe('EnqueuerAsNodeChildRunner', () => { |
| 26 | + beforeEach(() => { |
| 27 | + processOnMock.mockClear(); |
| 28 | + processSendMock.mockClear(); |
| 29 | + requisitionRunnerConstructorMock.mockClear(); |
| 30 | + runMock.mockClear(); |
| 31 | + notificationEmitterOnMock.mockClear(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should listen to message', async () => { |
| 35 | + const statusCode = await new EnqueuerAsNodeChildRunner().execute(true); |
| 36 | + |
| 37 | + expect(statusCode).toBe(0); |
| 38 | + expect(processOnMock.mock.calls[0][0]).toBe('message'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should send exit message', async () => { |
| 42 | + const statusCode = 0; |
| 43 | + await new EnqueuerAsNodeChildRunner().execute(true); |
| 44 | + |
| 45 | + expect(processOnMock.mock.calls[1][0]).toBe('exit'); |
| 46 | + |
| 47 | + const onExitCallback = processOnMock.mock.calls[1][1]; |
| 48 | + onExitCallback(statusCode); |
| 49 | + |
| 50 | + expect(processSendMock).toHaveBeenCalledWith({ |
| 51 | + event: 'PROCESS_EXIT', |
| 52 | + value: statusCode |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should run enqueuer runner when a message arrives', async () => { |
| 57 | + const requisition = 'value'; |
| 58 | + await new EnqueuerAsNodeChildRunner().execute(true); |
| 59 | + |
| 60 | + const onMessageCallback = processOnMock.mock.calls[0][1]; |
| 61 | + onMessageCallback({event: 'runRequisition', value: requisition}); |
| 62 | + expect(requisitionRunnerConstructorMock).toHaveBeenCalledWith(requisition); |
| 63 | + expect(runMock).toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not run enqueuer when a message arrives but is not a requisitions', async () => { |
| 67 | + await new EnqueuerAsNodeChildRunner().execute(true); |
| 68 | + |
| 69 | + const onMessageCallback = processOnMock.mock.calls[0][1]; |
| 70 | + onMessageCallback({event: 'NOT_A REQUISITION'}); |
| 71 | + expect(requisitionRunnerConstructorMock).not.toHaveBeenCalled(); |
| 72 | + expect(runMock).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should register senders to every notification', async () => { |
| 76 | + await new EnqueuerAsNodeChildRunner().execute(true); |
| 77 | + |
| 78 | + const everyNotificationKey = Object.keys(Notifications) |
| 79 | + .map((key: any) => Notifications[key]) |
| 80 | + .filter((key: any) => typeof key === 'number') |
| 81 | + |
| 82 | + expect(notificationEmitterOnMock.mock.calls.map((call: any) => call[0])) |
| 83 | + .toEqual(everyNotificationKey); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should send when notification is emitted', async () => { |
| 87 | + await new EnqueuerAsNodeChildRunner().execute(true); |
| 88 | + |
| 89 | + const report = {cycle: {}}; |
| 90 | + report.cycle = report; |
| 91 | + |
| 92 | + notificationEmitterOnMock.mock.calls[0][1]({report}); |
| 93 | + expect(processSendMock).toHaveBeenCalledWith({ |
| 94 | + event: 'REQUISITION_FINISHED', |
| 95 | + value: { |
| 96 | + report: {} |
| 97 | + } |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | +}); |
0 commit comments