-
Notifications
You must be signed in to change notification settings - Fork 126
/
focus.unit.spec.js
135 lines (120 loc) · 3.67 KB
/
focus.unit.spec.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React from 'react';
import { expect } from 'chai';
import { render, waitFor } from '@testing-library/react';
import { $ } from 'platform/forms-system/src/js/utilities/ui';
import { focusIssue, focusRadioH3, focusAlertH3 } from '../../utils/focus';
import { LAST_ISSUE } from '../../constants';
describe('focusIssue', () => {
afterEach(() => {
window.sessionStorage.removeItem(LAST_ISSUE);
});
const renderPage = () =>
render(
<div id="main">
<h3>Title</h3>
<ul>
<li id="issue-0">
<input />
<a href="#0" className="edit-issue-link">
Edit
</a>
</li>
<li id="issue-1">
<input />
<a href="#1" className="edit-issue-link">
Edit
</a>
</li>
</ul>
<a href="#new" className="add-new-issue">
Add
</a>
</div>,
);
it('should focus on header', async () => {
window.sessionStorage.removeItem(LAST_ISSUE);
const { container } = await renderPage();
await focusIssue(0, container);
await waitFor(() => {
const target = $('h3', container);
expect(document.activeElement).to.eq(target);
});
});
it('should focus on add new issue link', async () => {
window.sessionStorage.setItem(LAST_ISSUE, -1);
const { container } = await renderPage();
await focusIssue(0, container);
await waitFor(() => {
const target = $('.add-new-issue', container);
expect(document.activeElement).to.eq(target);
});
});
it('should focus on second input', async () => {
window.sessionStorage.setItem(LAST_ISSUE, '1,updated');
const { container } = await renderPage();
await focusIssue(0, container);
await waitFor(() => {
const target = $('#issue-1 input', container);
expect(document.activeElement).to.eq(target);
});
});
it('should focus on second edit link', async () => {
window.sessionStorage.setItem(LAST_ISSUE, '1,cancel');
const { container } = await renderPage();
await focusIssue(0, container);
await waitFor(() => {
const target = $('#issue-1 .edit-issue-link', container);
expect(document.activeElement).to.eq(target);
});
});
});
describe('focusRadioH3', () => {
const renderPage = (hasRadio = true) =>
render(
<div id="main">
{hasRadio ? (
<va-radio label-header-level="3" label="test">
<va-radio-option label="1" name="test" value="1" />
<va-radio-option label="2" name="test" value="2" />
</va-radio>
) : (
<div className="nav-header">
<h2 id="nav-form-header">test 2</h2>
</div>
)}
</div>,
);
// h3 is inside shadow DOM, so test isn't working properly with RTL
it.skip('should focus on H3', async () => {
const { container } = await renderPage();
await focusRadioH3();
await waitFor(() => {
const target = $('va-radio', container);
expect(document.activeElement).to.eq(target);
});
});
it('should focus on H2', async () => {
const { container } = await renderPage(false);
await focusRadioH3();
await waitFor(() => {
const target = $('#nav-form-header', container);
expect(document.activeElement).to.eq(target);
});
});
});
describe('focusAlertH3', () => {
const renderPage = () =>
render(
<div id="main">
<h3>test</h3>
</div>,
);
it('should focus on H3', async () => {
const { container } = await renderPage();
await focusAlertH3();
await waitFor(() => {
const target = $('h3', container);
expect(document.activeElement).to.eq(target);
});
});
});