|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { fireEvent, screen } from '@testing-library/react'; |
| 6 | + |
| 7 | +import { render } from 'firefox-profiler/test/fixtures/testing-library'; |
| 8 | +import { PanelSearch } from 'firefox-profiler/components/shared/PanelSearch'; |
| 9 | +import { fireFullKeyPress } from 'firefox-profiler/test/fixtures/utils'; |
| 10 | +import { coerce } from 'firefox-profiler/utils/types'; |
| 11 | + |
| 12 | +describe('shared/PanelSearch', function () { |
| 13 | + function setup({ alsoFocusOnF = false }: { alsoFocusOnF?: boolean } = {}) { |
| 14 | + const onSearch = jest.fn(); |
| 15 | + const renderResult = render( |
| 16 | + <PanelSearch |
| 17 | + className="testPanelSearch" |
| 18 | + label="Filter:" |
| 19 | + title="Filter description" |
| 20 | + currentSearchString="" |
| 21 | + onSearch={onSearch} |
| 22 | + alsoFocusOnF={alsoFocusOnF} |
| 23 | + /> |
| 24 | + ); |
| 25 | + return { ...renderResult, onSearch }; |
| 26 | + } |
| 27 | + |
| 28 | + function getSearchInput(): HTMLInputElement { |
| 29 | + return screen.getByRole('searchbox') as HTMLInputElement; |
| 30 | + } |
| 31 | + |
| 32 | + it('focuses the search input when the / key is pressed outside any input', () => { |
| 33 | + setup(); |
| 34 | + const input = getSearchInput(); |
| 35 | + expect(input).not.toHaveFocus(); |
| 36 | + |
| 37 | + fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '/' }); |
| 38 | + |
| 39 | + expect(input).toHaveFocus(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('does not steal the / key when focus is already inside an input', () => { |
| 43 | + setup(); |
| 44 | + const input = getSearchInput(); |
| 45 | + |
| 46 | + // Add a separate input that will hold focus. |
| 47 | + const otherInput = document.createElement('input'); |
| 48 | + otherInput.type = 'text'; |
| 49 | + document.body.appendChild(otherInput); |
| 50 | + otherInput.focus(); |
| 51 | + expect(otherInput).toHaveFocus(); |
| 52 | + |
| 53 | + // Fire the keydown event from the focused input. The listener should |
| 54 | + // bail out and leave focus where it was. |
| 55 | + fireEvent.keyDown(otherInput, { key: '/' }); |
| 56 | + |
| 57 | + expect(otherInput).toHaveFocus(); |
| 58 | + expect(input).not.toHaveFocus(); |
| 59 | + |
| 60 | + document.body.removeChild(otherInput); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not react to / combined with a modifier key', () => { |
| 64 | + setup(); |
| 65 | + const input = getSearchInput(); |
| 66 | + expect(input).not.toHaveFocus(); |
| 67 | + |
| 68 | + fireEvent.keyDown(window, { key: '/', ctrlKey: true }); |
| 69 | + expect(input).not.toHaveFocus(); |
| 70 | + |
| 71 | + fireEvent.keyDown(window, { key: '/', metaKey: true }); |
| 72 | + expect(input).not.toHaveFocus(); |
| 73 | + |
| 74 | + fireEvent.keyDown(window, { key: '/', altKey: true }); |
| 75 | + expect(input).not.toHaveFocus(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('focuses the search input when the f key is pressed and alsoFocusOnF is true', () => { |
| 79 | + setup({ alsoFocusOnF: true }); |
| 80 | + const input = getSearchInput(); |
| 81 | + expect(input).not.toHaveFocus(); |
| 82 | + |
| 83 | + fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: 'f' }); |
| 84 | + |
| 85 | + expect(input).toHaveFocus(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('does not focus the search input on the f key when alsoFocusOnF is not set', () => { |
| 89 | + setup(); |
| 90 | + const input = getSearchInput(); |
| 91 | + expect(input).not.toHaveFocus(); |
| 92 | + |
| 93 | + fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: 'f' }); |
| 94 | + |
| 95 | + expect(input).not.toHaveFocus(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('still uses / for focus even when alsoFocusOnF is true', () => { |
| 99 | + setup({ alsoFocusOnF: true }); |
| 100 | + const input = getSearchInput(); |
| 101 | + expect(input).not.toHaveFocus(); |
| 102 | + |
| 103 | + fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '/' }); |
| 104 | + |
| 105 | + expect(input).toHaveFocus(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('removes its global key listener on unmount', () => { |
| 109 | + const { unmount } = setup(); |
| 110 | + const input = getSearchInput(); |
| 111 | + unmount(); |
| 112 | + |
| 113 | + // After unmount, pressing / should be a no-op (no focus jump back). |
| 114 | + fireFullKeyPress(coerce<Window, HTMLElement>(window), { key: '/' }); |
| 115 | + expect(input).not.toHaveFocus(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments