-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 284 regex events #763
Changes from 5 commits
b0378dd
5a07521
7e59d38
cc5ec8d
b5f62d2
9e441a5
859d2d4
3b1a677
6cce7cd
0c81c26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ import { | |
} from '../types'; | ||
import { onlyCommands, onlyEvents, matchCommandName, matchEventType, subtype } from './builtin'; | ||
import { SlashCommand } from '../types/command'; | ||
import { AppMentionEvent } from '../types/events'; | ||
import { AppMentionEvent, AppHomeOpenedEvent } from '../types/events'; | ||
import { GenericMessageEvent } from '../types/events/message-events'; | ||
import { WebClient } from '@slack/web-api'; | ||
import { Logger } from '@slack/logger'; | ||
|
@@ -532,7 +532,7 @@ describe('onlyEvents', () => { | |
const args: SlackEventMiddlewareArgs<'app_mention'> & { event?: SlackEvent } = { | ||
payload: appMentionEvent, | ||
event: appMentionEvent, | ||
message: null as never, // a bit hackey to sartisfy TS compiler | ||
message: null as never, // a bit hackey to satisfy TS compiler as 'null' cannot be assigned to type 'never' | ||
body: { | ||
token: 'token-value', | ||
team_id: 'T1234567', | ||
|
@@ -582,7 +582,7 @@ describe('matchEventType', () => { | |
return { | ||
payload: appMentionEvent, | ||
event: appMentionEvent, | ||
message: null as never, // a bit hackey to sartisfy TS compiler | ||
message: null as never, // a bit hackey to satisfy TS compiler as 'null' cannot be assigned to type 'never' | ||
body: { | ||
token: 'token-value', | ||
team_id: 'T1234567', | ||
|
@@ -597,6 +597,27 @@ describe('matchEventType', () => { | |
}; | ||
} | ||
|
||
function buildArgsAppHomeOpened(): SlackEventMiddlewareArgs<'app_home_opened'> & { | ||
event?: SlackEvent; | ||
} { | ||
return { | ||
payload: appHomeOpenedEvent, | ||
event: appHomeOpenedEvent, | ||
message: null as never, // a bit hackey to satisfy TS compiler as 'null' cannot be assigned to type 'never' | ||
body: { | ||
token: 'token-value', | ||
team_id: 'T1234567', | ||
api_app_id: 'A1234567', | ||
event: appHomeOpenedEvent, | ||
type: 'event_callback', | ||
event_id: 'event-id-value', | ||
event_time: 123, | ||
authed_users: [], | ||
}, | ||
say: sayNoop, | ||
}; | ||
} | ||
|
||
it('should detect valid requests', async () => { | ||
const fakeNext = sinon.fake(); | ||
await matchEventType('app_mention')({ | ||
|
@@ -609,6 +630,30 @@ describe('matchEventType', () => { | |
assert.isTrue(fakeNext.called); | ||
}); | ||
|
||
it('should detect valid RegExp requests with app_mention', async () => { | ||
const fakeNext = sinon.fake(); | ||
await matchEventType(/app_mention|app_home_opened/)({ | ||
logger, | ||
client, | ||
next: fakeNext, | ||
context: {}, | ||
...buildArgs(), | ||
}); | ||
assert.isTrue(fakeNext.called); | ||
}); | ||
|
||
it('should detect valid RegExp requests with app_home_opened', async () => { | ||
const fakeNext = sinon.fake(); | ||
await matchEventType(/app_mention|app_home_opened/)({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the difference between this test case and the one above it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no big difference, I just wanted to cover tests for the regex(/app_mention|app_home_opened/) specific to app_home_opened. We can remove this test case if it’s unnecessary to have redundant tests. |
||
logger, | ||
client, | ||
next: fakeNext, | ||
context: {}, | ||
...buildArgsAppHomeOpened(), | ||
}); | ||
assert.isTrue(fakeNext.called); | ||
}); | ||
|
||
it('should skip other requests', async () => { | ||
const fakeNext = sinon.fake(); | ||
await matchEventType('app_home_opened')({ | ||
|
@@ -620,6 +665,18 @@ describe('matchEventType', () => { | |
}); | ||
assert.isFalse(fakeNext.called); | ||
}); | ||
|
||
it('should skip other requests for RegExp', async () => { | ||
const fakeNext = sinon.fake(); | ||
await matchEventType(/foo/)({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call to have a test case for both matching and not matching. |
||
logger, | ||
client, | ||
next: fakeNext, | ||
context: {}, | ||
...buildArgs(), | ||
}); | ||
assert.isFalse(fakeNext.called); | ||
}); | ||
}); | ||
|
||
describe('subtype', () => { | ||
|
@@ -749,6 +806,21 @@ const appMentionEvent: AppMentionEvent = { | |
thread_ts: '123.123', | ||
}; | ||
|
||
const appHomeOpenedEvent: AppHomeOpenedEvent = { | ||
type: 'app_home_opened', | ||
user: 'USERNAME', | ||
channel: 'U1234567', | ||
tab: 'home', | ||
view: { | ||
type: 'home', | ||
blocks: [], | ||
clear_on_close: false, | ||
notify_on_close: false, | ||
external_id: '', | ||
}, | ||
event_ts: '123.123', | ||
}; | ||
|
||
const botMessageEvent: MessageEvent = { | ||
type: 'message', | ||
subtype: 'bot_message', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect! this is the intended usage and i'm glad you specifically have a test that illustrates it.