-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathKeyboardShortcut.test.tsx
More file actions
73 lines (56 loc) · 2.5 KB
/
KeyboardShortcut.test.tsx
File metadata and controls
73 lines (56 loc) · 2.5 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
/* 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 { Provider } from 'react-redux';
import { render } from 'firefox-profiler/test/fixtures/testing-library';
import createStore from 'firefox-profiler/app-logic/create-store';
import { KeyboardShortcut } from 'firefox-profiler/components/app/KeyboardShortcut';
import { fireFullKeyPress } from 'firefox-profiler/test/fixtures/utils';
import { coerce } from 'firefox-profiler/utils/types';
import { mockRaf } from '../fixtures/mocks/request-animation-frame';
describe('app/KeyboardShortcut', function () {
function setup() {
const store = createStore();
const renderResults = render(
<Provider store={store}>
<KeyboardShortcut wrapperClassName="exampleClassName">
<div>Content</div>
<button type="button">Click me</button>
</KeyboardShortcut>
</Provider>
);
return { ...renderResults, ...store };
}
it('can open and close when hitting ? and Escape respectively', () => {
const { getByText, queryByText } = setup();
expect(queryByText('Keyboard shortcuts')).not.toBeInTheDocument();
// Show the dialog.
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '?' });
expect(getByText('Keyboard shortcuts')).toBeInTheDocument();
// Hide the dialog.
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: 'Escape' });
expect(queryByText('Keyboard shortcuts')).not.toBeInTheDocument();
});
it('matches the snapshot when closed', () => {
const { container } = setup();
expect(container).toMatchSnapshot();
});
it('matches the snapshot when open', () => {
const { container } = setup();
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '?' });
expect(container).toMatchSnapshot();
});
it('reverts to the previous active element', () => {
const flushRafCalls = mockRaf();
const { getByRole } = setup();
const clickMe = getByRole('button');
clickMe.focus();
expect(clickMe).toHaveFocus();
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '?' });
flushRafCalls(); // The focus is changed after react render.
expect(getByRole('dialog')).toHaveFocus();
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: 'Escape' });
flushRafCalls(); // The focus is changed after react render.
expect(clickMe).toHaveFocus();
});
});