Skip to content

Commit

Permalink
adding context type tests to shortcut type tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed Sep 17, 2024
1 parent abfe302 commit 61f9e6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 145 deletions.
16 changes: 15 additions & 1 deletion test/types/shortcut.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectError, expectType } from 'tsd';
import { expectAssignable, expectError, expectType } from 'tsd';
import type { GlobalShortcut, MessageShortcut, SayFn, SlackShortcut } from '../..';
import App from '../../src/App';

Expand Down Expand Up @@ -37,3 +37,17 @@ app.shortcut<GlobalShortcut>({}, async ({ shortcut, say }) => {

// 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

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

// Ensure custom context assigned to the entire app is honoured
const typedContextApp = new App<MyContext>();
typedContextApp.shortcut('callback_id', async ({ context }) => {
expectAssignable<MyContext>(context);
});
144 changes: 0 additions & 144 deletions test/unit/App/context-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,150 +22,6 @@ describe('context typing', () => {
it('message should handle middleware context', async () => {
const MockApp = await importApp();
const app = new MockApp({ receiver, authorize: noop });
it('shortcut should handle global and middleware context', async () => {
const MockApp = await importApp();
const app = new MockApp<GlobalContext>({ receiver, authorize: noop });

// Shortcut with RegExp callbackId is aware of global context and passes context to all middleware
app.shortcut(
/callback_id/,
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;
},
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;
},
);

// Shortcut with string callbackId is aware of global context and passes context to all middleware
app.shortcut(
'callback_id',
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;
},
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;
},
);

// Shortcut with RegExp callbackId is aware of global and middleware context and passes context to all middleware
app.shortcut<SlackShortcut, MiddlewareContext>(
/callback_id/,
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;

const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;

const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
);

// Shortcut with string callbackId is aware of global and middleware context and passes context to all middleware
app.shortcut<SlackShortcut, MiddlewareContext>(
'callback_id',
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;

const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;

const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
);

// Shortcut with constraints is aware of global context and passes context to all middleware
app.shortcut(
{ type: 'shortcut' },
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;
},
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;
},
);

// Shortcut with constraints is aware of global and middleware context and passes context to all middleware
app.shortcut<SlackShortcut, ShortcutConstraints<SlackShortcut>, MiddlewareContext>(
{ type: 'shortcut' },
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;

const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
async ({ context }) => {
const globalCheck = {} as IfAnyThenElse<(typeof context)['globalContextKey'], never, valid>;
globalCheck.valid = true;

const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
);
});

it('shortcut should handle middleware context', async () => {
const MockApp = await importApp();
const app = new MockApp({ receiver, authorize: noop });

// Shortcut with RegExp callbackId is aware of middleware context and passes context to all middleware
app.shortcut<SlackShortcut, MiddlewareContext>(
/callback_id/,
async ({ context }) => {
const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
async ({ context }) => {
const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
);

// Shortcut with string callbackId is aware of middleware context and passes context to all middleware
app.shortcut<SlackShortcut, MiddlewareContext>(
'callback_id',
async ({ context }) => {
const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
async ({ context }) => {
const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
);

// Shortcut with constraints is aware of middleware context and passes context to all middleware
app.shortcut<SlackShortcut, ShortcutConstraints, MiddlewareContext>(
{ type: 'shortcut' },
async ({ context }) => {
const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
async ({ context }) => {
const middlewareCheck = {} as IfAnyThenElse<(typeof context)['middlewareContextKey'], never, valid>;
middlewareCheck.valid = true;
},
);
});

it('action should handle global and middleware context', async () => {
const MockApp = await importApp();
Expand Down

0 comments on commit 61f9e6c

Please sign in to comment.