-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathutils.test.ts
More file actions
100 lines (86 loc) · 2.66 KB
/
utils.test.ts
File metadata and controls
100 lines (86 loc) · 2.66 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import path from 'node:path';
import type { Menubar } from 'menubar';
const writeFile = vi.fn((_p: string, _d: unknown, cb: () => void) => cb());
vi.mock('node:fs', () => ({
default: {
writeFile: (p: string, d: unknown, cb: () => void) => writeFile(p, d, cb),
},
writeFile: (p: string, d: unknown, cb: () => void) => writeFile(p, d, cb),
}));
const homedir = vi.fn(() => '/home/test');
vi.mock('node:os', () => ({
default: { homedir: () => homedir() },
homedir: () => homedir(),
}));
vi.mock('electron', () => ({
app: {
isPackaged: true,
},
shell: { openPath: vi.fn(() => Promise.resolve('')) },
dialog: { showMessageBoxSync: vi.fn(() => 0) },
}));
const fileGetFileMock = vi.fn(() => ({ path: '/var/log/app/app.log' }));
vi.mock('electron-log', () => ({
default: {
transports: {
file: { getFile: () => fileGetFileMock() },
},
},
transports: {
file: { getFile: () => fileGetFileMock() },
},
}));
const logInfoMock = vi.fn();
const logErrorMock = vi.fn();
vi.mock('../shared/logger', () => ({
logInfo: (...a: unknown[]) => logInfoMock(...a),
logError: (...a: unknown[]) => logErrorMock(...a),
}));
import { shell } from 'electron';
import { isDevMode, openLogsDirectory, takeScreenshot } from './utils';
function createMb() {
return {
window: {
capturePage: () =>
Promise.resolve({ toPNG: () => Buffer.from('image-bytes') }),
},
};
}
describe('main/utils', () => {
beforeEach(() => {
fileGetFileMock.mockReturnValue({ path: '/var/log/app/app.log' });
});
describe('isDevMode', () => {
it('returns false when app is packaged', () => {
expect(isDevMode()).toBe(false);
});
});
describe('takeScreenshot', () => {
it('writes file and logs info', async () => {
const mb = createMb();
await takeScreenshot(mb as unknown as Menubar);
expect(writeFile).toHaveBeenCalled();
const writtenPath = (writeFile.mock.calls[0] as unknown[])[0] as string;
expect(writtenPath.startsWith(path.join('/home/test'))).toBe(true);
expect(logInfoMock).toHaveBeenCalledWith(
'takeScreenshot',
expect.stringContaining('Screenshot saved'),
);
});
});
describe('openLogsDirectory', () => {
it('opens directory when log file is present', () => {
openLogsDirectory();
expect(shell.openPath).toHaveBeenCalledWith('/var/log/app');
});
it('logs error when log path is unavailable', () => {
fileGetFileMock.mockReturnValueOnce(null);
openLogsDirectory();
expect(logErrorMock).toHaveBeenCalledWith(
'openLogsDirectory',
'Could not find log directory!',
expect.any(Error),
);
});
});
});