/
eventHandlers.spec.ts
312 lines (256 loc) · 10.7 KB
/
eventHandlers.spec.ts
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Framework Emulator Github:
// https://github.com/Microsoft/BotFramwork-Emulator
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import { SharedConstants } from '@bfemulator/app-shared';
import { CommandServiceImpl, CommandServiceInstance } from '@bfemulator/sdk-shared';
import { globalHandlers } from './eventHandlers';
const {
Commands: {
Electron: { ToggleDevTools },
Notifications,
UI: { ShowBotCreationDialog, ShowOpenBotDialog },
},
} = SharedConstants;
let mockLocalCommandsCalled = [];
let mockRemoteCommandsCalled = [];
const mockCurrentWebContents = {
setZoomLevel: jest.fn(),
getZoomFactor: jest.fn(),
setZoomFactor: jest.fn(),
};
const mockCurrentWindow = {
isFullScreen: () => false,
setFullScreen: jest.fn(),
};
jest.mock('electron', () => ({
ipcMain: new Proxy(
{},
{
get(): any {
return () => ({});
},
has() {
return true;
},
}
),
ipcRenderer: new Proxy(
{},
{
get(): any {
return () => ({});
},
has() {
return true;
},
}
),
remote: {
getCurrentWebContents: () => mockCurrentWebContents,
getCurrentWindow: () => mockCurrentWindow,
},
}));
const mockDOM = `
<ul class="app-menu">
<button id="file-btn">File</button>
<button id="edit-btn">Edit</button>
</ul>
<nav>
<button id="navBtn">NavBtn</button>
</nav>
<main>
<button id="btn1">btn 1</button>
<button id="btn2">btn 2</button>
<div>
<button id="btn3">btn 3</button>
</div>
</main>
`;
describe('#globalHandlers', () => {
let commandService: CommandServiceImpl;
beforeAll(() => {
const decorator = CommandServiceInstance();
const descriptor = decorator({ descriptor: {} }, 'none') as any;
commandService = descriptor.descriptor.get();
commandService.call = (commandName: string, ...args: any[]) => {
mockLocalCommandsCalled.push({ commandName, args: args });
return true as any;
};
commandService.remoteCall = (commandName: string, ...args: any[]) => {
mockRemoteCommandsCalled.push({ commandName, args: args });
return true as any;
};
});
beforeEach(() => {
mockLocalCommandsCalled = [];
mockRemoteCommandsCalled = [];
});
afterAll(() => {
jest.restoreAllMocks();
});
it('calls ShowOpenBotDialog when CMD+O is pressed', async () => {
const event = new KeyboardEvent('keydown', { metaKey: true, key: 'o' });
await globalHandlers(event);
expect(mockLocalCommandsCalled.length).toBe(1);
expect(mockLocalCommandsCalled[0].commandName).toBe(ShowOpenBotDialog);
});
it('calls ShowOpenBotDialog when CTRL+O is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: 'O' });
await globalHandlers(event);
expect(mockLocalCommandsCalled.length).toBe(1);
expect(mockLocalCommandsCalled[0].commandName).toBe(ShowOpenBotDialog);
});
it('calls ShowBotCreationDialog when CMD+N is pressed', async () => {
const event = new KeyboardEvent('keydown', { metaKey: true, key: 'n' });
await globalHandlers(event);
expect(mockLocalCommandsCalled.length).toBe(1);
expect(mockLocalCommandsCalled[0].commandName).toBe(ShowBotCreationDialog);
});
it('calls ShowBotCreationDialog when CTRL+N is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: 'N' });
await globalHandlers(event);
expect(mockLocalCommandsCalled.length).toBe(1);
expect(mockLocalCommandsCalled[0].commandName).toBe(ShowBotCreationDialog);
});
it("calls nothing with a keydown it doesn't care about", async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: 'y' });
await globalHandlers(event);
expect(mockLocalCommandsCalled.length).toBe(0);
});
it('should send a notification if a command fails', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: 'N' });
const spy = jest.spyOn(commandService, 'call').mockRejectedValueOnce('oh noes!');
await globalHandlers(event);
expect(spy).toHaveBeenLastCalledWith(Notifications.Add, {
message: 'oh noes!',
type: 1,
});
});
it('should reset the zoom level when Ctrl+0 is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: '0' });
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomLevel).toHaveBeenCalledWith(0);
});
it('should zoom in when Ctrl+= is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: '=' });
// standard zoom from 1 to 1.1 zoom factor
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(1.1);
// trying to zoom past the max zoom factor (3) should not go above 3
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(3.1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(3);
});
it('should zoom in when Ctrl+Shift+= is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: '+' });
// standard zoom from 1 to 1.1 zoom factor
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(1.1);
// trying to zoom past the max zoom factor (3) should not go above 3
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(3.1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(3);
});
it('should zoom out when Ctrl+- is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: '-' });
// standard zoom from 1 to 0.9 zoom factor
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(0.9);
// trying to zoom past the minimum zoom factor (0.25) should not go below 0,25
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(0.1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(0.25);
});
it('should zoom out when Ctrl+Shift+- is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, key: '_' });
// standard zoom from 1 to 0.9 zoom factor
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(0.9);
// trying to zoom past the minimum zoom factor (0.25) should not go below 0,25
mockCurrentWebContents.setZoomFactor.mockClear();
mockCurrentWebContents.getZoomFactor.mockImplementation(callback => callback(0.1));
await globalHandlers(event);
expect(mockCurrentWebContents.setZoomFactor).toHaveBeenCalledWith(0.25);
});
it('should toggle fullscreen when F11 is pressed', async () => {
const event = new KeyboardEvent('keydown', { key: 'f11' });
await globalHandlers(event);
expect(mockCurrentWindow.setFullScreen).toHaveBeenCalledWith(!mockCurrentWindow.isFullScreen());
});
it('should toggle dev tools when Ctrl+Shit+I is pressed', async () => {
const event = new KeyboardEvent('keydown', { ctrlKey: true, shiftKey: true, key: 'i' });
await globalHandlers(event);
expect(mockRemoteCommandsCalled).toHaveLength(1);
expect(mockRemoteCommandsCalled[0].commandName).toBe(ToggleDevTools);
});
it('should move focus to first element when Tab is pressed on Mac', async () => {
const event = new KeyboardEvent('keydown', { key: 'Tab' });
Object.defineProperty(process, 'platform', { value: 'darwin' });
document.body.innerHTML = mockDOM;
var mockFirstElement = document.getElementById('navBtn');
var mockLastElement = document.getElementById('btn3');
mockLastElement.focus();
await globalHandlers(event);
expect(document.activeElement.id).toBe(mockFirstElement.id);
});
it('should move focus to first element when Tab is pressed on Linux', async () => {
const event = new KeyboardEvent('keydown', { key: 'Tab' });
Object.defineProperty(process, 'platform', { value: 'linux' });
document.body.innerHTML = mockDOM;
var mockFirstElement = document.getElementById('file-btn');
var mockLastElement = document.getElementById('btn3');
mockLastElement.focus();
await globalHandlers(event);
expect(document.activeElement.id).toBe(mockFirstElement.id);
});
it('should move focus to last element when Shift+Tab is pressed', async () => {
const event = new KeyboardEvent('keydown', { shiftKey: true, key: 'Tab' });
Object.defineProperty(process, 'platform', { value: 'darwin' });
document.body.innerHTML = mockDOM;
var mockFirstElement = document.getElementById('navBtn');
var mockLastElement = document.getElementById('btn3');
mockFirstElement.focus();
await globalHandlers(event);
expect(document.activeElement.id).toBe(mockLastElement.id);
});
});