Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message actions #202

Merged
merged 3 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions src/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// 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,
event_ts: 'EVENT_TS',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think type or event_ts are technically needed for the tests to succeed. right?

},
};

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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think response_url is needed for the test to succeed.

};

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 },
callback_id: 'CALLBACK_ID',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think a callback_id is necessary.

},
// Body for a dialog submission
{
type: 'dialog_submission',
channel: { id: conversationId },
callback_id: 'CALLBACK_ID',
submission: { KEY: 'VALUE' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think a callback_id or a submission are necessary.

},
// Body for an action within an interactive message
{
type: 'interactive_message',
channel: { id: conversationId },
callback_id: 'CALLBACK_ID',
actions: [
{
type: 'button',
name: 'NAME',
value: 'VALUE',
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think a callback_id or a real value for the first action (as long as there is something in the array) are necessary.

],
},
// Body for an action within a block
{
type: 'block_actions',
channel: { id: conversationId },
actions: [
{
type: 'static_select',
selected_option: {
text: {
type: 'plain_text',
text: 'ELEMENT_TEXT',
},
value: 'VALUE',
},
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think a real value for the first action (as long as there is something in the array) is necessary.

],
},
];
}

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 },
action_id: 'ACTION_ID',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think the action_id is necessary.

},
];
}
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