-
Notifications
You must be signed in to change notification settings - Fork 752
Expand file tree
/
Copy pathstate-spec.ts
More file actions
865 lines (724 loc) · 26.7 KB
/
state-spec.ts
File metadata and controls
865 lines (724 loc) · 26.7 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
import { mocked } from 'jest-mock';
import { IReactionDisposer, reaction } from 'mobx';
import {
AppStateBroadcastMessageType,
BlockableAccelerator,
ElectronReleaseChannel,
GenericDialogType,
InstallState,
MAIN_JS,
RunnableVersion,
Version,
VersionSource,
} from '../../src/interfaces';
import { Bisector } from '../../src/renderer/bisect';
import { AppState } from '../../src/renderer/state';
import {
fetchVersions,
getElectronVersions,
makeRunnable,
saveLocalVersions,
} from '../../src/renderer/versions';
import { VersionsMock, createEditorValues } from '../mocks/mocks';
import {
overrideRendererPlatform,
resetRendererPlatform,
waitFor,
} from '../utils';
jest.mock('../../src/renderer/versions', () => {
const { getReleaseChannel } = jest.requireActual(
'../../src/renderer/versions',
);
const { VersionsMock } = require('../mocks/electron-versions');
const { mockVersionsArray } = new VersionsMock();
return {
addLocalVersion: jest.fn(),
fetchVersions: jest.fn(mockVersionsArray),
getDefaultVersion: () => '2.0.2',
getElectronVersions: jest.fn(),
getReleaseChannel,
makeRunnable: jest.fn((v) => v),
saveLocalVersions: jest.fn(),
};
});
describe('AppState', () => {
let appState: AppState;
let mockVersions: Record<string, RunnableVersion>;
let mockVersionsArray: RunnableVersion[];
let removeSpy: jest.SpyInstance;
let installSpy: jest.SpyInstance;
let ensureDownloadedSpy: jest.SpyInstance;
let broadcastMessageSpy: jest.SpyInstance;
beforeEach(() => {
({ mockVersions, mockVersionsArray } = new VersionsMock());
mocked(fetchVersions).mockResolvedValue(mockVersionsArray);
jest
.spyOn(AppState.prototype, 'getVersionState')
.mockImplementation(() => InstallState.installed);
appState = new AppState(mockVersionsArray);
removeSpy = jest
.spyOn(appState.installer, 'remove')
.mockResolvedValue(undefined);
installSpy = jest
.spyOn(appState.installer, 'install')
.mockResolvedValue('');
ensureDownloadedSpy = jest
.spyOn(appState.installer, 'ensureDownloaded')
.mockResolvedValue({ path: '', alreadyExtracted: false });
broadcastMessageSpy = jest.spyOn(
(appState as any).broadcastChannel,
'postMessage',
);
});
it('exists', () => {
expect(appState).toBeTruthy();
});
afterAll(async () => {
// Wait for all the async task to resolve before the jest
// environment tears down
await new Promise((resolve) => setTimeout(resolve, 2000));
});
describe('updateElectronVersions()', () => {
it('handles errors gracefully', async () => {
mocked(fetchVersions).mockRejectedValue(new Error('Bwap-bwap'));
await appState.updateElectronVersions();
});
it('adds new versions', async () => {
const version = '100.0.0';
const ver: Version = { version };
mocked(fetchVersions).mockResolvedValue([ver]);
mocked(makeRunnable).mockImplementation(
(v: Version): RunnableVersion => v as RunnableVersion,
);
const oldCount = Object.keys(appState.versions).length;
await appState.updateElectronVersions();
const newCount = Object.keys(appState.versions).length;
expect(newCount).toBe(oldCount + 1);
expect(appState.versions[version]).toStrictEqual(ver);
expect(broadcastMessageSpy).toHaveBeenCalledWith({
type: AppStateBroadcastMessageType.syncVersions,
payload: [ver],
});
});
});
describe('getName()', () => {
it('returns the name', async () => {
(appState as any).name = 'hi';
expect(await appState.getName()).toBe('hi');
});
it('returns the name, even if none exists', async () => {
mocked(window.ElectronFiddle.getProjectName).mockResolvedValue('test');
(appState as any).name = undefined;
expect(await appState.getName()).toBe('test');
});
});
describe('get currentElectronVersion()', () => {
it('returns the current version', () => {
appState.version = '2.0.2';
expect(appState.currentElectronVersion).toEqual(mockVersions['2.0.2']);
});
it('falls back to the defaultVersion', () => {
appState.version = 'garbage';
expect(appState.currentElectronVersion).toEqual(mockVersions['2.0.2']);
});
});
describe('toggleConsole()', () => {
it('toggles the console', () => {
appState.toggleConsole();
expect(appState.isConsoleShowing).toBe(true);
appState.toggleConsole();
expect(appState.isConsoleShowing).toBe(false);
});
});
describe('clearConsole()', () => {
it('clears the console', () => {
expect(appState.output.length).toBe(1);
appState.clearConsole();
expect(appState.output.length).toBe(0);
});
});
describe('toggleAuthDialog()', () => {
it('toggles the token dialog', () => {
appState.toggleAuthDialog();
expect(appState.isTokenDialogShowing).toBe(true);
appState.toggleAuthDialog();
expect(appState.isTokenDialogShowing).toBe(false);
});
});
describe('toggleSettings()', () => {
it('toggles the settings page', () => {
appState.toggleSettings();
expect(appState.isSettingsShowing).toBe(true);
appState.toggleSettings();
expect(appState.isSettingsShowing).toBe(false);
});
});
describe('toggleBisectCommands()', () => {
it('toggles visibility of the bisect commands', () => {
const isVisible = appState.isBisectCommandShowing;
appState.toggleBisectCommands();
expect(isVisible).not.toBe(appState.isBisectCommandShowing);
});
it('takes no action if bisect dialog is active', () => {
const isVisible = appState.isBisectCommandShowing;
expect(appState.isBisectDialogShowing).toBe(false);
appState.toggleBisectDialog();
appState.toggleBisectCommands();
expect(isVisible).toBe(appState.isBisectCommandShowing);
});
it('takes no action if bisect instance is active', () => {
const isVisible = appState.isBisectCommandShowing;
appState.Bisector = new Bisector([]);
appState.toggleBisectCommands();
expect(isVisible).toBe(appState.isBisectCommandShowing);
});
});
describe('toggleAddVersionDialog()', () => {
it('toggles the add version dialog', () => {
appState.toggleAddVersionDialog();
expect(appState.isAddVersionDialogShowing).toBe(true);
appState.toggleAddVersionDialog();
expect(appState.isAddVersionDialogShowing).toBe(false);
});
});
describe('setIsQuitting()', () => {
it('sets isQuitting variable as true', () => {
appState.setIsQuitting();
expect(appState.isQuitting).toBe(true);
});
});
describe('disableTour()', () => {
it('disables the tour', () => {
appState.isTourShowing = false;
appState.disableTour();
expect(appState.isTourShowing).toBe(false);
});
});
describe('showTour()', () => {
it('shows the tour', () => {
appState.isTourShowing = true;
appState.showTour();
expect(appState.isTourShowing).toBe(true);
});
});
describe('versionsToShow()', () => {
it('returns all versions for no filters', () => {
expect(appState.versionsToShow.length).toEqual(
Object.keys(appState.versions).length,
);
});
it('handles empty items', () => {
const originalLength = Object.keys(appState.versions).length;
appState.versions.foo = undefined as any;
expect(appState.versionsToShow.length).toEqual(originalLength);
});
it('excludes channels', () => {
appState.channelsToShow = ['Unsupported' as any];
expect(appState.versionsToShow.length).toEqual(0);
appState.channelsToShow = [ElectronReleaseChannel.stable];
expect(appState.versionsToShow.length).toEqual(mockVersionsArray.length);
});
it('handles undownloaded versions', () => {
Object.values(appState.versions).forEach(
(ver) => (ver.state = InstallState.missing),
);
appState.showUndownloadedVersions = false;
expect(appState.versionsToShow.length).toEqual(0);
appState.showUndownloadedVersions = true;
expect(appState.versionsToShow.length).toEqual(mockVersionsArray.length);
});
});
describe('showChannels()', () => {
it('adds channels from `channelsToShow`', () => {
appState.channelsToShow = [
ElectronReleaseChannel.beta,
ElectronReleaseChannel.stable,
];
appState.showChannels([
ElectronReleaseChannel.beta,
ElectronReleaseChannel.nightly,
]);
expect([...appState.channelsToShow].sort()).toEqual([
ElectronReleaseChannel.beta,
ElectronReleaseChannel.nightly,
ElectronReleaseChannel.stable,
]);
});
});
describe('hideChannels()', () => {
it('removes channels from `channelsToShow`', () => {
appState.channelsToShow = [
ElectronReleaseChannel.beta,
ElectronReleaseChannel.nightly,
ElectronReleaseChannel.stable,
];
appState.hideChannels([ElectronReleaseChannel.beta]);
expect([...appState.channelsToShow].sort()).toEqual([
ElectronReleaseChannel.nightly,
ElectronReleaseChannel.stable,
]);
});
});
describe('removeVersion()', () => {
let active: string;
let version: string;
beforeEach(() => {
active = appState.currentElectronVersion.version;
version = mockVersionsArray.find((v) => v.version !== active)!.version;
});
it('does not remove the active version', async () => {
const ver = appState.versions[active];
await appState.setVersion(ver.version);
await waitFor(() => appState.activeVersions.size > 0);
broadcastMessageSpy.mockClear();
await appState.removeVersion(ver);
expect(removeSpy).not.toHaveBeenCalled();
expect(broadcastMessageSpy).not.toHaveBeenCalled();
});
it('removes a version', async () => {
const ver = appState.versions[version];
ver.state = InstallState.installed;
await appState.removeVersion(ver);
expect(removeSpy).toHaveBeenCalledWith(ver.version);
expect(broadcastMessageSpy).toHaveBeenCalledWith({
type: AppStateBroadcastMessageType.syncVersions,
payload: [ver],
});
});
it('does not remove it if not necessary', async () => {
const ver = appState.versions[version];
ver.state = InstallState.missing;
broadcastMessageSpy.mockClear();
await appState.removeVersion(ver);
expect(removeSpy).toHaveBeenCalledTimes(0);
expect(broadcastMessageSpy).not.toHaveBeenCalled();
});
it('removes (but does not delete) a local version', async () => {
const localPath = '/fake/path';
const ver = appState.versions[version];
ver.localPath = localPath;
ver.source = VersionSource.local;
ver.state = InstallState.installed;
broadcastMessageSpy.mockClear();
await appState.removeVersion(ver);
expect(saveLocalVersions).toHaveBeenCalledTimes(1);
expect(appState.versions[version]).toBeUndefined();
expect(removeSpy).toHaveBeenCalledTimes(0);
expect(broadcastMessageSpy).not.toHaveBeenCalled();
});
});
describe('downloadVersion()', () => {
it('downloads a version', async () => {
const ver = appState.versions['2.0.2'];
ver.state = InstallState.missing;
await appState.downloadVersion(ver);
expect(ensureDownloadedSpy).toHaveBeenCalled();
expect(installSpy).not.toHaveBeenCalled();
expect(broadcastMessageSpy).toHaveBeenCalledWith({
type: AppStateBroadcastMessageType.syncVersions,
payload: [ver],
});
});
it('does not download a version if already ready', async () => {
const ver = appState.versions['2.0.2'];
ver.state = InstallState.installed;
broadcastMessageSpy.mockClear();
await appState.downloadVersion(ver);
expect(ensureDownloadedSpy).not.toHaveBeenCalled();
expect(installSpy).not.toHaveBeenCalled();
expect(broadcastMessageSpy).not.toHaveBeenCalled();
});
});
describe('hasVersion()', () => {
it('returns false if state does not have that version', () => {
const UNKNOWN_VERSION = 'v999.99.99';
expect(appState.hasVersion(UNKNOWN_VERSION)).toEqual(false);
});
it('returns true if state has that version', () => {
const KNOWN_VERSION = Object.keys(appState.versions).pop();
expect(appState.hasVersion(KNOWN_VERSION!)).toBe(true);
});
});
describe('setVersion()', () => {
it('uses the newest version iff the specified version does not exist', async () => {
await appState.setVersion('v999.99.99');
expect(appState.version).toBe(mockVersionsArray[0].version);
});
it('downloads a version if necessary', async () => {
appState.downloadVersion = jest.fn();
await appState.setVersion('v2.0.2');
expect(appState.downloadVersion).toHaveBeenCalled();
});
describe('loads the template for the new version', () => {
let newVersion: string;
let oldVersion: string;
let replaceSpy: ReturnType<typeof jest.spyOn>;
const nextValues = createEditorValues();
beforeEach(() => {
// pick some version that differs from the current version
oldVersion = appState.version;
newVersion = Object.keys(appState.versions)
.filter((version) => version !== oldVersion)
.shift()!;
expect(newVersion).not.toStrictEqual(oldVersion);
expect(newVersion).toBeTruthy();
// spy on app.replaceFiddle
replaceSpy = jest.spyOn(window.ElectronFiddle.app, 'replaceFiddle');
replaceSpy.mockReset();
mocked(window.ElectronFiddle.getTemplate).mockResolvedValue(nextValues);
});
it('if there is no current fiddle', async () => {
// setup: current fiddle is empty
appState.editorMosaic.set({});
await appState.setVersion(newVersion);
expect(replaceSpy).toHaveBeenCalledTimes(1);
const templateName = newVersion;
expect(replaceSpy).toHaveBeenCalledWith(nextValues, { templateName });
});
it('if the current fiddle is an unedited template', async () => {
appState.templateName = oldVersion;
appState.editorMosaic.set({ [MAIN_JS]: '// content' });
appState.editorMosaic.isEdited = false;
await appState.setVersion(newVersion);
const templateName = newVersion;
expect(replaceSpy).toHaveBeenCalledWith(nextValues, { templateName });
});
it('but not if the current fiddle is edited', async () => {
appState.editorMosaic.set({ [MAIN_JS]: '// content' });
appState.editorMosaic.isEdited = true;
appState.templateName = oldVersion;
await appState.setVersion(newVersion);
expect(replaceSpy).not.toHaveBeenCalled();
});
it('but not if the current fiddle is not a template', async () => {
appState.editorMosaic.set({ [MAIN_JS]: '// content' });
appState.localPath = '/some/path/to/a/fiddle';
await appState.setVersion(newVersion);
expect(replaceSpy).not.toHaveBeenCalled();
});
});
it('updates typescript definitions', async () => {
const version = '2.0.2';
const ver = appState.versions[version];
ver.source = VersionSource.local;
appState.setVersion(version);
});
});
describe('setTheme()', () => {
it('calls loadTheme()', () => {
appState.setTheme('custom');
expect(appState.theme).toBe('custom');
expect(window.ElectronFiddle.app.loadTheme).toHaveBeenCalledTimes(1);
});
it('handles a missing theme name', () => {
appState.setTheme();
expect(appState.theme).toBe('');
expect(window.ElectronFiddle.app.loadTheme).toHaveBeenCalledTimes(1);
});
});
describe('dialog helpers', () => {
let dispose: IReactionDisposer;
afterEach(() => {
if (dispose) dispose();
});
function registerDialogHandler(input: string | null, result: boolean) {
dispose = reaction(
() => appState.isGenericDialogShowing,
() => {
appState.genericDialogLastInput = input;
appState.genericDialogLastResult = result;
appState.isGenericDialogShowing = false;
},
);
}
const Input = 'Entropy requires no maintenance.';
const DefaultInput = 'default input';
const Opts = {
label: 'foo',
ok: 'Close',
type: GenericDialogType.warning,
wantsInput: false,
} as const;
describe('showGenericDialog()', () => {
it('shows a dialog', async () => {
const promise = appState.showGenericDialog(Opts);
expect(appState.isGenericDialogShowing).toBe(true);
appState.isGenericDialogShowing = false;
await promise;
});
it('resolves when the dialog is dismissed', async () => {
registerDialogHandler(Input, true);
const promise = appState.showGenericDialog(Opts);
await promise;
expect(appState).toHaveProperty('isGenericDialogShowing', false);
});
it('returns true if confirmed by user', async () => {
registerDialogHandler(Input, true);
const result = await appState.showGenericDialog(Opts);
expect(result).toHaveProperty('confirm', true);
});
it('returns false if rejected by user', async () => {
registerDialogHandler(Input, false);
const result = await appState.showGenericDialog(Opts);
expect(result).toHaveProperty('confirm', false);
});
it('returns the user-inputted text', async () => {
registerDialogHandler(Input, true);
const result = await appState.showGenericDialog({
...Opts,
defaultInput: DefaultInput,
});
expect(result).toHaveProperty('input', Input);
});
it('returns defaultInput as a fallback', async () => {
registerDialogHandler(null, true);
const result = await appState.showGenericDialog({
...Opts,
defaultInput: DefaultInput,
});
expect(result).toHaveProperty('input', DefaultInput);
});
it('returns an empty string as a last resort', async () => {
registerDialogHandler(null, true);
const result = await appState.showGenericDialog(Opts);
expect(result).toHaveProperty('input', '');
});
});
describe('showInputDialog', () => {
const input = 'fnord' as const;
const inputOpts = {
label: 'label',
ok: 'Close',
placeholder: 'Placeholder',
} as const;
it('returns text when confirmed', async () => {
appState.showGenericDialog = jest.fn().mockResolvedValueOnce({
confirm: true,
input,
});
const result = await appState.showInputDialog(inputOpts);
expect(result).toBe(input);
});
it('returns undefined when canceled', async () => {
appState.showGenericDialog = jest.fn().mockResolvedValueOnce({
confirm: false,
input,
});
const result = await appState.showInputDialog(inputOpts);
expect(result).toBeUndefined();
});
});
describe('showConfirmDialog', () => {
const label = 'Do you want to confirm this dialog?';
async function testConfirmDialog(confirm: boolean) {
appState.showGenericDialog = jest.fn().mockResolvedValueOnce({
confirm,
input: undefined,
});
const result = await appState.showConfirmDialog({
label,
ok: 'Confirm',
});
expect(result).toBe(confirm);
}
it('returns true when confirmed', () => testConfirmDialog(true));
it('returns false when canceled', () => testConfirmDialog(false));
});
describe('showErrorDialog', () => {
const label = 'This is an error message.';
it('shows an error dialog', async () => {
appState.showGenericDialog = jest.fn().mockResolvedValueOnce({
confirm: true,
});
await appState.showErrorDialog(label);
expect(appState.showGenericDialog).toHaveBeenCalledWith({
label,
ok: 'Close',
type: GenericDialogType.warning,
wantsInput: false,
});
});
});
describe('showInfoDialog', () => {
const label = 'This is an informational message.';
it('shows an error dialog', async () => {
appState.showGenericDialog = jest.fn().mockResolvedValueOnce({
confirm: true,
});
await appState.showInfoDialog(label);
expect(appState.showGenericDialog).toHaveBeenCalledWith({
label,
ok: 'Close',
type: GenericDialogType.success,
wantsInput: false,
});
});
});
});
describe('addLocalVersion()', () => {
it('refreshes version state', async () => {
const version = '4.0.0';
const ver: RunnableVersion = {
localPath: '/fake/path',
name: 'local-foo',
version,
source: VersionSource.local,
state: InstallState.installed,
};
mocked(getElectronVersions).mockReturnValue([ver]);
appState.addLocalVersion(ver);
// `getElectronVersions` is called when the AppState is initialized
// as well
expect(getElectronVersions).toHaveBeenCalledTimes(2);
expect(appState.getVersion(version)).toStrictEqual(ver);
});
});
describe('signOutGitHub()', () => {
it('resets all GitHub information', () => {
appState.gitHubAvatarUrl = 'test';
appState.gitHubLogin = 'test';
appState.gitHubToken = 'test';
appState.gitHubName = 'test';
appState.signOutGitHub();
expect(appState.gitHubAvatarUrl).toBe(null);
expect(appState.gitHubLogin).toBe(null);
expect(appState.gitHubToken).toBe(null);
expect(appState.gitHubName).toBe(null);
});
});
describe('pushOutput()', () => {
it('takes a fancy buffer and turns it into output', () => {
appState.pushOutput(Buffer.from('hi'));
expect(appState.output[1].text).toBe('hi');
expect(appState.output[1].timeString).toBeTruthy();
});
it('ignores the "Debugger listening on..." output', () => {
appState.pushOutput('Debugger listening on ws://localhost:123');
expect(appState.output.length).toBe(1);
});
it('ignores the "For help, see: ..." output', () => {
appState.pushOutput(
'For help, see: https://nodejs.org/en/docs/inspector',
);
expect(appState.output.length).toBe(1);
});
it('handles a complex buffer on Win32', () => {
overrideRendererPlatform('win32');
appState.pushOutput(Buffer.from('Buffer\r\nStuff\nMore'), {
bypassBuffer: false,
});
expect(appState.output[1].text).toBe('Buffer');
expect(appState.output[2].text).toBe('Stuff');
resetRendererPlatform();
});
});
describe('pushError()', () => {
it('pushes an error', () => {
appState.pushError('Bwap bwap', new Error('Bwap bwap'));
expect(appState.output[1].text).toBe('⚠️ Bwap bwap. Error encountered:');
expect(appState.output[2].text).toBe('Error: Bwap bwap');
});
});
describe('blockAccelerators()', () => {
it('adds an accelerator to be blocked', () => {
appState.acceleratorsToBlock = [];
appState.addAcceleratorToBlock(BlockableAccelerator.save);
expect(appState.acceleratorsToBlock).toEqual([BlockableAccelerator.save]);
appState.addAcceleratorToBlock(BlockableAccelerator.save);
expect(appState.acceleratorsToBlock).toEqual([BlockableAccelerator.save]);
});
it('removes an accelerator to be blocked', () => {
appState.acceleratorsToBlock = [BlockableAccelerator.save];
appState.removeAcceleratorToBlock(BlockableAccelerator.save);
expect(appState.acceleratorsToBlock).toEqual([]);
appState.removeAcceleratorToBlock(BlockableAccelerator.save);
expect(appState.acceleratorsToBlock).toEqual([]);
});
});
describe('title', () => {
const APPNAME = 'Electron Fiddle';
it('defaults to the appname', () => {
const expected = APPNAME;
const actual = appState.title;
expect(actual).toBe(expected);
});
it('flags unsaved fiddles', () => {
const expected = `${APPNAME} - Unsaved`;
appState.editorMosaic.isEdited = true;
const actual = appState.title;
expect(actual).toBe(expected);
});
});
describe('startDownloadingAll()', () => {
it('change isDownloadingAll to true when false', () => {
appState.isDownloadingAll = false;
appState.startDownloadingAll();
expect(appState.isDownloadingAll).toBe(true);
expect(broadcastMessageSpy).toHaveBeenCalledWith({
type: AppStateBroadcastMessageType.isDownloadingAll,
payload: true,
});
});
it('takes no action when isDownloadingAll is true', () => {
appState.isDownloadingAll = true;
appState.startDownloadingAll();
expect(appState.isDownloadingAll).toBe(true);
});
});
describe('stopDownloadingAll()', () => {
it('change isDownloadingAll to false when true', () => {
appState.isDownloadingAll = true;
appState.stopDownloadingAll();
expect(appState.isDownloadingAll).toBe(false);
expect(broadcastMessageSpy).toHaveBeenCalledWith({
type: AppStateBroadcastMessageType.isDownloadingAll,
payload: false,
});
});
it('takes no action when isDownloadingAll is false', () => {
appState.isDownloadingAll = false;
appState.stopDownloadingAll();
expect(appState.isDownloadingAll).toBe(false);
});
});
describe('startDeletingAll()', () => {
it('change isDeletingAll to true when false', () => {
appState.isDeletingAll = false;
appState.startDeletingAll();
expect(appState.isDeletingAll).toBe(true);
});
it('takes no action when isDeletingAll is true', () => {
appState.isDeletingAll = true;
appState.startDeletingAll();
expect(appState.isDeletingAll).toBe(true);
});
});
describe('stopDeletingAll()', () => {
it('change isDeletingAll to false when true', () => {
appState.isDeletingAll = true;
appState.stopDeletingAll();
expect(appState.isDeletingAll).toBe(false);
});
it('takes no action when isDeletingAll is false', () => {
appState.isDeletingAll = false;
appState.stopDeletingAll();
expect(appState.isDeletingAll).toBe(false);
});
});
describe('broadcastChannel', () => {
it('updates the version state in response to changes in other windows', async () => {
const fakeVersion = {
version: '13.9.9',
state: InstallState.downloading,
} as RunnableVersion;
expect(appState.versions[fakeVersion.version]).toBeFalsy();
(appState as any).broadcastChannel.postMessage({
type: AppStateBroadcastMessageType.syncVersions,
payload: [fakeVersion],
});
expect(appState.versions[fakeVersion.version].state).toEqual(
InstallState.downloading,
);
});
});
});