Skip to content

Commit

Permalink
test: throw error test
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcherGu committed Feb 24, 2023
1 parent 8158412 commit 81aefbd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
4 changes: 4 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ describe('Einf Test', () => {
test('Window', () => {
expect(logs).toContain('Get window title: Einf Test')
})

test('Error', () => {
expect(logs).toContain('This is an error from ipc channel')
})
})
14 changes: 10 additions & 4 deletions tests/mock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1>Einf Test</h1>
<script type="text/javascript">
const {
sendMsg,
throwError,
printLog,
onReplyMsg,
exit,
Expand All @@ -22,13 +23,18 @@ <h1>Einf Test</h1>

onReplyMsg(async (msg) => {
printLog(msg)
const {
data: msgFromMain
} = await sendMsg("hello, this is the renderer")
const msgFromMain = await sendMsg("hello, this is the renderer")
printLog(msgFromMain)

try {
await throwError()
} catch (error) {
await printLog(error.message)
}

setTimeout(() => {
exit()
}, 500);
}, 500)
})
</script>
</body>
Expand Down
5 changes: 5 additions & 0 deletions tests/mock/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export class AppController {
return `main process received your message, current is run in ${this.isDev ? 'development' : 'production'} mode`
}

@IpcHandle('error')
public throwError() {
throw new Error('This is an error from ipc channel')
}

@IpcOn('print-log')
printLog(log: string) {
console.log(`Get log: ${log}`)
Expand Down
6 changes: 3 additions & 3 deletions tests/mock/src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { contextBridge, ipcRenderer } from 'electron'
import type { IpcResponse } from '../../../dist'

contextBridge.exposeInMainWorld(
'electron',
{
sendMsg: (msg: string): Promise<IpcResponse<string>> => ipcRenderer.invoke('send-msg', msg),
sendMsg: (msg: string): Promise<string> => ipcRenderer.invoke('send-msg', msg),
throwError: (): Promise<void> => ipcRenderer.invoke('error'),
printLog: (log: string): void => ipcRenderer.send('print-log', log),
onReplyMsg: (cb: (msg: string) => any) => ipcRenderer.on('reply-msg', (_, msg: string) => cb(msg)),
exit: (): Promise<IpcResponse<any>> => ipcRenderer.invoke('exit'),
exit: (): Promise<void> => ipcRenderer.invoke('exit'),
},
)

0 comments on commit 81aefbd

Please sign in to comment.