-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathform-field-rendering.test.tsx
More file actions
234 lines (196 loc) · 10.1 KB
/
form-field-rendering.test.tsx
File metadata and controls
234 lines (196 loc) · 10.1 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import { render } from '@testing-library/react';
import FormField, { FormFieldProps } from '../../../lib/components/form-field';
import createWrapper, { FormFieldWrapper } from '../../../lib/components/test-utils/dom';
import screenreaderOnlyStyles from '../../../lib/components/internal/components/screenreader-only/styles.css.js';
import liveRegionStyles from '../../../lib/components/live-region/test-classes/styles.css.js';
function renderFormField(props: FormFieldProps = {}) {
const { container, rerender } = render(<FormField {...props} />);
const wrapper = createWrapper(container).findFormField()!;
return { wrapper, rerender: (props: FormFieldProps) => rerender(<FormField {...props} />) };
}
function findDebouncedCharacterCount(wrapper: FormFieldWrapper): HTMLElement | undefined {
return wrapper.findByClassName(screenreaderOnlyStyles.root)?.getElement();
}
describe('FormField component', () => {
describe('Basic rendering', () => {
[
{ slot: 'label', finder: (wrapper: FormFieldWrapper) => wrapper.findLabel() },
{ slot: 'description', finder: (wrapper: FormFieldWrapper) => wrapper.findDescription() },
{ slot: 'info', finder: (wrapper: FormFieldWrapper) => wrapper.findInfo() },
{ slot: 'constraintText', finder: (wrapper: FormFieldWrapper) => wrapper.findConstraint() },
{ slot: 'errorText', finder: (wrapper: FormFieldWrapper) => wrapper.findError() },
{ slot: 'warningText', finder: (wrapper: FormFieldWrapper) => wrapper.findWarning() },
{ slot: 'children', finder: (wrapper: FormFieldWrapper) => wrapper.findControl() },
{ slot: 'secondaryControl', finder: (wrapper: FormFieldWrapper) => wrapper.findSecondaryControl() },
].forEach(({ slot, finder }) => {
describe(`${slot}`, () => {
test(`displays empty ${slot} when not set`, () => {
const { wrapper } = renderFormField({});
expect(finder(wrapper)).toBeNull();
});
test(`displays empty ${slot} when set to empty`, () => {
const props: any = {};
props[slot] = '';
const { wrapper } = renderFormField(props);
expect(finder(wrapper)).toBeNull();
});
test(`renders a ${slot} when a string is passed`, () => {
const value = 'this is a string';
const props: any = {};
props[slot] = value;
const { wrapper } = renderFormField(props);
expect(finder(wrapper)?.getElement()).toHaveTextContent(value);
});
test(`renders a ${slot} when jsx is passed`, () => {
const value = (
<div>
this is a <strong>formatted</strong> value
</div>
);
const props: any = {};
props[slot] = value;
const { wrapper } = renderFormField(props);
expect(finder(wrapper)?.getElement()).toHaveTextContent('this is a formatted value');
expect(finder(wrapper)?.getElement()).toContainHTML('<div>this is a <strong>formatted</strong> value</div>');
});
test(`${slot} re-renders content correctly`, () => {
let wrapper: FormFieldWrapper;
const renderResult = render(<FormField />);
wrapper = createWrapper(renderResult.container).findFormField()!;
expect(finder(wrapper)).toBeNull();
const value = 'this is a string';
const props: any = {};
props[slot] = value;
renderResult.rerender(<FormField {...props} />);
wrapper = createWrapper(renderResult.container).findFormField()!;
expect(finder(wrapper)?.getElement()).not.toBeNull();
});
});
});
});
test('label is rendered with semantic DOM element', () => {
const testLabel = 'Label Unit Test';
const { wrapper } = renderFormField({
label: testLabel,
});
const labelElement = wrapper.findLabel();
expect(labelElement).not.toBeNull();
expect(labelElement?.getElement()).toHaveTextContent(testLabel);
expect(labelElement?.getElement().tagName).toBe('LABEL');
});
test('errorIcon has an accessible text alternative', () => {
const errorText = 'Yikes, that is just plan wrong';
const errorIconAriaLabel = 'Error';
const { wrapper } = renderFormField({
errorText,
i18nStrings: { errorIconAriaLabel },
});
const errorLabel = wrapper.find(`:scope [aria-label]`);
expect(errorLabel?.getElement()).not.toBeNull();
expect(errorLabel?.getElement()).toHaveAttribute('aria-label', errorIconAriaLabel);
});
test('warningIcon has an accessible text alternative', () => {
const warningText = 'You sure?';
const warningIconAriaLabel = 'Warning';
const { wrapper } = renderFormField({
warningText,
i18nStrings: { warningIconAriaLabel },
});
const warningLabel = wrapper.find(`:scope [aria-label]`);
expect(warningLabel?.getElement()).not.toBeNull();
expect(warningLabel?.getElement()).toHaveAttribute('aria-label', warningIconAriaLabel);
});
test('constraintText region displays constraint content text when error-text is also set', () => {
const constraintText = 'let this be a lesson to you';
const errorText = 'wrong, do it again';
const { wrapper } = renderFormField({
constraintText,
errorText,
});
expect(wrapper.findConstraint()?.getElement()).toHaveTextContent(constraintText);
expect(wrapper.findError()?.getElement()).toHaveTextContent(errorText);
});
test('constraintText region displays constraint content text when warning-text is also set', () => {
const constraintText = 'think twice';
const warningText = 'warning you, check once again';
const { wrapper } = renderFormField({
constraintText,
warningText,
});
expect(wrapper.findConstraint()?.getElement()).toHaveTextContent(constraintText);
expect(wrapper.findWarning()?.getElement()).toHaveTextContent(warningText);
});
describe('live-region', () => {
test('Should render live region for error text', () => {
const errorText = 'Nope do it again';
const errorIconAriaLabel = 'Error';
renderFormField({ errorText, i18nStrings: { errorIconAriaLabel } });
// Since live region in this componennt uses 'source' prop
// it is too complex to successfully assert the aria live message
expect(createWrapper().findByClassName(liveRegionStyles.announcer)?.getElement()).toBeInTheDocument();
});
test('Should render live region for warning text', () => {
const warningText = 'Are you sure?';
const warningIconAriaLabel = 'Warning';
renderFormField({ warningText, i18nStrings: { warningIconAriaLabel } });
// Since live region in this componennt uses 'source' prop
// it is too complex to successfully assert the aria live message
expect(createWrapper().findByClassName(liveRegionStyles.announcer)?.getElement()).toBeInTheDocument();
});
});
describe('characterCountText', () => {
test('does not render wrapper element when not set', () => {
const { wrapper } = renderFormField({});
expect(wrapper.findCharacterCount()).toBeNull();
});
test('does not render wrapper element when set to empty', () => {
const { wrapper } = renderFormField({ characterCountText: '' });
expect(wrapper.findCharacterCount()).toBeNull();
});
test('renders characterCountText when a string is passed', () => {
const { wrapper } = renderFormField({ characterCountText: 'this is a string' });
expect(wrapper.findCharacterCount()!.getElement()).toHaveTextContent('this is a string');
});
describe('debouncing', () => {
const DEBOUNCE_TIME_MS = 1000;
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());
test('renders characterCountText directly on initial render', () => {
const { wrapper } = renderFormField({ characterCountText: 'this is a string' });
expect(wrapper.findCharacterCount()!.getElement()).toHaveTextContent('this is a string');
});
test("wrapper.findCharacterCount() doesn't return the debounced version of the slot", () => {
const { wrapper, rerender } = renderFormField({ characterCountText: 'this is a string' });
expect(wrapper.findCharacterCount()!.getElement()).toHaveTextContent('this is a string');
rerender({ characterCountText: 'another string' });
expect(wrapper.findCharacterCount()!.getElement()).toHaveTextContent('another string');
rerender({ characterCountText: '' });
expect(wrapper.findCharacterCount()).toBeNull();
});
test('delays updates until debounce duration', async () => {
const { wrapper, rerender } = renderFormField({ characterCountText: 'Character count: 5/10' });
rerender({ characterCountText: 'Character count: 6/10' });
expect(findDebouncedCharacterCount(wrapper)!).toHaveTextContent('Character count: 5/10');
await jest.advanceTimersByTimeAsync(DEBOUNCE_TIME_MS);
expect(findDebouncedCharacterCount(wrapper)!).toHaveTextContent('Character count: 6/10');
});
test('restarts timer if a new update happened during debounce duration', async () => {
const { wrapper, rerender } = renderFormField({ characterCountText: 'Character count: 5/10' });
// Rerender and wait 500ms: the text should not update
rerender({ characterCountText: 'Character count: 6/10' });
await jest.advanceTimersByTimeAsync(DEBOUNCE_TIME_MS / 2);
expect(findDebouncedCharacterCount(wrapper)!).toHaveTextContent('Character count: 5/10');
// Rerender and wait 500ms: the text should not update
rerender({ characterCountText: 'Character count: 7/10' });
await jest.advanceTimersByTimeAsync(DEBOUNCE_TIME_MS / 2);
expect(findDebouncedCharacterCount(wrapper)!).toHaveTextContent('Character count: 5/10');
// Wait another 500ms (1s since last update): the text should update
await jest.advanceTimersByTimeAsync(DEBOUNCE_TIME_MS / 2);
expect(findDebouncedCharacterCount(wrapper)!).toHaveTextContent('Character count: 7/10');
});
});
});
});