Skip to content

Commit

Permalink
command routing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj committed Sep 19, 2024
1 parent 20e1e1e commit d3095a6
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test/unit/App/routing-command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import sinon, { type SinonSpy } from 'sinon';
import {
FakeReceiver,
type Override,
createFakeLogger,
createDummyCommandMiddlewareArgs,
importApp,
mergeOverrides,
noopMiddleware,
noopVoid,
withConversationContext,
withMemoryStore,
withNoopAppMetadata,
withNoopWebClient,
} from '../helpers';
import type App from '../../../src/App';

function buildOverrides(secondOverrides: Override[]): Override {
return mergeOverrides(
withNoopAppMetadata(),
withNoopWebClient(),
...secondOverrides,
withMemoryStore(sinon.fake()),
withConversationContext(sinon.fake.returns(noopMiddleware)),
);
}

describe('App command() routing', () => {
let fakeReceiver: FakeReceiver;
let fakeHandler: SinonSpy;
let dummyAuthorizationResult: { botToken: string; botId: string };
let MockApp: Awaited<ReturnType<typeof importApp>>;
let app: App;

beforeEach(async () => {
fakeReceiver = new FakeReceiver();
fakeHandler = sinon.fake();
dummyAuthorizationResult = { botToken: '', botId: '' };
MockApp = await importApp(buildOverrides([]));
app = new MockApp({
logger: createFakeLogger(),
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});
});

it('should route a command to a handler registered with `command(string)` if command name matches', async () => {
app.command('/yo', fakeHandler);
await fakeReceiver.sendEvent({
...createDummyCommandMiddlewareArgs({ command: '/yo' }),
ack: noopVoid,
});
sinon.assert.called(fakeHandler);
});
it('should route a command to a handler registered with `command(RegExp)` if comand name matches', async () => {
app.command(/hi/, fakeHandler);
await fakeReceiver.sendEvent({
...createDummyCommandMiddlewareArgs({ command: '/hiya' }),
ack: noopVoid,
});
sinon.assert.called(fakeHandler);
});
});
31 changes: 31 additions & 0 deletions test/unit/helpers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
RespondFn,
SayFn,
SlackActionMiddlewareArgs,
SlackCommandMiddlewareArgs,
SlackEventMiddlewareArgs,
SlackOptionsMiddlewareArgs,
SlackShortcutMiddlewareArgs,
Expand All @@ -19,6 +20,7 @@ import type {
ViewSubmitAction,
ViewOutput,
BlockSuggestion,
SlashCommand,
} from '../../../src/types';

const ts = '1234.56';
Expand Down Expand Up @@ -80,6 +82,35 @@ export function createDummyAppMentionEventMiddlewareArgs(
};
}

interface DummyCommandOverride {
command?: string;
slashCommand?: SlashCommand;
}
export function createDummyCommandMiddlewareArgs(commandOverrides?: DummyCommandOverride): SlackCommandMiddlewareArgs {
const payload: SlashCommand = commandOverrides?.slashCommand || {
token,
command: commandOverrides?.command || '/slash',
text: 'yo',
response_url: 'https://slack.com',
trigger_id: ts,
user_id: user,
user_name: 'filmaj',
team_id: team,
team_domain: 'slack.com',
channel_id: channel,
channel_name: '#random',
api_app_id: app_id,
};
return {
payload,
command: payload,
body: payload,
respond,
say,
ack: () => Promise.resolve(),
};
}

interface DummyBlockActionOverride {
action_id?: string;
block_id?: string;
Expand Down

0 comments on commit d3095a6

Please sign in to comment.