Skip to content

Commit

Permalink
Improve focus trap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardHelm committed Feb 24, 2025
1 parent 665e651 commit 293bcd0
Showing 1 changed file with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,33 +595,35 @@ describe('vwc-inline-time-picker', () => {

expect(isScrolledIntoView(getPickerItem('hours', '00'))).toBe(true);
});
});

describe('focus trap support', () => {
const originalIgnoreEvent = TrappedFocus.ignoreEvent;
beforeEach(() => {
TrappedFocus.ignoreEvent = vi.fn();
});
afterEach(() => {
TrappedFocus.ignoreEvent = originalIgnoreEvent;
});

it('should submit Tab keydown events that move focus internally to be ignored by focus traps', () => {
(element.shadowRoot!.querySelector('#hours') as HTMLElement).focus();

const event = pressKey('Tab');

expect(TrappedFocus.ignoreEvent).toHaveBeenCalledTimes(1);
expect(TrappedFocus.ignoreEvent).toHaveBeenCalledWith(event);
});

it('should not submit Tab keydown events that move focus out', () => {
(element.shadowRoot!.querySelector('#hours') as HTMLElement).focus();
pressKey('Tab', { shiftKey: true });
(element.shadowRoot!.querySelector('#minutes') as HTMLElement).focus();
pressKey('Tab');

expect(TrappedFocus.ignoreEvent).not.toHaveBeenCalled();
describe('focus trap support', () => {
const originalIgnoreEvent = TrappedFocus.ignoreEvent;
beforeEach(() => {
TrappedFocus.ignoreEvent = vi.fn();
});
afterEach(() => {
TrappedFocus.ignoreEvent = originalIgnoreEvent;
});

it('should call focus trap ignoreEvent on tab keydown when focused on non-terminal picker element', () => {
(element.shadowRoot!.querySelector('#hours') as HTMLElement).focus();

const event = pressKey('Tab');

expect(TrappedFocus.ignoreEvent).toHaveBeenCalledTimes(1);
expect(TrappedFocus.ignoreEvent).toHaveBeenCalledWith(event);
});

it('should not call focus trap ignoreEvent on tab keydown when focused on terminal picker element', () => {
(element.shadowRoot!.querySelector('#hours') as HTMLElement).focus();
pressKey('Tab', { shiftKey: true });
(
element.shadowRoot!.querySelector('#minutes') as HTMLElement
).focus();
pressKey('Tab');

expect(TrappedFocus.ignoreEvent).not.toHaveBeenCalled();
});
});
});
});
Expand Down

0 comments on commit 293bcd0

Please sign in to comment.