mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-01 10:01:07 +02:00
fix(api): add async mockIPC() handler signature (#5056)
This commit is contained in:
committed by
GitHub
parent
e9f1e627f8
commit
4fa968dc0e
5
.changes/fix-async-mockipc.md
Normal file
5
.changes/fix-async-mockipc.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"api": patch
|
||||
---
|
||||
|
||||
Update `mockIPC()` handler signature to allow async handler functions.
|
||||
@@ -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 ({
|
||||
|
||||
Reference in New Issue
Block a user