-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathconfirm-quit.test.ts
More file actions
129 lines (113 loc) · 4.1 KB
/
Copy pathconfirm-quit.test.ts
File metadata and controls
129 lines (113 loc) · 4.1 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
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
import test from 'ava';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const proxyquire = require('proxyquire').noCallThru();
const loadModule = (dialogStub?: {showMessageBoxSync: (...args: any[]) => number}) =>
proxyquire('../../app/utils/confirm-quit', {
electron: {
app: {
getLastFocusedWindow: () => null
},
dialog: dialogStub ?? {
showMessageBoxSync: () => 0
}
}
});
const makeSession = (overrides: {pty?: any; ended?: boolean} = {}) => ({
pty: 'pty' in overrides ? overrides.pty : {pid: 123},
ended: overrides.ended ?? false
});
const makeWindow = (sessions: Array<ReturnType<typeof makeSession>>) => {
const map = new Map<string, ReturnType<typeof makeSession>>();
sessions.forEach((s, i) => map.set(`uid-${i}`, s));
return {sessions: map} as any;
};
test('hasRunningProcess returns true when any session has a live pty', (t) => {
const {hasRunningProcess} = loadModule();
const win = makeWindow([makeSession()]);
t.true(hasRunningProcess([win]));
});
test('hasRunningProcess returns false when sessions are ended', (t) => {
const {hasRunningProcess} = loadModule();
const win = makeWindow([makeSession({ended: true})]);
t.false(hasRunningProcess([win]));
});
test('hasRunningProcess returns false when no windows', (t) => {
const {hasRunningProcess} = loadModule();
t.false(hasRunningProcess([]));
});
test('hasRunningProcess returns false when sessions have no pty', (t) => {
const {hasRunningProcess} = loadModule();
const win = makeWindow([makeSession({pty: null})]);
t.false(hasRunningProcess([win]));
});
test("shouldAllowQuit allows quit when mode is 'never'", (t) => {
const {shouldAllowQuit} = loadModule();
let dialogCalls = 0;
const allow = shouldAllowQuit('never', [makeWindow([makeSession()])], () => {
dialogCalls += 1;
return false;
});
t.true(allow);
t.is(dialogCalls, 0);
});
test("shouldAllowQuit prompts when mode is 'always'", (t) => {
const {shouldAllowQuit} = loadModule();
let dialogCalls = 0;
// No running sessions, but mode is 'always' so the dialog should still fire.
const allow = shouldAllowQuit('always', [makeWindow([makeSession({ended: true})])], () => {
dialogCalls += 1;
return true;
});
t.true(allow);
t.is(dialogCalls, 1);
});
test("shouldAllowQuit returns false when user cancels in 'always' mode", (t) => {
const {shouldAllowQuit} = loadModule();
const allow = shouldAllowQuit('always', [makeWindow([makeSession()])], () => false);
t.false(allow);
});
test("shouldAllowQuit skips dialog in 'on-running-process' when nothing runs", (t) => {
const {shouldAllowQuit} = loadModule();
let dialogCalls = 0;
const allow = shouldAllowQuit('on-running-process', [makeWindow([makeSession({ended: true})])], () => {
dialogCalls += 1;
return true;
});
t.true(allow);
t.is(dialogCalls, 0);
});
test("shouldAllowQuit prompts in 'on-running-process' when something runs", (t) => {
const {shouldAllowQuit} = loadModule();
let dialogCalls = 0;
const allow = shouldAllowQuit('on-running-process', [makeWindow([makeSession()])], () => {
dialogCalls += 1;
return true;
});
t.true(allow);
t.is(dialogCalls, 1);
});
test('shouldAllowQuit falls back to default when mode is unknown', (t) => {
const {shouldAllowQuit} = loadModule();
let dialogCalls = 0;
// Unknown mode + running process -> behaves like 'on-running-process'.
const allow = shouldAllowQuit('bogus' as any, [makeWindow([makeSession()])], () => {
dialogCalls += 1;
return false;
});
t.false(allow);
t.is(dialogCalls, 1);
});
test('showQuitConfirmDialog returns true when user picks Quit', (t) => {
const {showQuitConfirmDialog} = loadModule({
showMessageBoxSync: () => 1
});
t.true(showQuitConfirmDialog());
});
test('showQuitConfirmDialog returns false when user picks Cancel', (t) => {
const {showQuitConfirmDialog} = loadModule({
showMessageBoxSync: () => 0
});
t.false(showQuitConfirmDialog());
});