1
- import type { ContextBridge , IpcRenderer } from 'electron' ;
1
+ import type { ContextBridge , IpcRenderer , IpcRendererEvent } from 'electron' ;
2
2
import { afterEach , beforeAll , describe , expect , it , vi } from 'vitest' ;
3
3
import type {
4
4
AccountWithPassword ,
5
5
Character ,
6
6
} from '../../common/account/types.js' ;
7
+ import { GameCode } from '../../common/game/types.js' ;
7
8
import type { Layout } from '../../common/layout/types.js' ;
8
9
import type { LogMessage } from '../../common/logger/types.js' ;
9
10
import { LogLevel } from '../../common/logger/types.js' ;
@@ -16,7 +17,11 @@ const { mockContextBridge, mockIpcRenderer } = vi.hoisted(() => {
16
17
const mockIpcRenderer = {
17
18
send : vi . fn < IpcRenderer [ 'send' ] > ( ) ,
18
19
invoke : vi . fn < IpcRenderer [ 'invoke' ] > ( ) ,
19
- on : vi . fn < IpcRenderer [ 'on' ] > ( ) ,
20
+ on : vi . fn < IpcRenderer [ 'on' ] > ( ) . mockImplementation ( ( channel , callback ) => {
21
+ // Invoke our callbacks so we can assert they're being registered.
22
+ callback ( { } as IpcRendererEvent , 'test-message' ) ;
23
+ return { } as IpcRenderer ;
24
+ } ) ,
20
25
off : vi . fn < IpcRenderer [ 'off' ] > ( ) ,
21
26
removeAllListeners : vi . fn < IpcRenderer [ 'removeAllListeners' ] > ( ) ,
22
27
} ;
@@ -132,7 +137,7 @@ describe('index', () => {
132
137
const mockCharacter : Character = {
133
138
accountName : 'test-account-name' ,
134
139
characterName : 'test-character-name' ,
135
- gameCode : 'DR' ,
140
+ gameCode : GameCode . PRIME ,
136
141
} ;
137
142
138
143
it ( 'invokes saveCharacter' , async ( ) => {
@@ -148,7 +153,7 @@ describe('index', () => {
148
153
const mockCharacter : Character = {
149
154
accountName : 'test-account-name' ,
150
155
characterName : 'test-character-name' ,
151
- gameCode : 'DR' ,
156
+ gameCode : GameCode . PRIME ,
152
157
} ;
153
158
154
159
it ( 'invokes removeCharacter' , async ( ) => {
@@ -164,7 +169,7 @@ describe('index', () => {
164
169
const mockCharacter : Character = {
165
170
accountName : 'test-account-name' ,
166
171
characterName : 'test-character-name' ,
167
- gameCode : 'DR' ,
172
+ gameCode : GameCode . PRIME ,
168
173
} ;
169
174
170
175
it ( 'invokes listCharacters' , async ( ) => {
@@ -179,7 +184,7 @@ describe('index', () => {
179
184
const mockCharacter : Character = {
180
185
accountName : 'test-account-name' ,
181
186
characterName : 'test-character-name' ,
182
- gameCode : 'DR' ,
187
+ gameCode : GameCode . PRIME ,
183
188
} ;
184
189
185
190
it ( 'invokes playCharacter' , async ( ) => {
@@ -299,19 +304,23 @@ describe('index', () => {
299
304
300
305
describe ( '#onMessage' , async ( ) => {
301
306
it ( 'invokes on, then off when unsubscribe' , async ( ) => {
307
+ // The `onMessage` API wraps our callback so to assert that
308
+ // our function is being used then we need to assert it is invoked.
302
309
const mockListener = vi . fn ( ) ;
303
310
304
311
const unsubscribe = api . onMessage ( 'test-channel' , mockListener ) ;
305
312
expect ( mockIpcRenderer . on ) . toHaveBeenCalledWith (
306
313
'test-channel' ,
307
- mockListener
314
+ expect . any ( Function )
308
315
) ;
309
316
310
317
unsubscribe ( ) ;
311
318
expect ( mockIpcRenderer . off ) . toHaveBeenCalledWith (
312
319
'test-channel' ,
313
- mockListener
320
+ expect . any ( Function )
314
321
) ;
322
+
323
+ expect ( mockListener ) . toHaveBeenCalledWith ( 'test-message' ) ;
315
324
} ) ;
316
325
} ) ;
317
326
0 commit comments