@instructure/ui-text-input 10.0.1-snapshot-7 → 10.0.1-snapshot-8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/es/TextInput/__new-tests__/TextInput.test.js +221 -0
- package/lib/TextInput/__new-tests__/TextInput.test.js +223 -0
- package/package.json +20 -16
- package/src/TextInput/__new-tests__/TextInput.test.tsx +235 -0
- package/tsconfig.build.json +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/TextInput/__new-tests__/TextInput.test.d.ts +2 -0
- package/types/TextInput/__new-tests__/TextInput.test.d.ts.map +1 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import React from 'react'
|
|
26
|
+
import { render, screen, waitFor, fireEvent } from '@testing-library/react'
|
|
27
|
+
import userEvent from '@testing-library/user-event'
|
|
28
|
+
import { vi } from 'vitest'
|
|
29
|
+
import '@testing-library/jest-dom'
|
|
30
|
+
|
|
31
|
+
import { runAxeCheck } from '@instructure/ui-axe-check'
|
|
32
|
+
import { TextInput } from '../index'
|
|
33
|
+
|
|
34
|
+
describe('<TextInput/>', () => {
|
|
35
|
+
let consoleErrorMock: any
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
// Mocking console to prevent test output pollution and expect for messages
|
|
39
|
+
consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
consoleErrorMock.mockRestore()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should include a label', async () => {
|
|
47
|
+
const { container } = render(<TextInput renderLabel="Name" />)
|
|
48
|
+
const label = container.querySelector('label')
|
|
49
|
+
expect(label).toHaveTextContent('Name')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('should focus the input when focus is called', async () => {
|
|
53
|
+
let ref: TextInput | undefined
|
|
54
|
+
render(
|
|
55
|
+
<TextInput
|
|
56
|
+
renderLabel="Name"
|
|
57
|
+
//@ts-expect-error TODO this is coming from ReactComponentWrapper
|
|
58
|
+
inputRef={(el: TextInput) => {
|
|
59
|
+
ref = el
|
|
60
|
+
}}
|
|
61
|
+
/>
|
|
62
|
+
)
|
|
63
|
+
const input = screen.getByRole('textbox')
|
|
64
|
+
|
|
65
|
+
ref?.focus()
|
|
66
|
+
|
|
67
|
+
expect(input).toHaveFocus()
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should provide an inputRef prop', async () => {
|
|
71
|
+
const inputRef = vi.fn()
|
|
72
|
+
render(<TextInput renderLabel="Name" inputRef={inputRef} />)
|
|
73
|
+
const input = screen.getByRole('textbox')
|
|
74
|
+
|
|
75
|
+
expect(inputRef).toHaveBeenCalledWith(input)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('should provide a value getter', async () => {
|
|
79
|
+
let ref: TextInput | undefined
|
|
80
|
+
render(
|
|
81
|
+
<TextInput
|
|
82
|
+
renderLabel="Name"
|
|
83
|
+
defaultValue="bar"
|
|
84
|
+
//@ts-expect-error TODO this is coming from ReactComponentWrapper
|
|
85
|
+
inputRef={(el: TextInput) => {
|
|
86
|
+
ref = el
|
|
87
|
+
}}
|
|
88
|
+
/>
|
|
89
|
+
)
|
|
90
|
+
expect(ref?.value).toBe('bar')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should provide messageId to FormField', async () => {
|
|
94
|
+
render(
|
|
95
|
+
<TextInput
|
|
96
|
+
renderLabel="Name"
|
|
97
|
+
messages={[
|
|
98
|
+
{
|
|
99
|
+
text: 'yup',
|
|
100
|
+
type: 'error'
|
|
101
|
+
}
|
|
102
|
+
]}
|
|
103
|
+
/>
|
|
104
|
+
)
|
|
105
|
+
const input = screen.getByRole('textbox')
|
|
106
|
+
|
|
107
|
+
expect(input).toHaveAttribute('aria-describedby')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('should have equal messagesId and aria-describedby values', async () => {
|
|
111
|
+
const { container } = render(
|
|
112
|
+
<TextInput
|
|
113
|
+
renderLabel="Name"
|
|
114
|
+
messages={[
|
|
115
|
+
{
|
|
116
|
+
text: 'yup',
|
|
117
|
+
type: 'error'
|
|
118
|
+
}
|
|
119
|
+
]}
|
|
120
|
+
/>
|
|
121
|
+
)
|
|
122
|
+
const id = screen.getByRole('textbox').getAttribute('aria-describedby')
|
|
123
|
+
const messages = container.querySelector(
|
|
124
|
+
'span[class$="-formFieldMessages"]'
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
expect(messages).toHaveAttribute('id', id)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('should handle multiple aria-describedby ids', async () => {
|
|
131
|
+
render(
|
|
132
|
+
<TextInput
|
|
133
|
+
renderLabel="Name"
|
|
134
|
+
aria-describedby="assistive-id"
|
|
135
|
+
messages={[
|
|
136
|
+
{
|
|
137
|
+
text: 'yup',
|
|
138
|
+
type: 'error'
|
|
139
|
+
}
|
|
140
|
+
]}
|
|
141
|
+
/>
|
|
142
|
+
)
|
|
143
|
+
const ids = screen.getByRole('textbox').getAttribute('aria-describedby')
|
|
144
|
+
|
|
145
|
+
expect(ids).toMatch('assistive-id TextInput-messages')
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
describe('events', () => {
|
|
149
|
+
it('responds to onChange event', async () => {
|
|
150
|
+
const onChange = vi.fn()
|
|
151
|
+
render(<TextInput renderLabel="Name" onChange={onChange} />)
|
|
152
|
+
const input = screen.getByRole('textbox')
|
|
153
|
+
fireEvent.change(input, { target: { value: 'foo' } })
|
|
154
|
+
|
|
155
|
+
await waitFor(() => {
|
|
156
|
+
expect(onChange).toHaveBeenCalledTimes(1)
|
|
157
|
+
})
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('responds to onBlur event', async () => {
|
|
161
|
+
const onBlur = vi.fn()
|
|
162
|
+
render(<TextInput renderLabel="Name" onBlur={onBlur} />)
|
|
163
|
+
|
|
164
|
+
userEvent.tab()
|
|
165
|
+
userEvent.tab()
|
|
166
|
+
|
|
167
|
+
await waitFor(() => {
|
|
168
|
+
expect(onBlur).toHaveBeenCalled()
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('responds to onFocus event', async () => {
|
|
173
|
+
const onFocus = vi.fn()
|
|
174
|
+
render(<TextInput renderLabel="Name" onFocus={onFocus} />)
|
|
175
|
+
const input = screen.getByRole('textbox')
|
|
176
|
+
|
|
177
|
+
input.focus()
|
|
178
|
+
|
|
179
|
+
await waitFor(() => {
|
|
180
|
+
expect(onFocus).toHaveBeenCalled()
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('interaction', () => {
|
|
186
|
+
it('should set the disabled attribute when `interaction` is disabled', async () => {
|
|
187
|
+
render(<TextInput renderLabel="Name" interaction="disabled" />)
|
|
188
|
+
const input = screen.getByRole('textbox')
|
|
189
|
+
|
|
190
|
+
expect(input).toHaveAttribute('disabled')
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('should set the disabled attribute when `disabled` is set', async () => {
|
|
194
|
+
render(<TextInput renderLabel="Name" disabled />)
|
|
195
|
+
const input = screen.getByRole('textbox')
|
|
196
|
+
|
|
197
|
+
expect(input).toHaveAttribute('disabled')
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
it('should set the readonly attribute when `interaction` is readonly', async () => {
|
|
201
|
+
render(<TextInput renderLabel="Name" interaction="readonly" />)
|
|
202
|
+
const input = screen.getByRole('textbox')
|
|
203
|
+
|
|
204
|
+
expect(input).toHaveAttribute('readonly')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('should set the readonly attribute when `readOnly` is set', async () => {
|
|
208
|
+
render(<TextInput renderLabel="Name" readOnly />)
|
|
209
|
+
const input = screen.getByRole('textbox')
|
|
210
|
+
|
|
211
|
+
expect(input).toHaveAttribute('readonly')
|
|
212
|
+
})
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
describe('for a11y', () => {
|
|
216
|
+
it('should meet standards', async () => {
|
|
217
|
+
const { container } = render(<TextInput renderLabel="Name" />)
|
|
218
|
+
const axeCheck = await runAxeCheck(container)
|
|
219
|
+
|
|
220
|
+
expect(axeCheck).toBe(true)
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
it('should set aria-invalid when errors prop is set', async () => {
|
|
224
|
+
render(
|
|
225
|
+
<TextInput
|
|
226
|
+
renderLabel="Name"
|
|
227
|
+
messages={[{ type: 'error', text: 'some error message' }]}
|
|
228
|
+
/>
|
|
229
|
+
)
|
|
230
|
+
const input = screen.getByRole('textbox')
|
|
231
|
+
|
|
232
|
+
expect(input).toHaveAttribute('aria-invalid')
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
})
|
package/tsconfig.build.json
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
{ "path": "../ui-themes/tsconfig.build.json" },
|
|
15
15
|
{ "path": "../emotion/tsconfig.build.json" },
|
|
16
16
|
{ "path": "../shared-types/tsconfig.build.json" },
|
|
17
|
+
{ "path": "../ui-axe-check/tsconfig.build.json" },
|
|
17
18
|
{ "path": "../ui-dom-utils/tsconfig.build.json" },
|
|
18
19
|
{ "path": "../ui-form-field/tsconfig.build.json" },
|
|
19
20
|
{ "path": "../ui-icons/tsconfig.build.json" },
|