Skip to content

Commit

Permalink
feat(overlay): test focus lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Coread committed Sep 28, 2023
1 parent 6424de4 commit a332a9a
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/components/Overlay/Overlay.unit.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React from 'react';
import { mount } from 'enzyme';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';

import Overlay, { OVERLAY_CONSTANTS as CONSTANTS } from './';

Expand Down Expand Up @@ -142,4 +145,78 @@ describe('<Overlay />', () => {
expect(element.getAttribute('data-fullscreen')).toBe(`${fullscreen}`);
});
});

describe('actions', () => {
it('should not lock focus if no focusLockProps are supplied', async () => {
expect.assertions(5);

const Component = () => {
return (
<>
<button>button</button>
<Overlay>
<button>button</button>
<button>button</button>
</Overlay>
</>
);
};

const user = userEvent.setup();

render(<Component />);

const buttons = await screen.findAllByText('button');

expect(document.body).toHaveFocus();

await user.tab();

expect(buttons[0]).toHaveFocus();

await user.tab();

expect(buttons[1]).toHaveFocus();

await user.tab();

expect(buttons[2]).toHaveFocus();

await user.tab();

expect(document.body).toHaveFocus();
});

it('should lock focus around the children', async () => {
expect.assertions(3);

const Component = () => {
return (
<>
<button>button</button>
<Overlay focusLockProps={{}}>
<button>button</button>
<button>button</button>
</Overlay>
</>
);
};

const user = userEvent.setup();

render(<Component />);

const buttons = await screen.findAllByText('button');

expect(buttons[1]).toHaveFocus();

await user.tab();

expect(buttons[2]).toHaveFocus();

await user.tab();

expect(buttons[1]).toHaveFocus();
});
});
});

0 comments on commit a332a9a

Please sign in to comment.