-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathunit.spec.ts
59 lines (45 loc) · 1.85 KB
/
unit.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {BrowserWindow} from 'electron';
import type {MockedObject} from 'vitest';
import {afterEach, expect, test, vi} from 'vitest';
import {restoreOrCreateWindow} from '../src/mainWindow';
const BrowserWindowMocked = BrowserWindow as MockedObject<typeof BrowserWindow>;
vi.mock('electron', () => {
const bw = vi.fn(function () {
this.on = vi.fn();
this.destroy = vi.fn();
this.loadURL = vi.fn();
this.isDestroyed = vi.fn(() => false);
this.isMinimized = vi.fn(() => false);
this.focus = vi.fn();
this.restore = vi.fn();
});
bw.getAllWindows = vi.fn();
return {
BrowserWindow: bw,
};
});
afterEach(() => {
BrowserWindowMocked.mockClear();
});
test('Should create new window', async () => {
expect(BrowserWindowMocked.getAllWindows.mock.calls.length).toEqual(0);
await restoreOrCreateWindow();
expect(BrowserWindowMocked.mock.instances.length).toEqual(1);
expect(BrowserWindowMocked.mock.instances[0].loadURL).toHaveBeenCalledTimes(1);
expect(BrowserWindowMocked.mock.instances[0].loadURL.calls.length).toEqual(1);
});
test('Should restore existing window', async () => {
expect(BrowserWindowMocked.mock.instances.length).toEqual(1);
await restoreOrCreateWindow();
expect(BrowserWindowMocked.mock.instances.length).toEqual(1);
expect(BrowserWindowMocked.mock.instances[0].focus).toHaveBeenCalledTimes(1);
BrowserWindowMocked.mock.instances[0].isMinimized.mockReturnValueOnce(true);
await restoreOrCreateWindow();
expect(BrowserWindowMocked.mock.instances[0].restore).toHaveBeenCalledTimes(1);
});
test('Should create new window if previous was destroyed', async () => {
expect(BrowserWindowMocked.mock.instances.length).toEqual(1);
BrowserWindowMocked.mock.instances[0].isDestroyed.mockReturnValueOnce(true);
await restoreOrCreateWindow();
expect(BrowserWindowMocked.mock.instances.length).toEqual(2);
});