Skip to content

Commit b2dc620

Browse files
docs(mocking): add note about event mocking (#3395)
1 parent 8cd9d1c commit b2dc620

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/content/docs/develop/Tests/mocking.md renamed to src/content/docs/develop/Tests/mocking.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar:
55
i18nReady: true
66
---
77

8+
import SinceVersion from '../../../../components/SinceVersion.astro';
9+
810
When writing your frontend tests, having a "fake" Tauri environment to simulate windows or intercept IPC calls is common, so-called _mocking_.
911
The [`@tauri-apps/api/mocks`] module provides some helpful tools to make this easier for you:
1012

@@ -29,6 +31,8 @@ The following examples use [Vitest], but you can use any other frontend testing
2931

3032
:::
3133

34+
### Mocking Commands for `invoke`
35+
3236
```javascript
3337
import { beforeAll, expect, test } from "vitest";
3438
import { randomFillSync } from "crypto";
@@ -124,6 +128,33 @@ mockIPC(async (cmd, args) => {
124128
});
125129
```
126130

131+
### Mocking Events
132+
133+
<SinceVersion version="2.7.0" />
134+
135+
There is partial support of the [Event System] to simulate events emitted by your Rust code via the `shouldMockEvents` option:
136+
137+
```javascript
138+
import { mockIPC, clearMocks } from '@tauri-apps/api/mocks';
139+
import { emit, listen } from '@tauri-apps/api/event';
140+
import { afterEach, expect, test, vi } from 'vitest';
141+
142+
test('mocked event', () => {
143+
mockIPC(() => {}, { shouldMockEvents: true }); // enable event mocking
144+
145+
const eventHandler = vi.fn();
146+
listen('test-event', eventHandler);
147+
148+
emit('test-event', { foo: 'bar' });
149+
expect(eventHandler).toHaveBeenCalledWith({
150+
event: 'test-event',
151+
payload: { foo: 'bar' },
152+
});
153+
});
154+
```
155+
156+
`emitTo` and `emit_filter` are **not** supported yet.
157+
127158
## Windows
128159

129160
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 () => {
168199
[`mockwindows()`]: /reference/javascript/api/namespacemocks/#mockwindows
169200
[`clearmocks()`]: /reference/javascript/api/namespacemocks/#clearmocks
170201
[vitest]: https://vitest.dev
202+
[Event System]: /develop/calling-frontend/#event-system

0 commit comments

Comments
 (0)