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