-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathindex.test.ts
More file actions
162 lines (141 loc) · 4.84 KB
/
index.test.ts
File metadata and controls
162 lines (141 loc) · 4.84 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { autoUpdater, dialog } from 'electron';
import {
updateElectronApp,
makeUserNotifier,
IUpdateInfo,
IUpdateDialogStrings,
UpdateSourceType,
} from '../src';
const repo = 'some-owner/some-repo';
beforeEach(() => {
jest.useFakeTimers();
});
describe('updateElectronApp', () => {
it('is a function', () => {
expect(typeof updateElectronApp).toBe('function');
});
describe('repository', () => {
const tmpdir = os.tmpdir();
const packageJson = path.join(tmpdir, 'package.json');
beforeAll(() => {
fs.writeFileSync(packageJson, JSON.stringify({}));
});
it('is required', () => {
expect(() => {
updateElectronApp();
}).toThrow("repo not found. Add repository string to your app's package.json file");
});
it('from opts', () => {
updateElectronApp({ repo: 'foo/bar' });
});
it('from package.json', () => {
fs.writeFileSync(packageJson, JSON.stringify({ repository: 'foo/bar' }));
updateElectronApp();
});
it.each([
['owner/repo', 'owner/repo'],
['https://github.com/owner/repo', 'owner/repo'],
['https://github.com/owner/repo.git', 'owner/repo'],
['git+https://github.com/owner/repo.git', 'owner/repo'],
['git@github.com:owner/repo.git', 'owner/repo'],
['github:owner/repo', 'owner/repo'],
['https://github.com/owner/my-repo.js', 'owner/my-repo.js'],
['https://github.com/owner/my_repo', 'owner/my_repo'],
])('parses repo from %s', (repository, expected) => {
fs.writeFileSync(packageJson, JSON.stringify({ repository }));
const logSpy = jest.spyOn(console, 'log').mockImplementation();
updateElectronApp();
expect(logSpy).toHaveBeenCalledWith('feedURL', expect.stringContaining(`/${expected}/`));
logSpy.mockRestore();
});
it.each([['https://github.com/owner/repo'], ['https://github.com/owner/repo.git']])(
'parses repo from repository.url: %s',
(url) => {
fs.writeFileSync(packageJson, JSON.stringify({ repository: { url } }));
const logSpy = jest.spyOn(console, 'log').mockImplementation();
updateElectronApp();
expect(logSpy).toHaveBeenCalledWith('feedURL', expect.stringContaining('/owner/repo/'));
logSpy.mockRestore();
},
);
afterAll(() => {
fs.rmSync(packageJson);
});
});
describe('host', () => {
it('must a valid HTTPS URL', () => {
expect(() => {
updateElectronApp({ repo, host: 'http://example.com' });
}).toThrow('host must be a valid HTTPS URL');
});
it('from default', () => {
updateElectronApp({
updateSource: {
type: UpdateSourceType.ElectronPublicUpdateService,
repo,
},
});
});
});
describe('updateInterval', () => {
it('must be 5 minutes or more', () => {
expect(() => {
updateElectronApp({ repo, updateInterval: 20_000 });
}).toThrow('updateInterval must be `5 minutes` or more');
});
});
});
describe('makeUserNotifier', () => {
const fakeUpdateInfo: IUpdateInfo = {
event: {} as Electron.Event,
releaseNotes: 'new release',
releaseName: 'v13.3.7',
releaseDate: new Date(),
updateURL: 'https://fake-update.url',
};
beforeEach(() => {
jest.mocked(dialog.showMessageBox).mockReset();
});
it('is a function that returns a callback function', () => {
expect(typeof makeUserNotifier).toBe('function');
expect(typeof makeUserNotifier()).toBe('function');
});
describe('callback', () => {
it.each([
['does', 0, 1],
['does not', 1, 0],
])('%s call autoUpdater.quitAndInstall if the user responds with %i', (_, response, called) => {
jest
.mocked(dialog.showMessageBox)
.mockResolvedValueOnce({ response, checkboxChecked: false });
const notifier = makeUserNotifier();
notifier(fakeUpdateInfo);
expect(dialog.showMessageBox).toHaveBeenCalled();
// quitAndInstall is only called after the showMessageBox promise resolves
process.nextTick(() => {
expect(autoUpdater.quitAndInstall).toHaveBeenCalledTimes(called);
});
});
});
it('can customize dialog properties', () => {
const strings: IUpdateDialogStrings = {
title: 'Custom Update Title',
detail: 'Custom update details',
restartButtonText: 'Custom restart string',
laterButtonText: 'Maybe not',
};
jest.mocked(dialog.showMessageBox).mockResolvedValue({ response: 0, checkboxChecked: false });
const notifier = makeUserNotifier(strings);
notifier(fakeUpdateInfo);
expect(dialog.showMessageBox).toHaveBeenCalledWith(
expect.objectContaining({
buttons: [strings.restartButtonText, strings.laterButtonText],
title: strings.title,
detail: strings.detail,
}),
);
});
});