diff --git a/src/content/docs/develop/Tests/mocking.md b/src/content/docs/develop/Tests/mocking.mdx similarity index 84% rename from src/content/docs/develop/Tests/mocking.md rename to src/content/docs/develop/Tests/mocking.mdx index 1223774020..a5c9166e13 100644 --- a/src/content/docs/develop/Tests/mocking.md +++ b/src/content/docs/develop/Tests/mocking.mdx @@ -4,6 +4,8 @@ sidebar: order: 10 i18nReady: true --- +import SinceVersion from '../../../../components/SinceVersion.astro'; + When writing your frontend tests, having a "fake" Tauri environment to simulate windows or intercept IPC calls is common, so-called _mocking_. The [`@tauri-apps/api/mocks`] module provides some helpful tools to make this easier for you: @@ -29,6 +31,8 @@ The following examples use [Vitest], but you can use any other frontend testing ::: +### Mocking Commands for `invoke` + ```javascript import { beforeAll, expect, test } from "vitest"; import { randomFillSync } from "crypto"; @@ -124,6 +128,33 @@ mockIPC(async (cmd, args) => { }); ``` +### Mocking Events + + + +There is partial support of the [Event System] to simulate events emitted by your Rust code via the `shouldMockEvents` option: + +```javascript +import { mockIPC, clearMocks } from "@tauri-apps/api/mocks" +import { emit, listen } from "@tauri-apps/api/event" +import { afterEach, expect, test, vi } from "vitest" + +test("mocked event", () => { + mockIPC(() => {}, { shouldMockEvents: true }); // enable event mocking + + const eventHandler = vi.fn(); + listen('test-event', eventHandler); + + emit('test-event', { foo: 'bar' }); + expect(eventHandler).toHaveBeenCalledWith({ + event: 'test-event', + payload: { foo: 'bar' } + }); +}) +``` +`emitTo` and `emit_filter` are **not** supported yet. + + ## Windows Sometimes you have window-specific code (a splash screen window, for example), so you need to simulate different windows. @@ -168,3 +199,4 @@ test('invoke', async () => { [`mockwindows()`]: /reference/javascript/api/namespacemocks/#mockwindows [`clearmocks()`]: /reference/javascript/api/namespacemocks/#clearmocks [vitest]: https://vitest.dev +[Event System]: /develop/calling-frontend/#event-system