From 4fa968dc0e74b5206bfcd54e704d180c16b67b08 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Fri, 26 Aug 2022 13:12:24 +0200 Subject: [PATCH] fix(api): add async `mockIPC()` handler signature (#5056) --- .changes/fix-async-mockipc.md | 5 +++++ tooling/api/src/mocks.ts | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .changes/fix-async-mockipc.md diff --git a/.changes/fix-async-mockipc.md b/.changes/fix-async-mockipc.md new file mode 100644 index 000000000..16059b76e --- /dev/null +++ b/.changes/fix-async-mockipc.md @@ -0,0 +1,5 @@ +--- +"api": patch +--- + +Update `mockIPC()` handler signature to allow async handler functions. diff --git a/tooling/api/src/mocks.ts b/tooling/api/src/mocks.ts index 4273a8135..a5d3e2891 100644 --- a/tooling/api/src/mocks.ts +++ b/tooling/api/src/mocks.ts @@ -35,10 +35,31 @@ interface IPCMessage { * }) * ``` * + * The callback function can also return a Promise: + * ```js + * import { mockIPC, clearMocks } from "@tauri-apps/api/mocks" + * import { invoke } from "@tauri-apps/api/tauri" + * + * afterEach(() => { + * clearMocks() + * }) + * + * test("mocked command", () => { + * mockIPC((cmd, args) => { + * if(cmd === "get_data") { + * return fetch("https://example.com/data.json") + * .then((response) => response.json()) + * } + * }); + * + * expect(invoke('get_data')).resolves.toBe({ foo: 'bar' }); + * }) + * ``` + * * @param cb */ export function mockIPC( - cb: (cmd: string, args: Record) => any + cb: (cmd: string, args: Record) => any | Promise ): void { // eslint-disable-next-line @typescript-eslint/no-misused-promises window.__TAURI_IPC__ = async ({