Skip to content

Commit

Permalink
Merge pull request #202 from shanedewael/fix-message-actions
Browse files Browse the repository at this point in the history
Fix message actions
  • Loading branch information
Shane DeWael authored May 24, 2019
2 parents c4a8e08 + 34e13b6 commit 44ca9d5
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 1 deletion.
152 changes: 152 additions & 0 deletions src/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// tslint:disable:no-implicit-dependencies
import 'mocha';
import { assert } from 'chai';
import { getTypeAndConversation, IncomingEventType } from './helpers';

describe('getTypeAndConversation()', () => {
describe('event types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyEventBody = {
event: {
type: 'app_home_opened',
channel: conversationId,
},
};

it('should find Event type for generic event', () => {
// Act
const typeAndConversation = getTypeAndConversation(dummyEventBody);

// Assert
assert(typeAndConversation.type === IncomingEventType.Event);
assert(typeAndConversation.conversationId === conversationId);
});
});

describe('command types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyCommandBody = {
command: 'COMMAND_NAME',
channel_id: conversationId,
response_url: 'https://hooks.slack.com/commands/RESPONSE_URL',
};

it('should find Command type for generic command', () => {
// Act
const typeAndConversation = getTypeAndConversation(dummyCommandBody);

// Assert
assert(typeAndConversation.type === IncomingEventType.Command);
assert(typeAndConversation.conversationId === conversationId);
});
});

describe('options types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyActionBodies = createFakeOptions(conversationId);

dummyActionBodies.forEach((option) => {
it(`should find Option type for ${option.type}`, () => {
// Act
const typeAndConversation = getTypeAndConversation(option);

// Assert
assert(typeAndConversation.type === IncomingEventType.Options);
assert(typeAndConversation.conversationId === conversationId);
});
});
});

describe('action types', () => {
// Arrange
const conversationId = 'CONVERSATION_ID';
const dummyActionBodies = createFakeActions(conversationId);

dummyActionBodies.forEach((action) => {
it(`should find Action type for ${action.type}`, () => {
// Act
const typeAndConversation = getTypeAndConversation(action);

// Assert
assert(typeAndConversation.type === IncomingEventType.Action);
assert(typeAndConversation.conversationId === conversationId);
});
});
});

describe('invalid events', () => {
// Arrange
const fakeEventBody = {
fake: 'THIS_IS_FAKE',
channel: { id: 'FAKE_CONVERSATION_ID' },
};

it('should not find type for invalid event', () => {
// Act
const typeAndConversation = getTypeAndConversation(fakeEventBody);

// Assert
assert.isEmpty(typeAndConversation);
});
});
});

function createFakeActions(conversationId: string): any[] {
return [
// Body for a message action
{
type: 'message_action',
channel: { id: conversationId },
},
// Body for a dialog submission
{
type: 'dialog_submission',
channel: { id: conversationId },
},
// Body for an action within an interactive message
{
type: 'interactive_message',
channel: { id: conversationId },
actions: [
{
type: 'button',
},
],
},
// Body for an action within a block
{
type: 'block_actions',
channel: { id: conversationId },
actions: [
{
type: 'static_select',
},
],
},
];
}

function createFakeOptions(conversationId: string): any[] {
return [
// Body for an options request in an interactive message
{
type: 'interactive_message',
channel: { id: conversationId },
name: 'OPTIONS_NAME',
},
// Body for an options request in a dialog
{
type: 'dialog_suggestion',
channel: { id: conversationId },
name: 'OPTIONS_NAME',
},
// Body for an action within a block
{
type: 'block_suggestion',
channel: { id: conversationId },
},
];
}
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getTypeAndConversation(body: any): { type?: IncomingEventType, c
(body as SlackOptionsMiddlewareArgs<OptionsSource>['body']).channel.id,
};
}
if (body.actions !== undefined || body.type === 'dialog_submission') {
if (body.actions !== undefined || body.type === 'dialog_submission' || body.type === 'message_action') {
return {
type: IncomingEventType.Action,
conversationId: (body as SlackActionMiddlewareArgs<SlackAction>['body']).channel.id,
Expand Down

0 comments on commit 44ca9d5

Please sign in to comment.