@instructure/ui-text-area 10.2.2-snapshot-15 → 10.2.2-snapshot-17

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.
@@ -0,0 +1,212 @@
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 { fireEvent, render, screen, waitFor } from '@testing-library/react'
27
+ import { vi } from 'vitest'
28
+ import userEvent from '@testing-library/user-event'
29
+ import { runAxeCheck } from '@instructure/ui-axe-check'
30
+ import '@testing-library/jest-dom'
31
+
32
+ import { TextArea } from '../index'
33
+
34
+ describe('TextArea', () => {
35
+ let consoleWarningMock: ReturnType<typeof vi.spyOn>
36
+ let consoleErrorMock: ReturnType<typeof vi.spyOn>
37
+
38
+ beforeEach(() => {
39
+ // Mocking console to prevent test output pollution and expect for messages
40
+ consoleWarningMock = vi
41
+ .spyOn(console, 'warn')
42
+ .mockImplementation(() => {}) as any
43
+ consoleErrorMock = vi
44
+ .spyOn(console, 'error')
45
+ .mockImplementation(() => {}) as any
46
+ })
47
+
48
+ afterEach(() => {
49
+ consoleWarningMock.mockRestore()
50
+ consoleErrorMock.mockRestore()
51
+ })
52
+
53
+ it('should accept a default value', async () => {
54
+ render(<TextArea label="Name" autoGrow={false} defaultValue="Tom Servo" />)
55
+ const input = screen.getByRole('textbox')
56
+
57
+ expect(input).toHaveTextContent('Tom Servo')
58
+ })
59
+
60
+ it('should include a label', async () => {
61
+ const { container } = render(<TextArea label="Name" autoGrow={false} />)
62
+ const textArea = container.querySelector('span[class$="-formFieldLabel"]')
63
+
64
+ expect(textArea).toHaveTextContent('Name')
65
+ })
66
+
67
+ it('should set an initial height', async () => {
68
+ render(<TextArea label="Name" autoGrow={false} height="100px" />)
69
+ const input = screen.getByRole('textbox')
70
+
71
+ expect(input).toHaveStyle('height: 100px')
72
+ })
73
+
74
+ it('should focus the textarea when focus is called', async () => {
75
+ let ref: TextArea
76
+ render(
77
+ <TextArea
78
+ label="Name"
79
+ autoGrow={false}
80
+ // @ts-expect-error this is managed by the testing framework
81
+ textareaRef={(el: TextArea) => (ref = el)}
82
+ />
83
+ )
84
+ const input = screen.getByRole('textbox')
85
+
86
+ ref!.focus()
87
+
88
+ expect(input).toHaveFocus()
89
+ })
90
+
91
+ it('provides a focused getter', async () => {
92
+ let ref: TextArea
93
+ render(
94
+ <TextArea
95
+ label="Name"
96
+ autoGrow={false}
97
+ ref={(el: TextArea) => (ref = el)}
98
+ />
99
+ )
100
+
101
+ ref!.focus()
102
+
103
+ expect(ref!.focused).toBe(true)
104
+ })
105
+
106
+ it('should provide an textareaRef prop', async () => {
107
+ const textareaRef = vi.fn()
108
+ render(<TextArea label="Name" autoGrow={false} textareaRef={textareaRef} />)
109
+ const input = screen.getByRole('textbox')
110
+
111
+ expect(textareaRef).toHaveBeenCalledWith(input)
112
+ })
113
+
114
+ it('should provide a value getter', async () => {
115
+ let ref: TextArea
116
+ render(
117
+ <TextArea
118
+ label="Name"
119
+ autoGrow={false}
120
+ defaultValue="bar"
121
+ // @ts-expect-error this is managed by the testing framework
122
+ textareaRef={(el: TextArea) => (ref = el)}
123
+ />
124
+ )
125
+
126
+ expect(ref!.value).toBe('bar')
127
+ })
128
+
129
+ describe('events', () => {
130
+ it('responds to onChange event', async () => {
131
+ const onChange = vi.fn()
132
+ render(<TextArea label="Name" autoGrow={false} onChange={onChange} />)
133
+ const input = screen.getByRole('textbox')
134
+
135
+ fireEvent.change(input, { target: { value: 'foo' } })
136
+
137
+ await waitFor(() => {
138
+ expect(onChange).toHaveBeenCalledTimes(1)
139
+ })
140
+ })
141
+
142
+ it('does not respond to onChange event when disabled', async () => {
143
+ const onChange = vi.fn()
144
+ render(
145
+ <TextArea disabled label="Name" autoGrow={false} onChange={onChange} />
146
+ )
147
+ const input = screen.getByRole('textbox')
148
+
149
+ fireEvent.change(input, { target: { value: 'foo' } })
150
+
151
+ expect(onChange).not.toHaveBeenCalled()
152
+ })
153
+
154
+ it('does not respond to onChange event when readOnly', async () => {
155
+ const onChange = vi.fn()
156
+ render(
157
+ <TextArea readOnly label="Name" autoGrow={false} onChange={onChange} />
158
+ )
159
+ const input = screen.getByRole('textbox')
160
+
161
+ fireEvent.change(input, { target: { value: 'foo' } })
162
+
163
+ expect(onChange).not.toHaveBeenCalled()
164
+ })
165
+
166
+ it('responds to onBlur event', async () => {
167
+ const onBlur = vi.fn()
168
+ render(<TextArea label="Name" autoGrow={false} onBlur={onBlur} />)
169
+
170
+ userEvent.tab()
171
+ userEvent.tab()
172
+
173
+ await waitFor(() => {
174
+ expect(onBlur).toHaveBeenCalled()
175
+ })
176
+ })
177
+
178
+ it('responds to onFocus event', async () => {
179
+ const onFocus = vi.fn()
180
+ render(<TextArea label="Name" autoGrow={false} onFocus={onFocus} />)
181
+ const input = screen.getByRole('textbox')
182
+
183
+ input.focus()
184
+
185
+ await waitFor(() => {
186
+ expect(onFocus).toHaveBeenCalled()
187
+ })
188
+ })
189
+ })
190
+
191
+ describe('for a11y', () => {
192
+ it('should meet standards', async () => {
193
+ const { container } = render(<TextArea label="Name" autoGrow={false} />)
194
+ const axeCheck = await runAxeCheck(container)
195
+
196
+ expect(axeCheck).toBe(true)
197
+ })
198
+
199
+ it('should set aria-invalid when errors prop is set', async () => {
200
+ render(
201
+ <TextArea
202
+ label="Name"
203
+ autoGrow={false}
204
+ messages={[{ type: 'error', text: 'some error message' }]}
205
+ />
206
+ )
207
+ const input = screen.getByRole('textbox')
208
+
209
+ expect(input).toHaveAttribute('aria-invalid')
210
+ })
211
+ })
212
+ })
@@ -10,6 +10,7 @@
10
10
  { "path": "../debounce/tsconfig.build.json" },
11
11
  { "path": "../emotion/tsconfig.build.json" },
12
12
  { "path": "../shared-types/tsconfig.build.json" },
13
+ { "path": "../ui-axe-check/tsconfig.build.json" },
13
14
  { "path": "../ui-dom-utils/tsconfig.build.json" },
14
15
  { "path": "../ui-form-field/tsconfig.build.json" },
15
16
  { "path": "../ui-prop-types/tsconfig.build.json" },
@@ -19,7 +20,6 @@
19
20
  { "path": "../uid/tsconfig.build.json" },
20
21
  { "path": "../ui-babel-preset/tsconfig.build.json" },
21
22
  { "path": "../ui-color-utils/tsconfig.build.json" },
22
- { "path": "../ui-test-locator/tsconfig.build.json" },
23
23
  { "path": "../ui-test-utils/tsconfig.build.json" },
24
24
  { "path": "../ui-themes/tsconfig.build.json" }
25
25
  ]