Skip to content

Commit 4fa968d

Browse files
fix(api): add async mockIPC() handler signature (#5056)
1 parent e9f1e62 commit 4fa968d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

.changes/fix-async-mockipc.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Update `mockIPC()` handler signature to allow async handler functions.

tooling/api/src/mocks.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,31 @@ interface IPCMessage {
3535
* })
3636
* ```
3737
*
38+
* The callback function can also return a Promise:
39+
* ```js
40+
* import { mockIPC, clearMocks } from "@tauri-apps/api/mocks"
41+
* import { invoke } from "@tauri-apps/api/tauri"
42+
*
43+
* afterEach(() => {
44+
* clearMocks()
45+
* })
46+
*
47+
* test("mocked command", () => {
48+
* mockIPC((cmd, args) => {
49+
* if(cmd === "get_data") {
50+
* return fetch("https://example.com/data.json")
51+
* .then((response) => response.json())
52+
* }
53+
* });
54+
*
55+
* expect(invoke('get_data')).resolves.toBe({ foo: 'bar' });
56+
* })
57+
* ```
58+
*
3859
* @param cb
3960
*/
4061
export function mockIPC(
41-
cb: (cmd: string, args: Record<string, unknown>) => any
62+
cb: (cmd: string, args: Record<string, unknown>) => any | Promise<any>
4263
): void {
4364
// eslint-disable-next-line @typescript-eslint/no-misused-promises
4465
window.__TAURI_IPC__ = async ({

0 commit comments

Comments
 (0)