Skip to content

Commit c1f32e5

Browse files
committed
Fix test fail
- Fixed unit test failure after the change of using Electron's `app.whenReady` API. - Implemented unit test for function `createWindow`. - Removed `mainWindow` from `main` export as its' value will always be `undefined` the way it's exported.
1 parent faccc8c commit c1f32e5

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/main/main.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function createWindow() {
3636
// initialization and is ready to create browser windows.
3737
// Some APIs can only be used after this event occurs.
3838
app.whenReady().then(() => {
39-
createWindow();
39+
if (nodeEnv.dev || nodeEnv.prod) createWindow();
4040

4141
app.on('activate', () => {
4242
if (BrowserWindow.getAllWindows.length === 0) createWindow();
@@ -59,5 +59,4 @@ ipcMain.on('renderer-ready', () => {
5959
// code. You can also put them in separate files and require them here.
6060

6161
// eslint-disable-next-line import/prefer-default-export
62-
export const exportedForTests = nodeEnv.test
63-
? { mainWindow, createWindow } : undefined;
62+
export const exportedForTests = nodeEnv.test ? { createWindow } : undefined;

tests/main/main.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import { exportedForTests } from '_main/main';
2+
import { BrowserWindow } from 'electron';
23

34
jest.mock('electron', () => ({
4-
app: { on: jest.fn() },
5+
app: {
6+
on: jest.fn(),
7+
whenReady: jest.fn(() => Promise.resolve()),
8+
},
59
ipcMain: { on: jest.fn() },
10+
BrowserWindow: jest.fn().mockImplementation(() => ({
11+
loadFile: jest.fn(() => Promise.resolve()),
12+
on: jest.fn(),
13+
})),
614
}));
715

816
test('Private props exported for unit tests', () => {
917
expect(exportedForTests).toBeDefined();
1018
});
19+
20+
test('func createWindow()', () => {
21+
const { createWindow } = exportedForTests!;
22+
23+
createWindow();
24+
expect(BrowserWindow).toHaveBeenCalledTimes(1);
25+
});

0 commit comments

Comments
 (0)