Skip to content

Commit

Permalink
extensive postMessage type tests and tweaks to types to make them pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed Nov 22, 2023
1 parent 6cc5f84 commit 4956cc8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/web-api/src/types/request/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export type ChatPostEphemeralArguments = TokenOverridable & MessageContents & Pa
};

// https://api.slack.com/methods/chat.postMessage
export type ChatPostMessageArguments = TokenOverridable & MessageContents & AsUser & Parse & LinkNames & Icon
& Username & Metadata & ReplyInThread & {
export type ChatPostMessageArguments = TokenOverridable & MessageContents & Parse & LinkNames & Authorship & Metadata
& ReplyInThread & {
/** @description Disable Slack markup parsing by setting to `false`. Enabled by default. */
mrkdwn?: boolean;
/** @description Pass `true` to enable unfurling of primarily text-based content. */
Expand Down
93 changes: 92 additions & 1 deletion packages/web-api/test/types/methods/chat.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ expectError(web.chat.postEphemeral({
user: 'U1234',
attachments: [],
icon_emoji: 'smile',
as_user: true, // cannot set both as_user=true and icon_url
as_user: true, // cannot set both as_user=true and icon_emoji
}));
// -- happy path
expectAssignable<Parameters<typeof web.chat.postEphemeral>>([{
Expand Down Expand Up @@ -171,3 +171,94 @@ expectAssignable<Parameters<typeof web.chat.postEphemeral>>([{
icon_emoji: 'someurl.png',
as_user: false, // ... or with as_user=false
}]);

// chat.postMessage
// -- sad path
expectError(web.chat.postMessage()); // lacking argument
expectError(web.chat.postMessage({})); // empty argument
expectError(web.chat.postMessage({
channel: 'C1234', // missing text/attachments/blocks
}));
expectError(web.chat.postMessage({
text: 'U1234', // missing channel
}));
expectError(web.chat.postMessage({
blocks: [], // missing channel
}));
expectError(web.chat.postMessage({
attachments: [], // missing channel
}));
expectError(web.chat.postMessage({
channel: 'C123',
attachments: [],
icon_url: 'someurl.png',
icon_emoji: 'smile', // cannot use both icon_url and icon_emoji
}));
expectError(web.chat.postMessage({
channel: 'C123',
attachments: [],
icon_url: 'someurl.png',
as_user: true, // cannot set both as_user=true and icon_url
}));
expectError(web.chat.postMessage({
channel: 'C123',
attachments: [],
icon_emoji: 'smile',
as_user: true, // cannot set both as_user=true and icon_emoji
}));
expectError(web.chat.postMessage({
channel: 'C123',
attachments: [],
reply_broadcast: true, // cannot reply_broadcast=true without setting thread_ts
}));
// -- happy path
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
text: '1234.56',
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
blocks: [],
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
attachments: [],
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
attachments: [],
as_user: true, // can pass as_user=true if no icon or username fields set
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
attachments: [],
icon_emoji: 'smile', // icon can be set on its own...
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
attachments: [],
icon_emoji: 'smile',
as_user: false, // ... or with as_user=false
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
attachments: [],
icon_url: 'someurl.png', // icon can be set on its own...
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
attachments: [],
icon_emoji: 'someurl.png',
as_user: false, // ... or with as_user=false
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
text: 'hello',
thread_ts: '1234.56', // can send a threaded message
}]);
expectAssignable<Parameters<typeof web.chat.postMessage>>([{
channel: 'C1234',
text: 'hello',
thread_ts: '1234.56',
reply_broadcast: true, // can send a threaded message and broadcast it, too
}]);

0 comments on commit 4956cc8

Please sign in to comment.