-
Notifications
You must be signed in to change notification settings - Fork 722
/
Copy pathsettings-electron-spec.tsx
312 lines (260 loc) · 8.99 KB
/
settings-electron-spec.tsx
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import * as React from 'react';
import { mount, shallow } from 'enzyme';
import { mocked } from 'jest-mock';
import {
ElectronReleaseChannel,
InstallState,
RunnableVersion,
VersionSource,
} from '../../../src/interfaces';
import { ElectronSettings } from '../../../src/renderer/components/settings-electron';
import { AppState } from '../../../src/renderer/state';
import { disableDownload } from '../../../src/renderer/utils/disable-download';
import { AppMock, StateMock, VersionsMock } from '../../mocks/mocks';
import { waitFor } from '../../utils';
jest.mock('../../../src/renderer/utils/disable-download.ts');
describe('ElectronSettings component', () => {
let store: StateMock;
let mockVersions: Record<string, RunnableVersion>;
let mockVersionsArray: RunnableVersion[];
const version = '2.0.1';
beforeEach(() => {
({ mockVersions, mockVersionsArray } = new VersionsMock());
({ state: store } = window.ElectronFiddle.app as unknown as AppMock);
store.initVersions(version, { ...mockVersions });
store.channelsToShow = [
ElectronReleaseChannel.stable,
ElectronReleaseChannel.beta,
];
// Render all the states
let i = 0;
store.versionsToShow[i++].state = InstallState.installed;
store.versionsToShow[i++].state = InstallState.downloading;
store.versionsToShow[i++].state = InstallState.missing;
store.versionsToShow[i++].state = InstallState.installing;
});
it('renders', async () => {
const spy = jest
.spyOn(window.ElectronFiddle, 'getOldestSupportedMajor')
.mockReturnValue(9);
const moreVersions: RunnableVersion[] = [
{
source: VersionSource.local,
state: InstallState.installed,
version: '3.0.0',
},
{
source: VersionSource.remote,
state: InstallState.installed,
version: '3.0.0-nightly.1',
},
];
for (const ver of moreVersions) {
store.versions[ver.version] = ver;
store.versionsToShow.unshift(ver);
}
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
await store.setVersion(version);
await waitFor(() => store.activeVersions.size > 0);
expect(wrapper).toMatchSnapshot();
spy.mockRestore();
});
it('handles removing a version', async () => {
store.versions['3.0.0-nightly.1'] = {
state: InstallState.installed,
version: '3.0.0-nightly.1',
source: VersionSource.local,
};
store.versions['3.0.0'] = {
state: InstallState.installed,
version: '3.0.0',
source: VersionSource.local,
};
const wrapper = mount(
<ElectronSettings appState={store as unknown as AppState} />,
);
wrapper
.find('.electron-versions-table .bp3-button')
.first()
.simulate('click');
expect(store.removeVersion).toHaveBeenCalledTimes(1);
});
it('handles downloading a version', async () => {
const version = '3.0.0';
const ver = {
source: VersionSource.remote,
state: InstallState.missing,
version,
};
store.versions = { version: ver };
store.versionsToShow = [ver];
const wrapper = mount(
<ElectronSettings appState={store as unknown as AppState} />,
);
wrapper
.find('.electron-versions-table .bp3-button')
.first()
.simulate('click');
expect(store.downloadVersion).toHaveBeenCalledTimes(1);
});
it('handles missing local versions', () => {
const version = '99999.0.0';
const ver = {
source: VersionSource.local,
state: InstallState.missing,
version,
};
store.versions = { version: ver };
store.versionsToShow = [ver];
const wrapper = mount(
<ElectronSettings appState={store as unknown as AppState} />,
);
wrapper
.find('.electron-versions-table .bp3-button')
.first()
.simulate('click');
expect(store.removeVersion).toHaveBeenCalledTimes(1);
});
it('handles the deleteAll()', async () => {
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
const instance: any = wrapper.instance();
await instance.handleDeleteAll();
expect(store.removeVersion).toHaveBeenCalledTimes(mockVersionsArray.length);
});
it('handles the downloadAll()', async () => {
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
const instance: any = wrapper.instance();
await instance.handleDownloadAll();
expect(store.downloadVersion).toHaveBeenCalled();
expect(store.downloadVersion).toHaveBeenCalledTimes(
store.versionsToShow.length,
);
});
it('handles stopDownloadingAll() during downloadAll()', async () => {
const versionsToShowCount = store.versionsToShow.length;
let completedDownloadCount = 0;
// Set up download mock
store.downloadVersion.mockImplementation(async () => {
completedDownloadCount++;
if (completedDownloadCount >= versionsToShowCount - 2) {
// Stop downloads before all versions downloaded
await instance.handleStopDownloads();
}
});
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
const instance = wrapper.instance() as any;
// Initiate download for all versions
await instance.handleDownloadAll();
// Stops downloading more versions
expect(completedDownloadCount).toBeGreaterThan(1);
expect(completedDownloadCount).toBeLessThan(versionsToShowCount);
});
describe('handleUpdateElectronVersions()', () => {
it('kicks off an update of Electron versions', async () => {
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
const instance: any = wrapper.instance();
instance.handleUpdateElectronVersions();
expect(store.updateElectronVersions).toHaveBeenCalled();
});
});
describe('handleAddVersion()', () => {
it('toggles the add version dialog', () => {
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
const instance: any = wrapper.instance();
instance.handleAddVersion();
expect(store.toggleAddVersionDialog).toHaveBeenCalled();
});
});
describe('handleStateChange()', () => {
it('toggles remote versions', async () => {
const id = 'showUndownloadedVersions';
for (const checked of [true, false]) {
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
const instance: any = wrapper.instance();
instance.handleStateChange({
currentTarget: { checked, id },
} as React.FormEvent<HTMLInputElement>);
expect(store[id]).toBe(checked);
}
});
});
describe('handleShowObsoleteChange()', () => {
it('toggles obsolete versions', async () => {
const id = 'showObsoleteVersions';
for (const checked of [true, false]) {
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
const instance: any = wrapper.instance();
instance.handleShowObsoleteChange({
currentTarget: { checked, id },
} as React.FormEvent<HTMLInputElement>);
expect(store[id]).toBe(checked);
}
});
});
describe('handleChannelChange()', () => {
it('handles a new selection', async () => {
const wrapper: any = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
store.showChannels.mockImplementation((ids: ElectronReleaseChannel[]) =>
store.channelsToShow.push(...ids),
);
store.hideChannels.mockImplementation(
(ids: ElectronReleaseChannel[]) =>
(store.channelsToShow = store.channelsToShow.filter(
(id: ElectronReleaseChannel) => !ids.includes(id),
)),
);
const instance = wrapper.instance();
instance.handleChannelChange({
currentTarget: {
id: ElectronReleaseChannel.stable,
checked: false,
},
} as React.FormEvent<HTMLInputElement>);
instance.handleChannelChange({
currentTarget: {
id: ElectronReleaseChannel.nightly,
checked: true,
},
} as React.FormEvent<HTMLInputElement>);
expect(store.channelsToShow).toEqual([
ElectronReleaseChannel.beta,
ElectronReleaseChannel.nightly,
]);
});
});
describe('disableDownload()', () => {
it('disables download buttons where return values are true', () => {
mocked(disableDownload).mockReturnValue(true);
const version = '3.0.0';
const ver = {
source: VersionSource.remote,
state: InstallState.missing,
version,
};
store.versions = { version: ver };
store.versionsToShow = [ver, { ...ver }, { ...ver }];
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);
expect(wrapper.find('.disabled-version')).toHaveLength(3);
});
});
});