|
| 1 | +import templates from '../src/templates'; |
| 2 | +import EventNotifyMock from './__mocks__/event-notify'; |
| 3 | +import SeveralEventsNotifyMock from './__mocks__/several-events-notify'; |
| 4 | +import WebhookProvider from '../src/provider'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The sample of a webhook endpoint |
| 8 | + */ |
| 9 | +const webhookEndpointSample = 'https://example.com/hawk-webhook'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Mock the 'deliver' method of WebhookDeliverer |
| 13 | + */ |
| 14 | +const deliver = jest.fn(); |
| 15 | + |
| 16 | +/** |
| 17 | + * Webhook Deliverer mock |
| 18 | + */ |
| 19 | +jest.mock('./../src/deliverer.ts', () => { |
| 20 | + return jest.fn().mockImplementation(() => { |
| 21 | + /** |
| 22 | + * Now we can track calls to 'deliver' |
| 23 | + */ |
| 24 | + return { |
| 25 | + deliver: deliver, |
| 26 | + }; |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +/** |
| 31 | + * Clear all records of mock calls between tests |
| 32 | + */ |
| 33 | +afterEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | +}); |
| 36 | + |
| 37 | +describe('WebhookProvider', () => { |
| 38 | + /** |
| 39 | + * Check that the 'send' method works without errors |
| 40 | + */ |
| 41 | + it('The "send" method should render and deliver message', async () => { |
| 42 | + const provider = new WebhookProvider(); |
| 43 | + |
| 44 | + await provider.send(webhookEndpointSample, EventNotifyMock); |
| 45 | + |
| 46 | + expect(deliver).toHaveBeenCalledTimes(1); |
| 47 | + expect(deliver).toHaveBeenCalledWith(webhookEndpointSample, expect.anything()); |
| 48 | + }); |
| 49 | + |
| 50 | + /** |
| 51 | + * Logic for select the template depended on events count |
| 52 | + */ |
| 53 | + describe('Select correct template', () => { |
| 54 | + /** |
| 55 | + * If there is a single event in payload, use the 'event' template |
| 56 | + */ |
| 57 | + it('Select the event template if there is a single event in notify payload', async () => { |
| 58 | + const provider = new WebhookProvider(); |
| 59 | + const EventTpl = jest.spyOn(templates, 'EventTpl'); |
| 60 | + const SeveralEventsTpl = jest.spyOn(templates, 'SeveralEventsTpl'); |
| 61 | + |
| 62 | + await provider.send(webhookEndpointSample, EventNotifyMock); |
| 63 | + |
| 64 | + expect(EventTpl).toHaveBeenCalledTimes(1); |
| 65 | + expect(SeveralEventsTpl).toHaveBeenCalledTimes(0); |
| 66 | + }); |
| 67 | + |
| 68 | + /** |
| 69 | + * If there are several events in payload, use the 'several-events' template |
| 70 | + */ |
| 71 | + it('Select the several-events template if there are several events in notify payload', async () => { |
| 72 | + const provider = new WebhookProvider(); |
| 73 | + const EventTpl = jest.spyOn(templates, 'EventTpl'); |
| 74 | + const SeveralEventsTpl = jest.spyOn(templates, 'SeveralEventsTpl'); |
| 75 | + |
| 76 | + await provider.send(webhookEndpointSample, SeveralEventsNotifyMock); |
| 77 | + |
| 78 | + expect(EventTpl).toHaveBeenCalledTimes(0); |
| 79 | + expect(SeveralEventsTpl).toHaveBeenCalledTimes(1); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments