Skip to content

Instantly share code, notes, and snippets.

@christianalfoni
Created April 29, 2021 05:13
Show Gist options
  • Save christianalfoni/34f27917403529c28f0008d186afefb1 to your computer and use it in GitHub Desktop.
Save christianalfoni/34f27917403529c28f0008d186afefb1 to your computer and use it in GitHub Desktop.
type ProviderPitcherRequest<T extends Provider> = T["onRequest"] extends (
request: infer Req,
dispatcher: Dispatcher<any, any>,
client: Client
) => void
? Req
: never;
type ProviderPitcherResult<T extends Provider> = T["onRequest"] extends (
request: any,
dispatcher: Dispatcher<infer Res, any>,
client: Client
) => void
? Res extends { status: PitcherResponseStatus.RESOLVED }
? Res
: never
: never;
type ProviderPitcherError<T extends Provider> = T["onRequest"] extends (
request: any,
dispatcher: Dispatcher<infer Res, any>,
client: Client
) => void
? Res extends { status: PitcherResponseStatus.REJECTED }
? Res
: never
: never;
type ProviderPitcherNotification<T extends Provider> = T["onRequest"] extends (
request: any,
dispatcher: Dispatcher<any, infer Not>,
client: Client
) => void
? Not
: never;
type DispatchArgs<T> = T extends jest.Mock<any, infer A> ? A : never;
async function onRequest<T extends Provider>(
instance: T,
request: ProviderPitcherRequest<T>
): Promise<{
result: jest.Mock<void, [ProviderPitcherResult<T>]>;
error: jest.Mock<void, [ProviderPitcherError<T>]>;
notification: jest.Mock<
void,
[string | Set<string>, ProviderPitcherNotification<T>]
>;
}> {
return {} as any;
}
async () => {
const { result, error, notification } = await onRequest(new FS(), {
method: "fs/createDirectory",
params: {
uri: "",
},
});
expect(notification).toHaveBeenCalledWith<DispatchArgs<typeof notification>>('123', {
method: 'fs/watch',
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment