Skip to content

Commit

Permalink
Fix #1478 ack() is not accessible in global middleware in TypeScript (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jun 10, 2022
1 parent e1f7728 commit 1a5e001
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
76 changes: 76 additions & 0 deletions src/App-basic-features.spec.ts
Expand Up @@ -1081,6 +1081,82 @@ describe('App basic features', () => {
assert.equal(fakeErrorHandler.callCount, dummyReceiverEvents.length);
});
});

describe('ack()', () => {
it('should be available in middleware/listener args', async () => {
// Arrange
const MockApp = await importApp(overrides);
const fakeLogger = createFakeLogger();
const app = new MockApp({
logger: fakeLogger,
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});
app.use(async ({ ack, next }) => {
if (ack) {
// this should be called even if app.view listeners do not exist
await ack();
return;
}
fakeLogger.info('Events API');
await next();
});

app.event('app_home_opened', async ({ logger, event }) => {
logger.debug(event);
});

let ackInMiddlewareCalled = false;

const receiverEvents = [
{
body: {
type: 'event_callback',
token: 'XXYYZZ',
team_id: 'TXXXXXXXX',
api_app_id: 'AXXXXXXXXX',
event: {
type: 'app_home_opened',
event_ts: '1234567890.123456',
user: 'UXXXXXXX1',
text: 'hello friends!',
tab: 'home',
view: {},
},
},
respond: noop,
ack: noop,
},
{
body: {
type: 'view_submission',
team: {},
user: {},
view: {
id: 'V111',
type: 'modal',
callback_id: 'view-id',
state: {},
title: {},
close: {},
submit: {},
},
},
respond: noop,
ack: async () => {
ackInMiddlewareCalled = true;
},
},
];

// Act
await Promise.all(receiverEvents.map((event) => fakeReceiver.sendEvent(event)));

// Assert
assert.isTrue(fakeLogger.info.called);
assert.isTrue(ackInMiddlewareCalled);
});
});
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/types/events/index.ts
Expand Up @@ -32,6 +32,8 @@ export interface SlackEventMiddlewareArgs<EventType extends string = string> {
message: EventType extends 'message' ? this['payload'] : never;
body: EnvelopedEvent<this['payload']>;
say: WhenEventHasChannelContext<this['payload'], SayFn>;
// Add `ack` as undefined for global middleware in TypeScript
ack: undefined;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion types-tests/middleware.test-d.ts
Expand Up @@ -9,4 +9,11 @@ app.use(async (args) => {
});
app.use(async (args) => {
onlyCommands(args);
});
});
app.use(async ({ ack, next }) => {
if (ack) {
await ack();
return;
}
await next();
});

0 comments on commit 1a5e001

Please sign in to comment.