fix(api): add async mockIPC() handler signature (#5056)

This commit is contained in:
Jonas Kruckenberg
2022-08-26 13:12:24 +02:00
committed by GitHub
parent e9f1e627f8
commit 4fa968dc0e
2 changed files with 27 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
"api": patch
---
Update `mockIPC()` handler signature to allow async handler functions.

View File

@@ -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 ({