Skip to content

Commit

Permalink
add hasFocus mock
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Jan 9, 2025
1 parent c7995ba commit aab1fc6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 8 additions & 0 deletions build-tools/jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ if (typeof window !== 'undefined') {
require('@testing-library/jest-dom/extend-expect');
const { cleanup } = require('@testing-library/react');
afterEach(cleanup);

// Autosuggest input blur handler uses document.hasFocus() to optimize user experience when focus leaves the page.
// In JSDOM the hasFocus() always returns false which differs from the in-browser experience, so setting it to true using mock.
// See: https://github.com/jsdom/jsdom/issues/3794
beforeEach(() => {
// eslint-disable-next-line no-undef
jest.spyOn(window.document, 'hasFocus').mockImplementation(() => true);
});
}
2 changes: 0 additions & 2 deletions src/autosuggest/__tests__/autosuggest.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ test('entered text option should not get screenreader override', () => {
});

test('should close dropdown on blur when document is in focus', () => {
jest.spyOn(document, 'hasFocus').mockImplementation(() => true);

const { wrapper } = renderAutosuggest(<Autosuggest enteredTextLabel={v => v} value="1" options={defaultOptions} />);
wrapper.findNativeInput().focus();
expect(wrapper.findDropdown().findOpenDropdown()).not.toBe(null);
Expand Down

0 comments on commit aab1fc6

Please sign in to comment.