-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
polish up type tests, finish moving context "unit" tests over to type…
… tests.
- Loading branch information
Filip Maj
committed
Sep 18, 2024
1 parent
4dac298
commit 4a7a0b7
Showing
6 changed files
with
165 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
import { expectType } from 'tsd'; | ||
import { expectAssignable, expectType } from 'tsd'; | ||
import type { SlashCommand } from '../..'; | ||
import App from '../../src/App'; | ||
|
||
const app = new App({ token: 'TOKEN', signingSecret: 'Signing Secret' }); | ||
|
||
expectType<void>( | ||
app.command('/hello', async ({ command }) => { | ||
expectType<SlashCommand>(command); | ||
await Promise.resolve(command); | ||
}), | ||
); | ||
app.command('/hello', async ({ command }) => { | ||
expectType<SlashCommand>(command); | ||
}); | ||
|
||
interface MyContext { | ||
doesnt: 'matter'; | ||
} | ||
// Ensure custom context assigned to individual middleware is honoured | ||
app.command<MyContext>('/action', async ({ context }) => { | ||
expectAssignable<MyContext>(context); | ||
}); | ||
|
||
// Ensure custom context assigned to the entire app is honoured | ||
const typedContextApp = new App<MyContext>(); | ||
typedContextApp.command('/action', async ({ context }) => { | ||
expectAssignable<MyContext>(context); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,102 @@ | ||
import type { Option } from '@slack/types'; | ||
import { expectError, expectType } from 'tsd'; | ||
import { expectAssignable, expectType } from 'tsd'; | ||
import App from '../../src/App'; | ||
import type { BlockSuggestion, DialogSuggestion, InteractiveMessageSuggestion, SlackOptions } from '../..'; | ||
import type { | ||
AckFn, | ||
BlockOptions, | ||
BlockSuggestion, | ||
DialogOptions, | ||
DialogOptionGroups, | ||
DialogSuggestion, | ||
InteractiveMessageSuggestion, | ||
MessageOptions, | ||
OptionGroups, | ||
} from '../..'; | ||
|
||
const app = new App({ token: 'TOKEN', signingSecret: 'Signing Secret' }); | ||
|
||
const blockSuggestionOptions: Option[] = [ | ||
{ | ||
text: { | ||
type: 'plain_text', | ||
text: 'foo', | ||
}, | ||
value: 'bar', | ||
}, | ||
]; | ||
|
||
// set the default to block_suggestion | ||
expectType<void>( | ||
app.options('action-id-or-callback-id', async ({ options, ack }) => { | ||
expectType<BlockSuggestion>(options); | ||
// biome-ignore lint/suspicious/noExplicitAny: TODO: should the callback ID be any? seems wrong | ||
expectType<any>(options.callback_id); | ||
options.block_id; | ||
options.action_id; | ||
// https://github.com/slackapi/bolt-js/issues/720 | ||
await ack({ options: blockSuggestionOptions }); | ||
await Promise.resolve(options); | ||
}), | ||
); | ||
app.options('action-id-or-callback-id', async ({ options, ack }) => { | ||
// TODO: should BlockSuggestion belong in types package? if so, assertions on its contents should also move to types package. | ||
// defaults options to block_suggestion | ||
expectType<BlockSuggestion>(options); | ||
// biome-ignore lint/suspicious/noExplicitAny: TODO: should the callback ID be any? seems wrong | ||
expectType<any>(options.callback_id); | ||
options.block_id; | ||
options.action_id; | ||
// ack should allow either BlockOptions or OptionGroups | ||
// https://github.com/slackapi/bolt-js/issues/720 | ||
expectAssignable<AckFn<BlockOptions>>(ack); | ||
expectAssignable<AckFn<OptionGroups<BlockOptions>>>(ack); | ||
}); | ||
|
||
// block_suggestion | ||
expectType<void>( | ||
app.options<'block_suggestion'>({ action_id: 'a' }, async ({ options, ack }) => { | ||
expectType<BlockSuggestion>(options); | ||
// https://github.com/slackapi/bolt-js/issues/720 | ||
await ack({ options: blockSuggestionOptions }); | ||
await Promise.resolve(options); | ||
}), | ||
); | ||
// FIXME: app.options({ type: 'block_suggestion', action_id: 'a' } does not work | ||
// FIXME: app.options({ type: 'block_suggestion', action_id: 'a' } does not constrain the arguments of the handler down to `block_suggestion` | ||
|
||
// interactive_message (attachments) | ||
expectType<void>( | ||
app.options<'interactive_message'>({ callback_id: 'a' }, async ({ options, ack }) => { | ||
expectType<InteractiveMessageSuggestion>(options); | ||
ack({ options: blockSuggestionOptions }); | ||
await Promise.resolve(options); | ||
}), | ||
); | ||
app.options<'interactive_message'>({ callback_id: 'a' }, async ({ options, ack }) => { | ||
expectType<InteractiveMessageSuggestion>(options); | ||
// ack should allow either MessageOptions or OptionGroups | ||
// https://github.com/slackapi/bolt-js/issues/720 | ||
expectAssignable<AckFn<MessageOptions>>(ack); | ||
expectAssignable<AckFn<OptionGroups<MessageOptions>>>(ack); | ||
}); | ||
|
||
expectType<void>( | ||
app.options({ type: 'interactive_message', callback_id: 'a' }, async ({ options, ack }) => { | ||
// FIXME: the type should be OptionsRequest<'interactive_message'> | ||
expectType<SlackOptions>(options); | ||
// https://github.com/slackapi/bolt-js/issues/720 | ||
expectError(ack({ options: blockSuggestionOptions })); | ||
await Promise.resolve(options); | ||
}), | ||
); | ||
// FIXME: app.options({ type: 'interactive_message', callback_id: 'a' } does not constrain the arguments of the handler down to `interactive_message` | ||
|
||
// dialog_suggestion (dialog) | ||
expectType<void>( | ||
app.options<'dialog_suggestion'>({ callback_id: 'a' }, async ({ options, ack }) => { | ||
expectType<DialogSuggestion>(options); | ||
// https://github.com/slackapi/bolt-js/issues/720 | ||
expectError(ack({ options: blockSuggestionOptions })); | ||
await Promise.resolve(options); | ||
}), | ||
); | ||
// FIXME: app.options({ type: 'dialog_suggestion', callback_id: 'a' } does not work | ||
app.options<'dialog_suggestion'>({ callback_id: 'a' }, async ({ options, ack }) => { | ||
expectType<DialogSuggestion>(options); | ||
// ack should allow either MessageOptions or OptionGroups | ||
// https://github.com/slackapi/bolt-js/issues/720 | ||
expectAssignable<AckFn<DialogOptions>>(ack); | ||
expectAssignable<AckFn<DialogOptionGroups<DialogOptions>>>(ack); | ||
}); | ||
// FIXME: app.options({ type: 'dialog_suggestion', callback_id: 'a' } does not constrain the arguments of the handler down to `dialog_sggestion` | ||
|
||
const db = { | ||
get: (_teamId: string) => { | ||
return [{ label: 'l', value: 'v' }]; | ||
}, | ||
}; | ||
|
||
expectType<void>( | ||
// Taken from https://slack.dev/bolt-js/concepts#options | ||
// Example of responding to an external_select options request | ||
app.options('external_action', async ({ options, ack }) => { | ||
// Get information specific to a team or channel | ||
// TODO: modified to satisfy TS compiler; should team be optional? | ||
const results = options.team != null ? db.get(options.team.id) : []; | ||
|
||
if (results) { | ||
// (modified to satisfy TS compiler) | ||
const options: Option[] = []; | ||
// Collect information in options array to send in Slack ack response | ||
for (const result of results) { | ||
options.push({ | ||
text: { | ||
type: 'plain_text', | ||
text: result.label, | ||
}, | ||
value: result.value, | ||
}); | ||
} | ||
// Taken from https://slack.dev/bolt-js/concepts#options | ||
// Example of responding to an external_select options request | ||
app.options('external_action', async ({ options, ack }) => { | ||
// Get information specific to a team or channel | ||
// TODO: modified to satisfy TS compiler; should team be optional? | ||
const results = options.team != null ? db.get(options.team.id) : []; | ||
|
||
await ack({ | ||
options: options, | ||
if (results) { | ||
// (modified to satisfy TS compiler) | ||
const options: Option[] = []; | ||
// Collect information in options array to send in Slack ack response | ||
for (const result of results) { | ||
options.push({ | ||
text: { | ||
type: 'plain_text', | ||
text: result.label, | ||
}, | ||
value: result.value, | ||
}); | ||
} else { | ||
await ack(); | ||
} | ||
}), | ||
); | ||
|
||
await ack({ | ||
options: options, | ||
}); | ||
} else { | ||
await ack(); | ||
} | ||
}); | ||
|
||
interface MyContext { | ||
doesnt: 'matter'; | ||
} | ||
// Ensure custom context assigned to individual middleware is honoured | ||
app.options<'block_suggestion', MyContext>('suggest', async ({ context }) => { | ||
expectAssignable<MyContext>(context); | ||
}); | ||
|
||
// Ensure custom context assigned to the entire app is honoured | ||
const typedContextApp = new App<MyContext>(); | ||
typedContextApp.options('suggest', async ({ context }) => { | ||
expectAssignable<MyContext>(context); | ||
}); |
Oops, something went wrong.