@instructure/ui-editable 8.50.1-snapshot-1 → 8.50.1-snapshot-13

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,237 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2018 - 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 } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+
29
+ import { InPlaceEdit } from '../index'
30
+ import type { InPlaceEditProps } from '../props'
31
+ import { runAxeCheck } from '@instructure/ui-axe-check'
32
+
33
+ const VIEWER_TEXT = 'viewer-text'
34
+ const EDIT_BUTTON_TEXT = 'edit-button-text'
35
+
36
+ const renderViewer = () => <div>{VIEWER_TEXT}</div>
37
+ const renderEditor: InPlaceEditProps['renderEditor'] = ({
38
+ editorRef,
39
+ onBlur
40
+ }) => {
41
+ // eslint-disable-line react/prop-types
42
+ return (
43
+ <input
44
+ data-testid="input-editor"
45
+ ref={editorRef}
46
+ onBlur={onBlur}
47
+ defaultValue="textvalue"
48
+ aria-label="Editor"
49
+ />
50
+ )
51
+ }
52
+ const renderEditButton: InPlaceEditProps['renderEditButton'] = (props) => {
53
+ const elementRef = props.elementRef as (el: Element | null) => void
54
+ const { onClick, onFocus, onBlur, isVisible, readOnly } = props
55
+ return InPlaceEdit.renderDefaultEditButton({
56
+ isVisible,
57
+ readOnly,
58
+ label: EDIT_BUTTON_TEXT,
59
+ ...{ onClick, onFocus, onBlur, elementRef }
60
+ })
61
+ }
62
+
63
+ describe('<InPlaceEdit />', () => {
64
+ it('should render view mode', () => {
65
+ render(
66
+ <InPlaceEdit
67
+ mode="view"
68
+ onChangeMode={jest.fn()}
69
+ renderViewer={renderViewer}
70
+ renderEditor={renderEditor}
71
+ renderEditButton={renderEditButton}
72
+ />
73
+ )
74
+ const viewModeText = screen.getByText(VIEWER_TEXT)
75
+ const editButton = screen.getByRole('button')
76
+
77
+ expect(viewModeText).toBeInTheDocument()
78
+ expect(editButton).toBeInTheDocument()
79
+ expect(editButton).toHaveTextContent(EDIT_BUTTON_TEXT)
80
+ })
81
+
82
+ it('should render view mode with string button label', () => {
83
+ const { container } = render(
84
+ <InPlaceEdit
85
+ mode="view"
86
+ onChangeMode={jest.fn()}
87
+ renderViewer={renderViewer}
88
+ renderEditor={renderEditor}
89
+ renderEditButton={renderEditButton}
90
+ />
91
+ )
92
+ const viewModeText = screen.getByText(VIEWER_TEXT)
93
+ const editButton = screen.getByRole('button')
94
+ const buttonLabel = container.querySelector(
95
+ "[class$='-screenReaderContent']"
96
+ ) as HTMLElement
97
+
98
+ expect(viewModeText).toBeInTheDocument()
99
+ expect(editButton).toBeInTheDocument()
100
+ expect(editButton).toContainElement(buttonLabel!)
101
+ expect(buttonLabel).toHaveTextContent(EDIT_BUTTON_TEXT)
102
+ })
103
+
104
+ it('should render edit mode', () => {
105
+ render(
106
+ <InPlaceEdit
107
+ mode="edit"
108
+ onChangeMode={jest.fn()}
109
+ renderViewer={renderViewer}
110
+ renderEditor={renderEditor}
111
+ renderEditButton={renderEditButton}
112
+ />
113
+ )
114
+ const inputForEdit = screen.getByTestId('input-editor')
115
+
116
+ expect(inputForEdit).toBeInTheDocument()
117
+ expect(document.activeElement).toBe(inputForEdit)
118
+ })
119
+
120
+ it('should render a custom edit button', () => {
121
+ const customButtonText = 'custom-button-text'
122
+ render(
123
+ <InPlaceEdit
124
+ mode="view"
125
+ onChangeMode={jest.fn()}
126
+ renderViewer={renderViewer}
127
+ renderEditor={renderEditor}
128
+ renderEditButton={() => {
129
+ return <button data-testid="custom_button">{customButtonText}</button>
130
+ }}
131
+ />
132
+ )
133
+ const viewModeText = screen.getByText(VIEWER_TEXT)
134
+ const customEditButton = screen.getByTestId('custom_button')
135
+
136
+ expect(viewModeText).toBeInTheDocument()
137
+ expect(customEditButton).toBeInTheDocument()
138
+ expect(customEditButton).toHaveTextContent(customButtonText)
139
+ })
140
+
141
+ it('should switch mode to edit via props, and focus the editor', () => {
142
+ const { rerender, container } = render(
143
+ <InPlaceEdit
144
+ mode="view"
145
+ onChangeMode={jest.fn()}
146
+ renderViewer={renderViewer}
147
+ renderEditor={renderEditor}
148
+ renderEditButton={renderEditButton}
149
+ />
150
+ )
151
+ const viewModeText = screen.getByText(VIEWER_TEXT)
152
+ expect(viewModeText).toBeInTheDocument()
153
+
154
+ // Simulate mode prop change
155
+ rerender(
156
+ <InPlaceEdit
157
+ mode="edit"
158
+ onChangeMode={jest.fn()}
159
+ renderViewer={renderViewer}
160
+ renderEditor={renderEditor}
161
+ renderEditButton={renderEditButton}
162
+ />
163
+ )
164
+ const inputForEdit = screen.getByTestId('input-editor')
165
+
166
+ expect(container).not.toHaveTextContent(VIEWER_TEXT)
167
+ expect(inputForEdit).toBeInTheDocument()
168
+ expect(document.activeElement).toBe(inputForEdit)
169
+ })
170
+
171
+ it('should switch mode to view via props, and focus the edit button', () => {
172
+ const { rerender, container } = render(
173
+ <InPlaceEdit
174
+ mode="edit"
175
+ onChangeMode={jest.fn()}
176
+ renderViewer={renderViewer}
177
+ renderEditor={renderEditor}
178
+ renderEditButton={renderEditButton}
179
+ />
180
+ )
181
+ const inputForEdit = screen.getByTestId('input-editor')
182
+
183
+ expect(container).not.toHaveTextContent(VIEWER_TEXT)
184
+ expect(inputForEdit).toBeInTheDocument()
185
+
186
+ // Simulate mode prop change
187
+ rerender(
188
+ <InPlaceEdit
189
+ mode="view"
190
+ onChangeMode={jest.fn()}
191
+ renderViewer={renderViewer}
192
+ renderEditor={renderEditor}
193
+ renderEditButton={renderEditButton}
194
+ />
195
+ )
196
+ const inputForEditAfterModeChange = container.querySelector(
197
+ "[id='input-editor']"
198
+ )
199
+ const editButton = screen.getByRole('button')
200
+
201
+ expect(container).toHaveTextContent(VIEWER_TEXT)
202
+ expect(inputForEditAfterModeChange).not.toBeInTheDocument()
203
+ expect(editButton).toBeInTheDocument()
204
+ expect(editButton).toHaveTextContent(EDIT_BUTTON_TEXT)
205
+ expect(document.activeElement).toBe(editButton)
206
+ })
207
+
208
+ it('should meet a11y standards in view mode', async () => {
209
+ const { container } = render(
210
+ <InPlaceEdit
211
+ mode="view"
212
+ onChangeMode={jest.fn()}
213
+ renderViewer={renderViewer}
214
+ renderEditor={renderEditor}
215
+ renderEditButton={renderEditButton}
216
+ />
217
+ )
218
+ const axeCheck = await runAxeCheck(container)
219
+
220
+ expect(axeCheck).toBe(true)
221
+ })
222
+
223
+ it('should meet a11y standards in edit mode', async () => {
224
+ const { container } = render(
225
+ <InPlaceEdit
226
+ mode="edit"
227
+ onChangeMode={jest.fn()}
228
+ renderViewer={renderViewer}
229
+ renderEditor={renderEditor}
230
+ renderEditButton={renderEditButton}
231
+ />
232
+ )
233
+ const axeCheck = await runAxeCheck(container)
234
+
235
+ expect(axeCheck).toBe(true)
236
+ })
237
+ })
@@ -8,7 +8,6 @@
8
8
  "include": ["src"],
9
9
  "references": [
10
10
  { "path": "../ui-babel-preset/tsconfig.build.json" },
11
- { "path": "../ui-test-utils/tsconfig.build.json" },
12
11
  { "path": "../console/tsconfig.build.json" },
13
12
  { "path": "../emotion/tsconfig.build.json" },
14
13
  { "path": "../ui-a11y-content/tsconfig.build.json" },
@@ -18,6 +17,7 @@
18
17
  { "path": "../ui-focusable/tsconfig.build.json" },
19
18
  { "path": "../ui-icons/tsconfig.build.json" },
20
19
  { "path": "../ui-utils/tsconfig.build.json" },
21
- { "path": "../ui-view/tsconfig.build.json" }
20
+ { "path": "../ui-view/tsconfig.build.json" },
21
+ { "path": "../ui-axe-check/tsconfig.build.json" }
22
22
  ]
23
23
  }