|
| 1 | +import { StreamLabsMock } from '../../__mocks__/StreamLabs'; |
| 2 | +import { TwitchChatMock } from '../../__mocks__/TwitchChat'; |
| 3 | +import { MergeRequestOpened } from '../../../src/reactions/gitlab/merge-request-opened'; |
| 4 | +import { Config } from '../../../src/config'; |
| 5 | +import { MergeRequestPayload } from '../../../src/schemas/gitlab/merge-request-payload'; |
| 6 | + |
| 7 | +describe('MergeRequestOpened', () => { |
| 8 | + const twitchChat = new TwitchChatMock(); |
| 9 | + const streamlabs = new StreamLabsMock(); |
| 10 | + |
| 11 | + describe('#canHandle', () => { |
| 12 | + it("creates a new 'MergeRequestOpened' reaction", () => { |
| 13 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 14 | + |
| 15 | + expect(subject).not.toBeNull(); |
| 16 | + }); |
| 17 | + |
| 18 | + it("returns true if the event is 'Merge Request Hook' and 'object_attributes.state' is 'opened'", () => { |
| 19 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 20 | + |
| 21 | + const result = subject.canHandle({ |
| 22 | + event: 'Merge Request Hook', |
| 23 | + payload: { object_attributes: { state: 'opened' } } as MergeRequestPayload, |
| 24 | + }); |
| 25 | + |
| 26 | + expect(result).toEqual(true); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns false if the event is 'Fork' and 'object_attributes.state' is 'opened'", () => { |
| 30 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 31 | + |
| 32 | + const result = subject.canHandle({ |
| 33 | + event: 'Fork', |
| 34 | + payload: { object_attributes: { state: 'opened' } } as MergeRequestPayload, |
| 35 | + }); |
| 36 | + |
| 37 | + expect(result).toEqual(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns false if the event is 'Merge Request Hook' and 'object_attributes.state' is 'merged'", () => { |
| 41 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 42 | + |
| 43 | + const result = subject.canHandle({ |
| 44 | + event: 'Merge Request Hook', |
| 45 | + payload: { object_attributes: { state: 'merged' } } as MergeRequestPayload, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(result).toEqual(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns false if the event is 'Merge Request Hook', 'object_attributes.state' is 'opened' but the opener is in the ignore list", () => { |
| 52 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 53 | + |
| 54 | + const result = subject.canHandle({ |
| 55 | + event: 'Merge Request Hook', |
| 56 | + payload: { |
| 57 | + object_attributes: { state: 'opened' }, |
| 58 | + user: { username: 'SantiMA10' }, |
| 59 | + } as MergeRequestPayload, |
| 60 | + config: { IGNORE_PR_OPENED_BY: ['SantiMA10'] } as Config, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result).toEqual(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns true if the event is 'Merge Request Hook', 'object_attributes.state' is 'opened' and the 'IGNORE_PR_OPENED_BY' is empty", () => { |
| 67 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 68 | + |
| 69 | + const result = subject.canHandle({ |
| 70 | + event: 'Merge Request Hook', |
| 71 | + payload: { |
| 72 | + object_attributes: { state: 'opened' }, |
| 73 | + user: { username: 'SantiMA10' }, |
| 74 | + } as MergeRequestPayload, |
| 75 | + config: { IGNORE_PR_OPENED_BY: [] as string[] } as Config, |
| 76 | + }); |
| 77 | + |
| 78 | + expect(result).toEqual(true); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('#handle', () => { |
| 83 | + it('sends the expected message to TwitchChat', async () => { |
| 84 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 85 | + const payload = { |
| 86 | + object_attributes: { state: 'opened' }, |
| 87 | + user: { username: 'SantiMA10' }, |
| 88 | + repository: { homepage: 'https://gitlab.com/streamlabs/webhook' }, |
| 89 | + } as MergeRequestPayload; |
| 90 | + |
| 91 | + const { twitchChat: response } = await subject.handle({ payload }); |
| 92 | + |
| 93 | + expect(response).toEqual({ |
| 94 | + notified: true, |
| 95 | + message: `${payload.user.username} just opened a merge request in ${payload.repository.homepage}`, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('sends the expected message to StreamLabs', async () => { |
| 100 | + const subject = new MergeRequestOpened(twitchChat, streamlabs); |
| 101 | + const payload = { |
| 102 | + user: { username: 'SantiMA10' }, |
| 103 | + repository: { name: 'streamdevs/webhook' }, |
| 104 | + } as MergeRequestPayload; |
| 105 | + |
| 106 | + const { streamlabs: response } = await subject.handle({ payload }); |
| 107 | + |
| 108 | + expect(response).toEqual({ |
| 109 | + notified: true, |
| 110 | + message: `*${payload.user.username}* just opened a merge request in *${payload.repository.name}*`, |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments