-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathaccessability.test.tsx
93 lines (82 loc) · 2.91 KB
/
accessability.test.tsx
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
import React from 'react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom/extend-expect';
import Popup from '../src';
import { PopupProps } from '../src/types';
const SimplePopup = ({
children = <span> popup Content </span>,
...props
}: Partial<PopupProps>) => (
<Popup trigger={<button> trigger </button>} {...props}>
{children}
</Popup>
);
describe('Popup Positions ', () => {
test('tooltip with focus should be open on Tab', async () => {
render(<SimplePopup on="focus" />);
expect(document.body).toHaveFocus();
userEvent.tab();
expect(screen.getByText(/trigger/)).toHaveFocus();
await waitFor(
() => expect(screen.getByRole('tooltip')).toBeInTheDocument(),
{
timeout: 200,
}
);
});
test('On Modal Open we should set Focus first focusable element ', async () => {
render(
<SimplePopup modal>
<button data-testid="b1"> button 1</button>
<button data-testid="b2"> button 2</button>
<button data-testid="b3"> button 3</button>
</SimplePopup>
);
expect(document.body).toHaveFocus();
userEvent.tab();
expect(screen.getByText(/trigger/)).toHaveFocus();
userEvent.click(screen.getByText(/trigger/));
expect(screen.getByRole('dialog')).toBeInTheDocument();
expect(screen.getByTestId('b1')).toHaveFocus();
});
test('On Modal Open we should Prevent Tabbing to Outside the Modal ', async () => {
render(
<SimplePopup modal>
<button data-testid="b1"> button 1</button>
<button data-testid="b2"> button 2</button>
<button data-testid="b3"> button 3</button>
</SimplePopup>
);
expect(document.body).toHaveFocus();
userEvent.tab();
expect(screen.getByText(/trigger/)).toHaveFocus();
userEvent.click(screen.getByText(/trigger/));
expect(screen.getByRole('dialog')).toBeInTheDocument();
expect(screen.getByTestId('b1')).toHaveFocus();
userEvent.tab();
expect(screen.getByTestId('b2')).toHaveFocus();
userEvent.tab();
expect(screen.getByTestId('b3')).toHaveFocus();
userEvent.tab();
expect(screen.getByTestId('b1')).toHaveFocus();
userEvent.tab({ shift: true });
expect(screen.getByTestId('b3')).toHaveFocus();
});
test('On Close it should focus the latest focused element ', async () => {
render(
<div>
<button data-testid="b1"> button 1</button>
<SimplePopup modal />
</div>
);
expect(document.body).toHaveFocus();
userEvent.tab();
expect(screen.getByTestId('b1')).toHaveFocus();
userEvent.click(screen.getByText(/trigger/));
expect(screen.getByRole('dialog')).toBeInTheDocument();
fireEvent.keyUp(document, { key: 'Escape', code: 'Escape' });
expect(screen.queryByRole('dialog')).toBeNull();
expect(screen.getByText(/trigger/)).toHaveFocus();
});
});