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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
import React from "devtools/client/shared/vendor/react";
import { shallow } from "enzyme";
import CommandBar from "../CommandBar";
describe("CommandBar", () => {
it("f8 key command calls props.breakOnNext when not in paused state", () => {
const props = {
breakOnNext: jest.fn(),
resume: jest.fn(),
isPaused: false,
};
const mockEvent = {
preventDefault: jest.fn(),
stopPropagation: jest.fn(),
};
// The "on" spy will see all the keyboard listeners being registered by
// the shortcuts.on function
const context = { shortcuts: { on: jest.fn() } };
shallow(React.createElement(CommandBar.WrappedComponent, props), {
context,
});
// get the keyboard event listeners recorded from the "on" spy.
// this will be an array where each item is itself a two item array
// containing the key code and the corresponding handler for that key code
const keyEventHandlers = context.shortcuts.on.mock.calls;
// simulate pressing the F8 key by calling the F8 handlers
keyEventHandlers
.filter(i => i[0] === "F8")
.forEach(([_, handler]) => {
handler(mockEvent);
});
expect(props.breakOnNext).toHaveBeenCalled();
expect(props.resume).not.toHaveBeenCalled();
});
it("f8 key command calls props.resume when in paused state", () => {
const props = {
breakOnNext: jest.fn(),
resume: jest.fn(),
isPaused: true,
};
const mockEvent = {
preventDefault: jest.fn(),
stopPropagation: jest.fn(),
};
// The "on" spy will see all the keyboard listeners being registered by
// the shortcuts.on function
const context = { shortcuts: { on: jest.fn() } };
shallow(React.createElement(CommandBar.WrappedComponent, props), {
context,
});
// get the keyboard event listeners recorded from the "on" spy.
// this will be an array where each item is itself a two item array
// containing the key code and the corresponding handler for that key code
const keyEventHandlers = context.shortcuts.on.mock.calls;
// simulate pressing the F8 key by calling the F8 handlers
keyEventHandlers
.filter(i => i[0] === "F8")
.forEach(([_, handler]) => {
handler(mockEvent);
});
expect(props.resume).toHaveBeenCalled();
expect(props.breakOnNext).not.toHaveBeenCalled();
});
});
|