1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

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!
This commit is contained in:
Nuno Góis 2023-07-18 08:12:15 +01:00 committed by GitHub
parent 593f83d5d3
commit 3b20978eb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 208 additions and 0 deletions

View File

@ -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* <http://some-url.com/projects/default/features/some-toggle|some-toggle> 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* <http://some-url.com/projects/default/features/some-toggle|some-toggle> 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* <http://some-url.com/projects/default/features/some-toggle|some-toggle> 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* <http://some-url.com/projects/default/features/some-toggle|some-toggle> in *development* environment in project *default*",
}
`;

View File

@ -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();
});