-
Notifications
You must be signed in to change notification settings - Fork 484
Expand file tree
/
Copy pathPanelSearch.test.tsx
More file actions
117 lines (90 loc) · 3.68 KB
/
Copy pathPanelSearch.test.tsx
File metadata and controls
117 lines (90 loc) · 3.68 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
/* 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 { fireEvent, screen } from '@testing-library/react';
import { render } from 'firefox-profiler/test/fixtures/testing-library';
import { PanelSearch } from 'firefox-profiler/components/shared/PanelSearch';
import { fireFullKeyPress } from 'firefox-profiler/test/fixtures/utils';
import { coerce } from 'firefox-profiler/utils/types';
describe('shared/PanelSearch', function () {
function setup({ alsoFocusOnF = false }: { alsoFocusOnF?: boolean } = {}) {
const onSearch = jest.fn();
const renderResult = render(
<PanelSearch
className="testPanelSearch"
label="Filter:"
title="Filter description"
currentSearchString=""
onSearch={onSearch}
alsoFocusOnF={alsoFocusOnF}
/>
);
return { ...renderResult, onSearch };
}
function getSearchInput(): HTMLInputElement {
return screen.getByRole('searchbox') as HTMLInputElement;
}
it('focuses the search input when the / key is pressed outside any input', () => {
setup();
const input = getSearchInput();
expect(input).not.toHaveFocus();
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '/' });
expect(input).toHaveFocus();
});
it('does not steal the / key when focus is already inside an input', () => {
setup();
const input = getSearchInput();
// Add a separate input that will hold focus.
const otherInput = document.createElement('input');
otherInput.type = 'text';
document.body.appendChild(otherInput);
otherInput.focus();
expect(otherInput).toHaveFocus();
// Fire the keydown event from the focused input. The listener should
// bail out and leave focus where it was.
fireEvent.keyDown(otherInput, { key: '/' });
expect(otherInput).toHaveFocus();
expect(input).not.toHaveFocus();
document.body.removeChild(otherInput);
});
it('does not react to / combined with a modifier key', () => {
setup();
const input = getSearchInput();
expect(input).not.toHaveFocus();
fireEvent.keyDown(window, { key: '/', ctrlKey: true });
expect(input).not.toHaveFocus();
fireEvent.keyDown(window, { key: '/', metaKey: true });
expect(input).not.toHaveFocus();
fireEvent.keyDown(window, { key: '/', altKey: true });
expect(input).not.toHaveFocus();
});
it('focuses the search input when the f key is pressed and alsoFocusOnF is true', () => {
setup({ alsoFocusOnF: true });
const input = getSearchInput();
expect(input).not.toHaveFocus();
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: 'f' });
expect(input).toHaveFocus();
});
it('does not focus the search input on the f key when alsoFocusOnF is not set', () => {
setup();
const input = getSearchInput();
expect(input).not.toHaveFocus();
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: 'f' });
expect(input).not.toHaveFocus();
});
it('still uses / for focus even when alsoFocusOnF is true', () => {
setup({ alsoFocusOnF: true });
const input = getSearchInput();
expect(input).not.toHaveFocus();
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '/' });
expect(input).toHaveFocus();
});
it('removes its global key listener on unmount', () => {
const { unmount } = setup();
const input = getSearchInput();
unmount();
// After unmount, pressing / should be a no-op (no focus jump back).
fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '/' });
expect(input).not.toHaveFocus();
});
});