Skip to content

Commit

Permalink
fix(api): add async mockIPC() handler signature (#5056)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg authored Aug 26, 2022
1 parent e9f1e62 commit 4fa968d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/fix-async-mockipc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

Update `mockIPC()` handler signature to allow async handler functions.
23 changes: 22 additions & 1 deletion tooling/api/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>) => any
cb: (cmd: string, args: Record<string, unknown>) => any | Promise<any>
): void {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
window.__TAURI_IPC__ = async ({
Expand Down

0 comments on commit 4fa968d

Please sign in to comment.