From 3b20978eb258caa6cc649dc1d354a1d08cec18f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Tue, 18 Jul 2023 08:12:15 +0100 Subject: [PATCH] test: add some basic tests to the new slack app (#4259) https://linear.app/unleash/issue/2-1244/write-some-tests-for-the-new-slack-app-addon Pretty self-explanatory. These are based on the tests we have for the other addons. If you're reading this and have any suggestions of tests we could add at this stage, please let me know. Thanks! --- .../__snapshots__/slack-app.test.ts.snap | 85 ++++++++++++ src/lib/addons/slack-app.test.ts | 123 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 src/lib/addons/__snapshots__/slack-app.test.ts.snap create mode 100644 src/lib/addons/slack-app.test.ts 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(); +});