Skip to content

Commit

Permalink
todos, context type tests in message type tests, cleaning up shortcut…
Browse files Browse the repository at this point in the history
… type tests.
  • Loading branch information
Filip Maj committed Sep 17, 2024
1 parent 8ebda18 commit abfe302
Show file tree
Hide file tree
Showing 5 changed files with 719 additions and 1,092 deletions.
2 changes: 1 addition & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ export default class App<AppCustomContext extends StringIndexed = StringIndexed>
public message<MiddlewareCustomContext extends StringIndexed = StringIndexed>(
...patternsOrMiddleware: (string | RegExp | MessageEventMiddleware<AppCustomContext & MiddlewareCustomContext>)[]
): void;
// TODO: expose a type parameter for overriding the MessageEvent type (just like shortcut() and action() does) https://github.com/slackapi/bolt-js/issues/796
public message<MiddlewareCustomContext extends StringIndexed = StringIndexed>(
...patternsOrMiddleware: (string | RegExp | MessageEventMiddleware<AppCustomContext & MiddlewareCustomContext>)[]
): void {
Expand Down Expand Up @@ -719,7 +720,6 @@ export default class App<AppCustomContext extends StringIndexed = StringIndexed>
return;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _listeners = listeners as any; // FIXME: workaround for TypeScript 4.7 breaking changes
this.listeners.push([
onlyShortcuts,
Expand Down
16 changes: 15 additions & 1 deletion test/types/message.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectError, expectNotType, expectType } from 'tsd';
import { expectAssignable, expectError, expectNotType, expectType } from 'tsd';
import type {
AllMessageEvents,
BotMessageEvent,
Expand Down Expand Up @@ -95,3 +95,17 @@ app.message(async ({ message }) => {

await Promise.resolve(message);
});

interface MyContext {
doesnt: 'matter';
}
// Ensure custom context assigned to individual middleware is honoured
app.message<MyContext>(async ({ context }) => {
expectAssignable<MyContext>(context);
});

// Ensure custom context assigned to the entire app is honoured
const typedContextApp = new App<MyContext>();
typedContextApp.message(async ({ context }) => {
expectAssignable<MyContext>(context);
});
58 changes: 23 additions & 35 deletions test/types/shortcut.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,39 @@
import { expectError, expectType } from 'tsd';
import type { GlobalShortcut, MessageShortcut, SayFn } from '../..';
import type { GlobalShortcut, MessageShortcut, SayFn, SlackShortcut } from '../..';
import App from '../../src/App';

const app = new App({ token: 'TOKEN', signingSecret: 'Signing Secret' });

// calling shortcut method with incorrect an type constraint value should not work
// calling shortcut method with incorrect type constraint value should not work
expectError(
app.shortcut({ type: 'Something wrong' }, async ({ shortcut }) => {
await Promise.resolve(shortcut);
}),
);

// Shortcut in listener should be - MessageShortcut
expectType<void>(
app.shortcut({ type: 'message_action' }, async ({ shortcut }) => {
expectType<MessageShortcut>(shortcut);
await Promise.resolve(shortcut);
}),
);
app.shortcut({ type: 'message_action' }, async ({ shortcut, say }) => {
// Shortcut in listener should be MessageShortcut if type:message_action
expectType<MessageShortcut>(shortcut);
expectType<SayFn>(say);
});

// If shortcut is parameterized with MessageShortcut, shortcut argument in callback should be type MessageShortcut
expectType<void>(
app.shortcut<MessageShortcut>({}, async ({ shortcut }) => {
expectType<MessageShortcut>(shortcut);
await Promise.resolve(shortcut);
}),
);
app.shortcut<MessageShortcut>({}, async ({ shortcut, say }) => {
expectType<MessageShortcut>(shortcut);
expectType<SayFn>(say);
});

expectType<void>(
app.shortcut({}, async ({ shortcut, say }) => {
expectType<SayFn | undefined>(say);
await Promise.resolve(shortcut);
}),
);

// If shortcut is parameterized with MessageShortcut, say argument in callback should be type SayFn
expectType<void>(
app.shortcut<MessageShortcut>({}, async ({ shortcut, say }) => {
expectType<SayFn>(say);
await Promise.resolve(shortcut);
}),
);
// If the constraint is unspecific, say may be undefined and the shortcut is the more general SlackShortcut type
app.shortcut({}, async ({ shortcut, say }) => {
expectType<SlackShortcut>(shortcut);
expectType<SayFn | undefined>(say);
});

// If shortcut is parameterized with GlobalShortcut, say argument in callback should be type undefined
expectType<void>(
app.shortcut<GlobalShortcut>({}, async ({ shortcut, say }) => {
expectType<undefined>(say);
await Promise.resolve(shortcut);
}),
);
app.shortcut<GlobalShortcut>({}, async ({ shortcut, say }) => {
expectType<undefined>(say);
expectType<GlobalShortcut>(shortcut);
});

// TODO: test the Shortcut and Constraints type parameters and how they can rely on each other.
// relates to https://github.com/slackapi/bolt-js/issues/796; proof out how the Shortcut type parameter can provide nice typing utilities for developers
File renamed without changes.
Loading

0 comments on commit abfe302

Please sign in to comment.