diff --git a/src/lib/addons/__snapshots__/slack-app.test.ts.snap b/src/lib/addons/__snapshots__/slack-app.test.ts.snap new file mode 100644 index 0000000000..796b0787f4 --- /dev/null +++ b/src/lib/addons/__snapshots__/slack-app.test.ts.snap @@ -0,0 +1,85 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Should not post to unexisting tagged channels 1`] = ` +{ + "attachments": [ + { + "actions": [ + { + "name": "featureToggle", + "style": "primary", + "text": "Open in Unleash", + "type": "button", + "url": "http://some-url.com/projects/default/features/some-toggle", + "value": "featureToggle", + }, + ], + }, + ], + "channel": 2, + "text": "some@user.com *enabled* in *development* environment in project *default*", +} +`; + +exports[`Should post message when feature is toggled 1`] = ` +{ + "attachments": [ + { + "actions": [ + { + "name": "featureToggle", + "style": "primary", + "text": "Open in Unleash", + "type": "button", + "url": "http://some-url.com/projects/default/features/some-toggle", + "value": "featureToggle", + }, + ], + }, + ], + "channel": 1, + "text": "some@user.com *enabled* in *development* environment in project *default*", +} +`; + +exports[`Should post to all channels in tags 1`] = ` +{ + "attachments": [ + { + "actions": [ + { + "name": "featureToggle", + "style": "primary", + "text": "Open in Unleash", + "type": "button", + "url": "http://some-url.com/projects/default/features/some-toggle", + "value": "featureToggle", + }, + ], + }, + ], + "channel": 1, + "text": "some@user.com *enabled* in *development* environment in project *default*", +} +`; + +exports[`Should post to all channels in tags 2`] = ` +{ + "attachments": [ + { + "actions": [ + { + "name": "featureToggle", + "style": "primary", + "text": "Open in Unleash", + "type": "button", + "url": "http://some-url.com/projects/default/features/some-toggle", + "value": "featureToggle", + }, + ], + }, + ], + "channel": 2, + "text": "some@user.com *enabled* in *development* environment in project *default*", +} +`; diff --git a/src/lib/addons/slack-app.test.ts b/src/lib/addons/slack-app.test.ts new file mode 100644 index 0000000000..73880612bd --- /dev/null +++ b/src/lib/addons/slack-app.test.ts @@ -0,0 +1,123 @@ +import { IEvent, FEATURE_ENVIRONMENT_ENABLED } from '../types/events'; + +import SlackAppAddon from './slack-app'; + +import noLogger from '../../test/fixtures/no-logger'; +import { ChatPostMessageArguments } from '@slack/web-api'; + +const accessToken = 'test-access-token'; +const slackApiCalls: ChatPostMessageArguments[] = []; +jest.mock('@slack/web-api', () => ({ + WebClient: jest.fn().mockImplementation(() => ({ + conversations: { + list: () => ({ + channels: [ + { + id: 1, + name: 'general', + }, + { + id: 2, + name: 'another-channel-1', + }, + ], + }), + }, + chat: { + postMessage: jest.fn().mockImplementation((options) => { + slackApiCalls.push(options); + return Promise.resolve(); + }), + }, + })), +})); + +beforeEach(() => { + slackApiCalls.length = 0; +}); + +test('Should post message when feature is toggled', async () => { + const addon = new SlackAppAddon({ + getLogger: noLogger, + unleashUrl: 'http://some-url.com', + }); + const event: IEvent = { + id: 1, + createdAt: new Date(), + type: FEATURE_ENVIRONMENT_ENABLED, + createdBy: 'some@user.com', + project: 'default', + featureName: 'some-toggle', + environment: 'development', + data: { + name: 'some-toggle', + enabled: false, + type: 'release', + strategies: [{ name: 'default' }], + }, + tags: [{ type: 'slack', value: 'general' }], + }; + + await addon.handleEvent(event, { accessToken }); + expect(slackApiCalls.length).toBe(1); + expect(slackApiCalls[0].channel).toBe(1); + expect(slackApiCalls[0]).toMatchSnapshot(); +}); + +test('Should post to all channels in tags', async () => { + const addon = new SlackAppAddon({ + getLogger: noLogger, + unleashUrl: 'http://some-url.com', + }); + const event: IEvent = { + id: 2, + createdAt: new Date(), + type: FEATURE_ENVIRONMENT_ENABLED, + createdBy: 'some@user.com', + project: 'default', + featureName: 'some-toggle', + environment: 'development', + data: { + name: 'some-toggle', + }, + tags: [ + { type: 'slack', value: 'general' }, + { type: 'slack', value: 'another-channel-1' }, + ], + }; + + await addon.handleEvent(event, { accessToken }); + expect(slackApiCalls.length).toBe(2); + expect(slackApiCalls[0].channel).toBe(1); + expect(slackApiCalls[0]).toMatchSnapshot(); + expect(slackApiCalls[1].channel).toBe(2); + expect(slackApiCalls[1]).toMatchSnapshot(); +}); + +test('Should not post to unexisting tagged channels', async () => { + const addon = new SlackAppAddon({ + getLogger: noLogger, + unleashUrl: 'http://some-url.com', + }); + const event: IEvent = { + id: 3, + createdAt: new Date(), + type: FEATURE_ENVIRONMENT_ENABLED, + createdBy: 'some@user.com', + project: 'default', + featureName: 'some-toggle', + environment: 'development', + data: { + name: 'some-toggle', + }, + tags: [ + { type: 'slack', value: 'random' }, + { type: 'slack', value: 'another-channel-1' }, + ], + }; + + await addon.handleEvent(event, { accessToken }); + expect(slackApiCalls.length).toBe(1); + expect(slackApiCalls[0].channel).toBe(2); + expect(slackApiCalls[0]).toMatchSnapshot(); +});