Skip to content

Commit

Permalink
Merge pull request slackapi#357 from seratch/more-tests
Browse files Browse the repository at this point in the history
Add more unit tests for built-in middleware and ExpressReceiver
  • Loading branch information
seratch authored Jan 3, 2020
2 parents 9ff92ed + 4e5084e commit 3d59cf7
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 53 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"build": "tsc",
"build:clean": "shx rm -rf ./dist ./coverage ./.nyc_output",
"lint": "tslint --project .",
"test-lint": "tslint \"src/**/*.spec.ts\" && tslint \"src/test-helpers.ts\"",
"mocha": "nyc mocha --config .mocharc.json \"src/**/*.spec.ts\"",
"test": "npm run lint && npm run mocha && npm run test:integration",
"test": "npm run lint && npm run test-lint && npm run mocha && npm run test:integration",
"test:integration": "cd integration-tests && npm install && npm test",
"coverage": "codecov"
},
Expand Down
74 changes: 62 additions & 12 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ErrorCode } from './errors';
import { Receiver, ReceiverEvent, SayFn, NextMiddleware } from './types';
import { ConversationStore } from './conversation-store';
import { LogLevel } from '@slack/logger';
import { ViewConstraints } from './App';

describe('App', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -157,31 +158,33 @@ describe('App', () => {
});
});
it('with clientOptions', async () => {
const fakeConstructor = sinon.fake()
const fakeConstructor = sinon.fake();
const overrides = mergeOverrides(
withNoopAppMetadata(),
{
'@slack/web-api': {
WebClient: class {
constructor() {
fakeConstructor(...arguments)
fakeConstructor(...arguments);
}
},
}
}
)
},
},
);
// tslint:disable-next-line: variable-name
const App = await importApp(overrides);

const clientOptions = { slackApiUrl: 'proxy.slack.com' };
new App({ authorize: noopAuthorize, signingSecret: '', logLevel: LogLevel.ERROR, clientOptions });
// tslint:disable-next-line: no-unused-expression
new App({ clientOptions, authorize: noopAuthorize, signingSecret: '', logLevel: LogLevel.ERROR });

assert.ok(fakeConstructor.called);

const [token, options] = fakeConstructor.lastCall.args;
assert.strictEqual(undefined, token, 'token should be undefined');
assert.strictEqual(clientOptions.slackApiUrl, options.slackApiUrl);
assert.strictEqual(LogLevel.ERROR, options.logLevel, 'override logLevel');
})
});
// TODO: tests for ignoreSelf option
// TODO: tests for logger and logLevel option
// TODO: tests for providing botId and botUserId options
Expand Down Expand Up @@ -322,15 +325,15 @@ describe('App', () => {
const dummyChannelId = 'CHANNEL_ID';
let overrides: Override;

function buildOverrides(secondOverride: Override) {
function buildOverrides(secondOverride: Override): Override {
fakeReceiver = createFakeReceiver();
fakeErrorHandler = sinon.fake();
dummyAuthorizationResult = { botToken: '', botId: '' };
overrides = mergeOverrides(
withNoopAppMetadata(),
secondOverride,
withMemoryStore(sinon.fake()),
withConversationContext(sinon.fake.returns(noopMiddleware))
withConversationContext(sinon.fake.returns(noopMiddleware)),
);
return overrides;
}
Expand All @@ -357,7 +360,7 @@ describe('App', () => {
body: {
type: 'block_actions',
actions: [{
action_id: 'block_action_id'
action_id: 'block_action_id',
}],
channel: {},
user: {},
Expand Down Expand Up @@ -461,6 +464,22 @@ describe('App', () => {
respond: noop,
ack: noop,
},
{
body: {
type: 'event_callback',
token: 'XXYYZZ',
team_id: 'TXXXXXXXX',
api_app_id: 'AXXXXXXXXX',
event: {
type: 'message',
event_ts: '1234567890.123456',
user: 'UXXXXXXX1',
text: 'hello friends!',
},
},
respond: noop,
ack: noop,
},
];
}

Expand All @@ -475,11 +494,19 @@ describe('App', () => {
const dummyReceiverEvents = createReceiverEvents();

// Act
const app = new App({ receiver: fakeReceiver, authorize: sinon.fake.resolves(dummyAuthorizationResult) });
const fakeLogger = createFakeLogger();
const app = new App({
logger: fakeLogger,
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});

app.use((_args) => { ackFn(); });
app.action('block_action_id', ({ }) => { actionFn(); });
app.action({ callback_id: 'message_action_callback_id' }, ({ }) => { actionFn(); });
app.action({ type: 'message_action', callback_id: 'another_message_action_callback_id' }, ({ }) => { actionFn(); });
app.action(
{ type: 'message_action', callback_id: 'another_message_action_callback_id' },
({ }) => { actionFn(); });
app.action({ type: 'message_action', callback_id: 'does_not_exist' }, ({ }) => { actionFn(); });
app.action({ callback_id: 'interactive_message_callback_id' }, ({ }) => { actionFn(); });
app.action({ callback_id: 'dialog_submission_callback_id' }, ({ }) => { actionFn(); });
Expand All @@ -488,6 +515,29 @@ describe('App', () => {
app.options('external_select_action_id', ({ }) => { optionsFn(); });
app.options({ callback_id: 'dialog_suggestion_callback_id' }, ({ }) => { optionsFn(); });

app.event('app_home_opened', ({ }) => { /* noop */ });
app.message('hello', ({ }) => { /* noop */ });
app.command('/echo', ({ }) => { /* noop */ });

// invalid view constraints
const invalidViewConstraints1 = {
callback_id: 'foo',
type: 'view_submission',
unknown_key: 'should be detected',
} as any as ViewConstraints;
app.view(invalidViewConstraints1, ({ }) => { /* noop */ });
assert.isTrue(fakeLogger.error.called);

fakeLogger.error = sinon.fake();

const invalidViewConstraints2 = {
callback_id: 'foo',
type: undefined,
unknown_key: 'should be detected',
} as any as ViewConstraints;
app.view(invalidViewConstraints2, ({ }) => { /* noop */ });
assert.isTrue(fakeLogger.error.called);

app.error(fakeErrorHandler);
dummyReceiverEvents.forEach(dummyEvent => fakeReceiver.emit('message', dummyEvent));
await delay();
Expand Down
Loading

0 comments on commit 3d59cf7

Please sign in to comment.