-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathfirst-run.test.ts
More file actions
108 lines (92 loc) · 3.36 KB
/
first-run.test.ts
File metadata and controls
108 lines (92 loc) · 3.36 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
101
102
103
104
105
106
107
108
import path from 'node:path';
// Mocks
const existsSync = jest.fn();
const mkdirSync = jest.fn();
const writeFileSync = jest.fn();
jest.mock('node:fs', () => ({
__esModule: true,
default: {
existsSync: (...a: unknown[]) => existsSync(...a),
mkdirSync: (...a: unknown[]) => mkdirSync(...a),
writeFileSync: (...a: unknown[]) => writeFileSync(...a),
},
existsSync: (...a: unknown[]) => existsSync(...a),
mkdirSync: (...a: unknown[]) => mkdirSync(...a),
writeFileSync: (...a: unknown[]) => writeFileSync(...a),
}));
const moveToApplicationsFolder = jest.fn();
const isInApplicationsFolder = jest.fn(() => false);
const getPath = jest.fn(() => '/User/Data');
const showMessageBox = jest.fn(async () => ({ response: 0 }));
jest.mock('electron', () => ({
app: {
getPath: () => getPath(),
isInApplicationsFolder: () => isInApplicationsFolder(),
moveToApplicationsFolder: () => moveToApplicationsFolder(),
},
dialog: { showMessageBox: () => showMessageBox() },
}));
const logError = jest.fn();
jest.mock('../shared/logger', () => ({
logError: (...a: unknown[]) => logError(...a),
}));
let mac = true;
jest.mock('../shared/platform', () => ({ isMacOS: () => mac }));
import { APPLICATION } from '../shared/constants';
import { onFirstRunMaybe } from './first-run';
describe('main/first-run', () => {
beforeEach(() => {
jest.clearAllMocks();
mac = true;
});
function configPath() {
return path.join('/User/Data', 'FirstRun', APPLICATION.FIRST_RUN_FOLDER);
}
it('creates first-run marker when not existing and returns true', async () => {
existsSync.mockReturnValueOnce(false); // marker absent
existsSync.mockReturnValueOnce(false); // folder absent
await onFirstRunMaybe();
expect(mkdirSync).toHaveBeenCalledWith(path.dirname(configPath()));
expect(writeFileSync).toHaveBeenCalledWith(configPath(), '');
});
it('skips writing when marker exists', async () => {
existsSync.mockReturnValueOnce(true); // marker present
await onFirstRunMaybe();
expect(writeFileSync).not.toHaveBeenCalled();
expect(mkdirSync).not.toHaveBeenCalled();
});
it('handles fs write error gracefully', async () => {
existsSync.mockReturnValueOnce(false); // marker absent
existsSync.mockReturnValueOnce(true); // folder exists
writeFileSync.mockImplementation(() => {
throw new Error('fail');
});
await onFirstRunMaybe();
expect(logError).toHaveBeenCalledWith(
'isFirstRun',
'Unable to write firstRun file',
expect.any(Error),
);
});
it('prompts and moves app on macOS when user accepts', async () => {
existsSync.mockReturnValueOnce(false); // marker
existsSync.mockReturnValueOnce(false); // folder
showMessageBox.mockResolvedValueOnce({ response: 0 });
await onFirstRunMaybe();
expect(moveToApplicationsFolder).toHaveBeenCalled();
});
it('does not move when user declines', async () => {
existsSync.mockReturnValueOnce(false);
existsSync.mockReturnValueOnce(false);
showMessageBox.mockResolvedValueOnce({ response: 1 });
await onFirstRunMaybe();
expect(moveToApplicationsFolder).not.toHaveBeenCalled();
});
it('skips prompt on non-macOS', async () => {
mac = false;
existsSync.mockReturnValueOnce(false);
existsSync.mockReturnValueOnce(false);
await onFirstRunMaybe();
expect(showMessageBox).not.toHaveBeenCalled();
});
});