@instructure/ui-editable 10.19.2-snapshot-2 → 10.19.2-snapshot-4

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.
@@ -1,225 +0,0 @@
1
- "use strict";
2
-
3
- var _react = require("@testing-library/react");
4
- var _vitest = require("vitest");
5
- require("@testing-library/jest-dom");
6
- var _index = require("../index");
7
- var _runAxeCheck = require("@instructure/ui-axe-check/lib/runAxeCheck.js");
8
- var _jsxRuntime = require("@emotion/react/jsx-runtime");
9
- var _div;
10
- /*
11
- * The MIT License (MIT)
12
- *
13
- * Copyright (c) 2018 - present Instructure, Inc.
14
- *
15
- * Permission is hereby granted, free of charge, to any person obtaining a copy
16
- * of this software and associated documentation files (the "Software"), to deal
17
- * in the Software without restriction, including without limitation the rights
18
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
- * copies of the Software, and to permit persons to whom the Software is
20
- * furnished to do so, subject to the following conditions:
21
- *
22
- * The above copyright notice and this permission notice shall be included in all
23
- * copies or substantial portions of the Software.
24
- *
25
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
- * SOFTWARE.
32
- */
33
- const VIEWER_TEXT = 'viewer-text';
34
- const EDIT_BUTTON_TEXT = 'edit-button-text';
35
- const renderViewer = () => _div || (_div = (0, _jsxRuntime.jsx)("div", {
36
- children: VIEWER_TEXT
37
- }));
38
- const renderEditor = ({
39
- editorRef,
40
- onBlur
41
- }) => {
42
- return (0, _jsxRuntime.jsx)("input", {
43
- "data-testid": "input-editor",
44
- ref: editorRef,
45
- onBlur: onBlur,
46
- defaultValue: "textvalue",
47
- "aria-label": "Editor"
48
- });
49
- };
50
- const renderEditButton = props => {
51
- const elementRef = props.elementRef;
52
- const onClick = props.onClick,
53
- onFocus = props.onFocus,
54
- onBlur = props.onBlur,
55
- isVisible = props.isVisible,
56
- readOnly = props.readOnly;
57
- return _index.InPlaceEdit.renderDefaultEditButton({
58
- isVisible,
59
- readOnly,
60
- label: EDIT_BUTTON_TEXT,
61
- ...{
62
- onClick,
63
- onFocus,
64
- onBlur,
65
- elementRef
66
- }
67
- });
68
- };
69
- describe('<InPlaceEdit />', () => {
70
- let consoleWarningMock;
71
- let consoleErrorMock;
72
- beforeEach(() => {
73
- // Mocking console to prevent test output pollution and expect for messages
74
- consoleWarningMock = _vitest.vi.spyOn(console, 'warn').mockImplementation(() => {});
75
- consoleErrorMock = _vitest.vi.spyOn(console, 'error').mockImplementation(() => {});
76
- });
77
- afterEach(() => {
78
- consoleWarningMock.mockRestore();
79
- consoleErrorMock.mockRestore();
80
- });
81
- it('should render view mode', () => {
82
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
83
- mode: "view",
84
- onChangeMode: _vitest.vi.fn(),
85
- renderViewer: renderViewer,
86
- renderEditor: renderEditor,
87
- renderEditButton: renderEditButton
88
- }));
89
- const viewModeText = _react.screen.getByText(VIEWER_TEXT);
90
- const editButton = _react.screen.getByRole('button');
91
- expect(viewModeText).toBeInTheDocument();
92
- expect(editButton).toBeInTheDocument();
93
- expect(editButton).toHaveTextContent(EDIT_BUTTON_TEXT);
94
- });
95
- it('should render view mode with string button label', () => {
96
- const _render = (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
97
- mode: "view",
98
- onChangeMode: _vitest.vi.fn(),
99
- renderViewer: renderViewer,
100
- renderEditor: renderEditor,
101
- renderEditButton: renderEditButton
102
- })),
103
- container = _render.container;
104
- const viewModeText = _react.screen.getByText(VIEWER_TEXT);
105
- const editButton = _react.screen.getByRole('button');
106
- const buttonLabel = container.querySelector("[class$='-screenReaderContent']");
107
- expect(viewModeText).toBeInTheDocument();
108
- expect(editButton).toBeInTheDocument();
109
- expect(editButton).toContainElement(buttonLabel);
110
- expect(buttonLabel).toHaveTextContent(EDIT_BUTTON_TEXT);
111
- });
112
- it('should render edit mode', () => {
113
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
114
- mode: "edit",
115
- onChangeMode: _vitest.vi.fn(),
116
- renderViewer: renderViewer,
117
- renderEditor: renderEditor,
118
- renderEditButton: renderEditButton
119
- }));
120
- const inputForEdit = _react.screen.getByTestId('input-editor');
121
- expect(inputForEdit).toBeInTheDocument();
122
- expect(document.activeElement).toBe(inputForEdit);
123
- });
124
- it('should render a custom edit button', () => {
125
- var _button;
126
- const customButtonText = 'custom-button-text';
127
- (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
128
- mode: "view",
129
- onChangeMode: _vitest.vi.fn(),
130
- renderViewer: renderViewer,
131
- renderEditor: renderEditor,
132
- renderEditButton: () => {
133
- return _button || (_button = (0, _jsxRuntime.jsx)("button", {
134
- "data-testid": "custom_button",
135
- children: customButtonText
136
- }));
137
- }
138
- }));
139
- const viewModeText = _react.screen.getByText(VIEWER_TEXT);
140
- const customEditButton = _react.screen.getByTestId('custom_button');
141
- expect(viewModeText).toBeInTheDocument();
142
- expect(customEditButton).toBeInTheDocument();
143
- expect(customEditButton).toHaveTextContent(customButtonText);
144
- });
145
- it('should switch mode to edit via props, and focus the editor', () => {
146
- const _render2 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
147
- mode: "view",
148
- onChangeMode: _vitest.vi.fn(),
149
- renderViewer: renderViewer,
150
- renderEditor: renderEditor,
151
- renderEditButton: renderEditButton
152
- })),
153
- rerender = _render2.rerender,
154
- container = _render2.container;
155
- const viewModeText = _react.screen.getByText(VIEWER_TEXT);
156
- expect(viewModeText).toBeInTheDocument();
157
-
158
- // Simulate mode prop change
159
- rerender((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
160
- mode: "edit",
161
- onChangeMode: _vitest.vi.fn(),
162
- renderViewer: renderViewer,
163
- renderEditor: renderEditor,
164
- renderEditButton: renderEditButton
165
- }));
166
- const inputForEdit = _react.screen.getByTestId('input-editor');
167
- expect(container).not.toHaveTextContent(VIEWER_TEXT);
168
- expect(inputForEdit).toBeInTheDocument();
169
- expect(document.activeElement).toBe(inputForEdit);
170
- });
171
- it('should switch mode to view via props, and focus the edit button', () => {
172
- const _render3 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
173
- mode: "edit",
174
- onChangeMode: _vitest.vi.fn(),
175
- renderViewer: renderViewer,
176
- renderEditor: renderEditor,
177
- renderEditButton: renderEditButton
178
- })),
179
- rerender = _render3.rerender,
180
- container = _render3.container;
181
- const inputForEdit = _react.screen.getByTestId('input-editor');
182
- expect(container).not.toHaveTextContent(VIEWER_TEXT);
183
- expect(inputForEdit).toBeInTheDocument();
184
-
185
- // Simulate mode prop change
186
- rerender((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
187
- mode: "view",
188
- onChangeMode: _vitest.vi.fn(),
189
- renderViewer: renderViewer,
190
- renderEditor: renderEditor,
191
- renderEditButton: renderEditButton
192
- }));
193
- const inputForEditAfterModeChange = container.querySelector("[id='input-editor']");
194
- const editButton = _react.screen.getByRole('button');
195
- expect(container).toHaveTextContent(VIEWER_TEXT);
196
- expect(inputForEditAfterModeChange).not.toBeInTheDocument();
197
- expect(editButton).toBeInTheDocument();
198
- expect(editButton).toHaveTextContent(EDIT_BUTTON_TEXT);
199
- expect(document.activeElement).toBe(editButton);
200
- });
201
- it('should meet a11y standards in view mode', async () => {
202
- const _render4 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
203
- mode: "view",
204
- onChangeMode: _vitest.vi.fn(),
205
- renderViewer: renderViewer,
206
- renderEditor: renderEditor,
207
- renderEditButton: renderEditButton
208
- })),
209
- container = _render4.container;
210
- const axeCheck = await (0, _runAxeCheck.runAxeCheck)(container);
211
- expect(axeCheck).toBe(true);
212
- });
213
- it('should meet a11y standards in edit mode', async () => {
214
- const _render5 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.InPlaceEdit, {
215
- mode: "edit",
216
- onChangeMode: _vitest.vi.fn(),
217
- renderViewer: renderViewer,
218
- renderEditor: renderEditor,
219
- renderEditButton: renderEditButton
220
- })),
221
- container = _render5.container;
222
- const axeCheck = await (0, _runAxeCheck.runAxeCheck)(container);
223
- expect(axeCheck).toBe(true);
224
- });
225
- });
@@ -1,271 +0,0 @@
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 { fireEvent, render, screen, waitFor } from '@testing-library/react'
26
- import { vi } from 'vitest'
27
- import type { MockInstance } from 'vitest'
28
- import userEvent from '@testing-library/user-event'
29
- import '@testing-library/jest-dom'
30
-
31
- import { Editable } from '../index'
32
- import type { EditableRenderProps } from '../props'
33
-
34
- const TEXT_VIEW = 'text-view'
35
- const TEXT_EDIT = 'text-edit'
36
-
37
- const childRender = vi.fn(
38
- ({
39
- mode,
40
- getContainerProps,
41
- getViewerProps,
42
- getEditorProps,
43
- getEditButtonProps
44
- }: EditableRenderProps) => {
45
- const { isVisible, buttonRef, ...buttonProps } = getEditButtonProps()
46
- const { onBlur, editorRef } = getEditorProps()
47
- return (
48
- <div data-testid="child-container" {...getContainerProps()}>
49
- {mode === 'view' && <div {...getViewerProps()}>{TEXT_VIEW}</div>}
50
- {mode === 'edit' && (
51
- <input
52
- ref={editorRef}
53
- onBlur={onBlur}
54
- defaultValue="textvalue"
55
- data-testid="edit-mode-input"
56
- />
57
- )}
58
- {
59
- <button
60
- {...buttonProps}
61
- className={isVisible ? 'test-visible' : 'test-hidden'}
62
- >
63
- {TEXT_EDIT}
64
- </button>
65
- }
66
- </div>
67
- )
68
- }
69
- )
70
-
71
- describe('<Editable />', () => {
72
- let consoleWarningMock: ReturnType<typeof vi.spyOn>
73
- let consoleErrorMock: ReturnType<typeof vi.spyOn>
74
-
75
- beforeEach(() => {
76
- // Mocking console to prevent test output pollution and expect for messages
77
- consoleWarningMock = vi
78
- .spyOn(console, 'warn')
79
- .mockImplementation(() => {}) as MockInstance
80
- consoleErrorMock = vi
81
- .spyOn(console, 'error')
82
- .mockImplementation(() => {}) as MockInstance
83
- })
84
-
85
- afterEach(() => {
86
- consoleWarningMock.mockRestore()
87
- consoleErrorMock.mockRestore()
88
- childRender.mockClear()
89
- })
90
-
91
- it('should render view mode', async () => {
92
- render(<Editable mode="view" onChangeMode={vi.fn()} render={childRender} />)
93
- const currentMode = childRender.mock.calls[0][0].mode
94
- const viewModeText = screen.getByText(TEXT_VIEW)
95
- const editButton = screen.getByRole('button', { name: TEXT_EDIT })
96
-
97
- expect(childRender).toHaveBeenCalled()
98
- expect(currentMode).toBe('view')
99
-
100
- expect(viewModeText).toBeInTheDocument()
101
- expect(editButton).toBeInTheDocument()
102
- })
103
-
104
- it('should render edit mode', async () => {
105
- render(<Editable mode="edit" onChangeMode={vi.fn()} render={childRender} />)
106
- const currentMode = childRender.mock.calls[0][0].mode
107
- const inputForEdit = screen.getByTestId('edit-mode-input')
108
- const editButton = screen.getByRole('button', { name: TEXT_EDIT })
109
-
110
- expect(childRender).toHaveBeenCalled()
111
- expect(currentMode).toBe('edit')
112
-
113
- expect(inputForEdit).toBeInTheDocument()
114
- expect(editButton).toBeInTheDocument()
115
- })
116
-
117
- it('should change to edit mode on button click', () => {
118
- const onChangeModeSpy = vi.fn()
119
-
120
- render(
121
- <Editable
122
- mode="view"
123
- onChangeMode={onChangeModeSpy}
124
- render={childRender}
125
- />
126
- )
127
- const viewModeText = screen.getByText(TEXT_VIEW)
128
- const editButton = screen.getByRole('button')
129
-
130
- expect(viewModeText).toBeInTheDocument()
131
-
132
- fireEvent.click(editButton)
133
-
134
- expect(onChangeModeSpy).toHaveBeenCalledWith('edit')
135
- })
136
-
137
- it('should change to edit mode on component click', async () => {
138
- const onChangeModeSpy = vi.fn()
139
-
140
- render(
141
- <Editable
142
- mode="view"
143
- onChangeMode={onChangeModeSpy}
144
- render={childRender}
145
- />
146
- )
147
- const childContainer = screen.getByTestId('child-container')
148
-
149
- userEvent.click(childContainer)
150
-
151
- await waitFor(() => expect(onChangeModeSpy).toHaveBeenCalledWith('edit'))
152
- })
153
-
154
- it('should set the button to visible on mouse over', async () => {
155
- const onChangeModeSpy = vi.fn()
156
-
157
- render(
158
- <Editable
159
- mode="view"
160
- onChangeMode={onChangeModeSpy}
161
- render={childRender}
162
- />
163
- )
164
- const editButton = screen.getByRole('button')
165
-
166
- expect(editButton).toHaveClass('test-hidden')
167
-
168
- fireEvent.mouseOver(editButton)
169
-
170
- await waitFor(() => {
171
- expect(editButton).toHaveClass('test-visible')
172
- })
173
-
174
- fireEvent.mouseOut(editButton)
175
-
176
- await waitFor(() => {
177
- expect(editButton).toHaveClass('test-hidden')
178
- })
179
- })
180
-
181
- it('should change to view mode on editor blur', () => {
182
- const onChangeModeSpy = vi.fn()
183
-
184
- render(
185
- <Editable
186
- mode="edit"
187
- onChangeMode={onChangeModeSpy}
188
- render={childRender}
189
- />
190
- )
191
- const inputForEdit = screen.getByTestId('edit-mode-input')
192
-
193
- fireEvent.focus(inputForEdit)
194
- fireEvent.blur(inputForEdit)
195
-
196
- expect(onChangeModeSpy).toHaveBeenCalledWith('view')
197
- })
198
-
199
- it('should change to view mode on escape', () => {
200
- const onChangeModeSpy = vi.fn()
201
-
202
- render(
203
- <Editable
204
- mode="edit"
205
- onChangeMode={onChangeModeSpy}
206
- render={childRender}
207
- />
208
- )
209
- const inputForEdit = screen.getByTestId('edit-mode-input')
210
-
211
- fireEvent.keyUp(inputForEdit, { key: 'Escape', code: 'Escape' })
212
-
213
- expect(onChangeModeSpy).toHaveBeenCalledWith('view')
214
- })
215
-
216
- it('should call onChange when the user is finished editing', () => {
217
- const onChangeSpy = vi.fn()
218
-
219
- const { rerender } = render(
220
- <Editable
221
- mode="edit"
222
- onChangeMode={vi.fn()}
223
- render={childRender}
224
- value="initial_value"
225
- onChange={onChangeSpy}
226
- />
227
- )
228
- const inputForEdit = screen.getByTestId('edit-mode-input')
229
-
230
- fireEvent.change(inputForEdit, { target: { value: 'updated_value' } })
231
-
232
- expect(onChangeSpy).not.toHaveBeenCalled()
233
-
234
- // Simulate mode prop change
235
- rerender(
236
- <Editable
237
- mode="view"
238
- onChangeMode={vi.fn()}
239
- render={childRender}
240
- value="updated_value"
241
- onChange={onChangeSpy}
242
- />
243
- )
244
-
245
- expect(onChangeSpy).toHaveBeenCalledWith('updated_value')
246
- })
247
-
248
- it('should warn if readOnly + mode="edit"', () => {
249
- const consoleWarningSpy = vi
250
- .spyOn(console, 'warn')
251
- .mockImplementation(() => {})
252
-
253
- render(
254
- <Editable
255
- mode="edit"
256
- onChangeMode={vi.fn()}
257
- render={childRender}
258
- value="foo"
259
- readOnly
260
- />
261
- )
262
- const expectedErrorMessage = 'When readOnly is true, mode must be "view"'
263
-
264
- expect(consoleWarningSpy).toHaveBeenCalledWith(
265
- expect.stringContaining(expectedErrorMessage),
266
- expect.any(String)
267
- )
268
-
269
- consoleWarningSpy.mockRestore()
270
- })
271
- })