mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-19 01:17:18 +02:00
fix: update slack-app tests to reflect what we now do
This commit is contained in:
parent
c58d325173
commit
d7c7abdda3
@ -1,127 +0,0 @@
|
|||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
||||||
|
|
||||||
exports[`SlackAppAddon 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[`SlackAppAddon 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[`SlackAppAddon 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[`SlackAppAddon 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*",
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`SlackAppAddon should use defaultChannels if no tagged channels are found 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[`SlackAppAddon should use defaultChannels if no tagged channels are found 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*",
|
|
||||||
}
|
|
||||||
`;
|
|
@ -3,34 +3,16 @@ import SlackAppAddon from './slack-app';
|
|||||||
import { ChatPostMessageArguments, ErrorCode } from '@slack/web-api';
|
import { ChatPostMessageArguments, ErrorCode } from '@slack/web-api';
|
||||||
|
|
||||||
const slackApiCalls: ChatPostMessageArguments[] = [];
|
const slackApiCalls: ChatPostMessageArguments[] = [];
|
||||||
const conversationsList = jest.fn();
|
|
||||||
let postMessage = jest.fn().mockImplementation((options) => {
|
let scheduleMessage = jest.fn().mockImplementation((options) => {
|
||||||
slackApiCalls.push(options);
|
slackApiCalls.push(options);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
jest.mock('@slack/web-api', () => ({
|
jest.mock('@slack/web-api', () => ({
|
||||||
WebClient: jest.fn().mockImplementation(() => ({
|
WebClient: jest.fn().mockImplementation(() => ({
|
||||||
conversations: {
|
|
||||||
list: conversationsList.mockImplementation(() => ({
|
|
||||||
channels: [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
name: 'general',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
name: 'another-channel-1',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
name: 'another-channel-2',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
chat: {
|
chat: {
|
||||||
postMessage,
|
scheduleMessage,
|
||||||
},
|
},
|
||||||
on: jest.fn(),
|
on: jest.fn(),
|
||||||
})),
|
})),
|
||||||
@ -78,8 +60,7 @@ describe('SlackAppAddon', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
slackApiCalls.length = 0;
|
slackApiCalls.length = 0;
|
||||||
conversationsList.mockClear();
|
scheduleMessage.mockClear();
|
||||||
postMessage.mockClear();
|
|
||||||
addon = new SlackAppAddon({
|
addon = new SlackAppAddon({
|
||||||
getLogger,
|
getLogger,
|
||||||
unleashUrl: 'http://some-url.com',
|
unleashUrl: 'http://some-url.com',
|
||||||
@ -88,15 +69,13 @@ describe('SlackAppAddon', () => {
|
|||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.useRealTimers();
|
jest.useRealTimers();
|
||||||
addon.destroy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should post message when feature is toggled', async () => {
|
it('should post message when feature is toggled', async () => {
|
||||||
await addon.handleEvent(event, { accessToken });
|
await addon.handleEvent(event, { accessToken });
|
||||||
|
|
||||||
expect(slackApiCalls.length).toBe(1);
|
expect(slackApiCalls.length).toBe(1);
|
||||||
expect(slackApiCalls[0].channel).toBe(1);
|
expect(slackApiCalls[0].channel).toBe('general');
|
||||||
expect(slackApiCalls[0]).toMatchSnapshot();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should post to all channels in tags', async () => {
|
it('should post to all channels in tags', async () => {
|
||||||
@ -111,47 +90,8 @@ describe('SlackAppAddon', () => {
|
|||||||
await addon.handleEvent(eventWith2Tags, { accessToken });
|
await addon.handleEvent(eventWith2Tags, { accessToken });
|
||||||
|
|
||||||
expect(slackApiCalls.length).toBe(2);
|
expect(slackApiCalls.length).toBe(2);
|
||||||
expect(slackApiCalls[0].channel).toBe(1);
|
expect(slackApiCalls[0].channel).toBe('general');
|
||||||
expect(slackApiCalls[0]).toMatchSnapshot();
|
expect(slackApiCalls[1].channel).toBe('another-channel-1');
|
||||||
expect(slackApiCalls[1].channel).toBe(2);
|
|
||||||
expect(slackApiCalls[1]).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not post to unexisting tagged channels', async () => {
|
|
||||||
const eventWithUnexistingTaggedChannel: IEvent = {
|
|
||||||
...event,
|
|
||||||
tags: [
|
|
||||||
{ type: 'slack', value: 'random' },
|
|
||||||
{ type: 'slack', value: 'another-channel-1' },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
await addon.handleEvent(eventWithUnexistingTaggedChannel, {
|
|
||||||
accessToken,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(slackApiCalls.length).toBe(1);
|
|
||||||
expect(slackApiCalls[0].channel).toBe(2);
|
|
||||||
expect(slackApiCalls[0]).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should cache Slack channels', async () => {
|
|
||||||
await addon.handleEvent(event, { accessToken });
|
|
||||||
await addon.handleEvent(event, { accessToken });
|
|
||||||
|
|
||||||
expect(slackApiCalls.length).toBe(2);
|
|
||||||
expect(conversationsList).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should refresh Slack channels cache after 30 seconds', async () => {
|
|
||||||
await addon.handleEvent(event, { accessToken });
|
|
||||||
|
|
||||||
jest.advanceTimersByTime(30000);
|
|
||||||
|
|
||||||
await addon.handleEvent(event, { accessToken });
|
|
||||||
|
|
||||||
expect(slackApiCalls.length).toBe(2);
|
|
||||||
expect(conversationsList).toHaveBeenCalledTimes(2);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not post a message if there are no tagged channels and no defaultChannels', async () => {
|
it('should not post a message if there are no tagged channels and no defaultChannels', async () => {
|
||||||
@ -179,14 +119,12 @@ describe('SlackAppAddon', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(slackApiCalls.length).toBe(2);
|
expect(slackApiCalls.length).toBe(2);
|
||||||
expect(slackApiCalls[0].channel).toBe(1);
|
expect(slackApiCalls[0].channel).toBe('general');
|
||||||
expect(slackApiCalls[0]).toMatchSnapshot();
|
expect(slackApiCalls[1].channel).toBe('another-channel-1');
|
||||||
expect(slackApiCalls[1].channel).toBe(2);
|
|
||||||
expect(slackApiCalls[1]).toMatchSnapshot();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should log error when an API call fails', async () => {
|
it('should log error when an API call fails', async () => {
|
||||||
postMessage = jest.fn().mockRejectedValue(mockError);
|
scheduleMessage = jest.fn().mockRejectedValue(mockError);
|
||||||
|
|
||||||
await addon.handleEvent(event, { accessToken });
|
await addon.handleEvent(event, { accessToken });
|
||||||
|
|
||||||
@ -196,7 +134,7 @@ describe('SlackAppAddon', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle rejections in chat.postMessage', async () => {
|
it('should handle rejections in chat.scheduleMessage', async () => {
|
||||||
const eventWith3Tags: IEvent = {
|
const eventWith3Tags: IEvent = {
|
||||||
...event,
|
...event,
|
||||||
tags: [
|
tags: [
|
||||||
@ -206,7 +144,7 @@ describe('SlackAppAddon', () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
postMessage = jest
|
scheduleMessage = jest
|
||||||
.fn()
|
.fn()
|
||||||
.mockResolvedValueOnce({ ok: true })
|
.mockResolvedValueOnce({ ok: true })
|
||||||
.mockResolvedValueOnce({ ok: true })
|
.mockResolvedValueOnce({ ok: true })
|
||||||
@ -214,7 +152,7 @@ describe('SlackAppAddon', () => {
|
|||||||
|
|
||||||
await addon.handleEvent(eventWith3Tags, { accessToken });
|
await addon.handleEvent(eventWith3Tags, { accessToken });
|
||||||
|
|
||||||
expect(postMessage).toHaveBeenCalledTimes(3);
|
expect(scheduleMessage).toHaveBeenCalledTimes(3);
|
||||||
expect(loggerMock.warn).toHaveBeenCalledWith(
|
expect(loggerMock.warn).toHaveBeenCalledWith(
|
||||||
`Error handling event ${FEATURE_ENVIRONMENT_ENABLED}. A platform error occurred: Platform error message`,
|
`Error handling event ${FEATURE_ENVIRONMENT_ENABLED}. A platform error occurred: Platform error message`,
|
||||||
expect.any(Object),
|
expect.any(Object),
|
||||||
|
@ -93,7 +93,6 @@ export default class SlackAppAddon extends Addon {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'actions',
|
type: 'actions',
|
||||||
block_id: url,
|
|
||||||
elements: [
|
elements: [
|
||||||
{
|
{
|
||||||
type: 'button',
|
type: 'button',
|
||||||
|
Loading…
Reference in New Issue
Block a user