5
5
i18nReady : true
6
6
---
7
7
8
+ import SinceVersion from ' ../../../../components/SinceVersion.astro' ;
9
+
8
10
When writing your frontend tests, having a "fake" Tauri environment to simulate windows or intercept IPC calls is common, so-called _ mocking_ .
9
11
The [ ` @tauri-apps/api/mocks ` ] module provides some helpful tools to make this easier for you:
10
12
@@ -29,6 +31,8 @@ The following examples use [Vitest], but you can use any other frontend testing
29
31
30
32
:::
31
33
34
+ ### Mocking Commands for ` invoke `
35
+
32
36
``` javascript
33
37
import { beforeAll , expect , test } from " vitest" ;
34
38
import { randomFillSync } from " crypto" ;
@@ -124,6 +128,33 @@ mockIPC(async (cmd, args) => {
124
128
});
125
129
```
126
130
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
+
127
158
## Windows
128
159
129
160
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 () => {
168
199
[ `mockwindows()` ] : /reference/javascript/api/namespacemocks/#mockwindows
169
200
[ `clearmocks()` ] : /reference/javascript/api/namespacemocks/#clearmocks
170
201
[ vitest ] : https://vitest.dev
202
+ [ Event System ] : /develop/calling-frontend/#event-system
0 commit comments