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