@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.
- package/CHANGELOG.md +1 -1
- package/es/Editable/__new-tests__/Editable.test.js +204 -0
- package/es/InPlaceEdit/__new-tests__/InPlaceEdit.test.js +210 -0
- package/lib/Editable/__new-tests__/Editable.test.js +206 -0
- package/lib/InPlaceEdit/__new-tests__/InPlaceEdit.test.js +212 -0
- package/package.json +16 -13
- package/src/Editable/README.md +2 -3
- package/src/Editable/__new-tests__/Editable.test.tsx +259 -0
- package/src/InPlaceEdit/README.md +4 -8
- package/src/InPlaceEdit/__new-tests__/InPlaceEdit.test.tsx +237 -0
- package/tsconfig.build.json +2 -2
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/Editable/__new-tests__/Editable.test.d.ts +2 -0
- package/types/Editable/__new-tests__/Editable.test.d.ts.map +1 -0
- package/types/InPlaceEdit/__new-tests__/InPlaceEdit.test.d.ts +2 -0
- package/types/InPlaceEdit/__new-tests__/InPlaceEdit.test.d.ts.map +1 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
var _react = _interopRequireDefault(require("react"));
|
|
5
|
+
var _react2 = require("@testing-library/react");
|
|
6
|
+
require("@testing-library/jest-dom");
|
|
7
|
+
var _index = require("../index");
|
|
8
|
+
var _runAxeCheck = require("@instructure/ui-axe-check/lib/runAxeCheck.js");
|
|
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 = /*#__PURE__*/_react.default.createElement("div", null, VIEWER_TEXT));
|
|
36
|
+
const renderEditor = ({
|
|
37
|
+
editorRef,
|
|
38
|
+
onBlur
|
|
39
|
+
}) => {
|
|
40
|
+
// eslint-disable-line react/prop-types
|
|
41
|
+
return /*#__PURE__*/_react.default.createElement("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 _index.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
|
+
it('should render view mode', () => {
|
|
70
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
71
|
+
mode: "view",
|
|
72
|
+
onChangeMode: jest.fn(),
|
|
73
|
+
renderViewer: renderViewer,
|
|
74
|
+
renderEditor: renderEditor,
|
|
75
|
+
renderEditButton: renderEditButton
|
|
76
|
+
}));
|
|
77
|
+
const viewModeText = _react2.screen.getByText(VIEWER_TEXT);
|
|
78
|
+
const editButton = _react2.screen.getByRole('button');
|
|
79
|
+
expect(viewModeText).toBeInTheDocument();
|
|
80
|
+
expect(editButton).toBeInTheDocument();
|
|
81
|
+
expect(editButton).toHaveTextContent(EDIT_BUTTON_TEXT);
|
|
82
|
+
});
|
|
83
|
+
it('should render view mode with string button label', () => {
|
|
84
|
+
const _render = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
85
|
+
mode: "view",
|
|
86
|
+
onChangeMode: jest.fn(),
|
|
87
|
+
renderViewer: renderViewer,
|
|
88
|
+
renderEditor: renderEditor,
|
|
89
|
+
renderEditButton: renderEditButton
|
|
90
|
+
})),
|
|
91
|
+
container = _render.container;
|
|
92
|
+
const viewModeText = _react2.screen.getByText(VIEWER_TEXT);
|
|
93
|
+
const editButton = _react2.screen.getByRole('button');
|
|
94
|
+
const buttonLabel = container.querySelector("[class$='-screenReaderContent']");
|
|
95
|
+
expect(viewModeText).toBeInTheDocument();
|
|
96
|
+
expect(editButton).toBeInTheDocument();
|
|
97
|
+
expect(editButton).toContainElement(buttonLabel);
|
|
98
|
+
expect(buttonLabel).toHaveTextContent(EDIT_BUTTON_TEXT);
|
|
99
|
+
});
|
|
100
|
+
it('should render edit mode', () => {
|
|
101
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
102
|
+
mode: "edit",
|
|
103
|
+
onChangeMode: jest.fn(),
|
|
104
|
+
renderViewer: renderViewer,
|
|
105
|
+
renderEditor: renderEditor,
|
|
106
|
+
renderEditButton: renderEditButton
|
|
107
|
+
}));
|
|
108
|
+
const inputForEdit = _react2.screen.getByTestId('input-editor');
|
|
109
|
+
expect(inputForEdit).toBeInTheDocument();
|
|
110
|
+
expect(document.activeElement).toBe(inputForEdit);
|
|
111
|
+
});
|
|
112
|
+
it('should render a custom edit button', () => {
|
|
113
|
+
var _button;
|
|
114
|
+
const customButtonText = 'custom-button-text';
|
|
115
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
116
|
+
mode: "view",
|
|
117
|
+
onChangeMode: jest.fn(),
|
|
118
|
+
renderViewer: renderViewer,
|
|
119
|
+
renderEditor: renderEditor,
|
|
120
|
+
renderEditButton: () => {
|
|
121
|
+
return _button || (_button = /*#__PURE__*/_react.default.createElement("button", {
|
|
122
|
+
"data-testid": "custom_button"
|
|
123
|
+
}, customButtonText));
|
|
124
|
+
}
|
|
125
|
+
}));
|
|
126
|
+
const viewModeText = _react2.screen.getByText(VIEWER_TEXT);
|
|
127
|
+
const customEditButton = _react2.screen.getByTestId('custom_button');
|
|
128
|
+
expect(viewModeText).toBeInTheDocument();
|
|
129
|
+
expect(customEditButton).toBeInTheDocument();
|
|
130
|
+
expect(customEditButton).toHaveTextContent(customButtonText);
|
|
131
|
+
});
|
|
132
|
+
it('should switch mode to edit via props, and focus the editor', () => {
|
|
133
|
+
const _render2 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
134
|
+
mode: "view",
|
|
135
|
+
onChangeMode: jest.fn(),
|
|
136
|
+
renderViewer: renderViewer,
|
|
137
|
+
renderEditor: renderEditor,
|
|
138
|
+
renderEditButton: renderEditButton
|
|
139
|
+
})),
|
|
140
|
+
rerender = _render2.rerender,
|
|
141
|
+
container = _render2.container;
|
|
142
|
+
const viewModeText = _react2.screen.getByText(VIEWER_TEXT);
|
|
143
|
+
expect(viewModeText).toBeInTheDocument();
|
|
144
|
+
|
|
145
|
+
// Simulate mode prop change
|
|
146
|
+
rerender( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
147
|
+
mode: "edit",
|
|
148
|
+
onChangeMode: jest.fn(),
|
|
149
|
+
renderViewer: renderViewer,
|
|
150
|
+
renderEditor: renderEditor,
|
|
151
|
+
renderEditButton: renderEditButton
|
|
152
|
+
}));
|
|
153
|
+
const inputForEdit = _react2.screen.getByTestId('input-editor');
|
|
154
|
+
expect(container).not.toHaveTextContent(VIEWER_TEXT);
|
|
155
|
+
expect(inputForEdit).toBeInTheDocument();
|
|
156
|
+
expect(document.activeElement).toBe(inputForEdit);
|
|
157
|
+
});
|
|
158
|
+
it('should switch mode to view via props, and focus the edit button', () => {
|
|
159
|
+
const _render3 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
160
|
+
mode: "edit",
|
|
161
|
+
onChangeMode: jest.fn(),
|
|
162
|
+
renderViewer: renderViewer,
|
|
163
|
+
renderEditor: renderEditor,
|
|
164
|
+
renderEditButton: renderEditButton
|
|
165
|
+
})),
|
|
166
|
+
rerender = _render3.rerender,
|
|
167
|
+
container = _render3.container;
|
|
168
|
+
const inputForEdit = _react2.screen.getByTestId('input-editor');
|
|
169
|
+
expect(container).not.toHaveTextContent(VIEWER_TEXT);
|
|
170
|
+
expect(inputForEdit).toBeInTheDocument();
|
|
171
|
+
|
|
172
|
+
// Simulate mode prop change
|
|
173
|
+
rerender( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
174
|
+
mode: "view",
|
|
175
|
+
onChangeMode: jest.fn(),
|
|
176
|
+
renderViewer: renderViewer,
|
|
177
|
+
renderEditor: renderEditor,
|
|
178
|
+
renderEditButton: renderEditButton
|
|
179
|
+
}));
|
|
180
|
+
const inputForEditAfterModeChange = container.querySelector("[id='input-editor']");
|
|
181
|
+
const editButton = _react2.screen.getByRole('button');
|
|
182
|
+
expect(container).toHaveTextContent(VIEWER_TEXT);
|
|
183
|
+
expect(inputForEditAfterModeChange).not.toBeInTheDocument();
|
|
184
|
+
expect(editButton).toBeInTheDocument();
|
|
185
|
+
expect(editButton).toHaveTextContent(EDIT_BUTTON_TEXT);
|
|
186
|
+
expect(document.activeElement).toBe(editButton);
|
|
187
|
+
});
|
|
188
|
+
it('should meet a11y standards in view mode', async () => {
|
|
189
|
+
const _render4 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
190
|
+
mode: "view",
|
|
191
|
+
onChangeMode: jest.fn(),
|
|
192
|
+
renderViewer: renderViewer,
|
|
193
|
+
renderEditor: renderEditor,
|
|
194
|
+
renderEditButton: renderEditButton
|
|
195
|
+
})),
|
|
196
|
+
container = _render4.container;
|
|
197
|
+
const axeCheck = await (0, _runAxeCheck.runAxeCheck)(container);
|
|
198
|
+
expect(axeCheck).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
it('should meet a11y standards in edit mode', async () => {
|
|
201
|
+
const _render5 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.InPlaceEdit, {
|
|
202
|
+
mode: "edit",
|
|
203
|
+
onChangeMode: jest.fn(),
|
|
204
|
+
renderViewer: renderViewer,
|
|
205
|
+
renderEditor: renderEditor,
|
|
206
|
+
renderEditButton: renderEditButton
|
|
207
|
+
})),
|
|
208
|
+
container = _render5.container;
|
|
209
|
+
const axeCheck = await (0, _runAxeCheck.runAxeCheck)(container);
|
|
210
|
+
expect(axeCheck).toBe(true);
|
|
211
|
+
});
|
|
212
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instructure/ui-editable",
|
|
3
|
-
"version": "8.50.1-snapshot-
|
|
3
|
+
"version": "8.50.1-snapshot-13",
|
|
4
4
|
"description": "A UI component library made by Instructure Inc.",
|
|
5
5
|
"author": "Instructure, Inc. Engineering and Product Design",
|
|
6
6
|
"module": "./es/index.js",
|
|
@@ -23,21 +23,24 @@
|
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@instructure/ui-
|
|
27
|
-
"@instructure/ui-
|
|
26
|
+
"@instructure/ui-axe-check": "8.50.1-snapshot-13",
|
|
27
|
+
"@instructure/ui-babel-preset": "8.50.1-snapshot-13",
|
|
28
|
+
"@testing-library/jest-dom": "^6.1.4",
|
|
29
|
+
"@testing-library/react": "^14.0.0",
|
|
30
|
+
"@testing-library/user-event": "^14.5.1"
|
|
28
31
|
},
|
|
29
32
|
"dependencies": {
|
|
30
33
|
"@babel/runtime": "^7.23.2",
|
|
31
|
-
"@instructure/console": "8.50.1-snapshot-
|
|
32
|
-
"@instructure/emotion": "8.50.1-snapshot-
|
|
33
|
-
"@instructure/ui-a11y-content": "8.50.1-snapshot-
|
|
34
|
-
"@instructure/ui-buttons": "8.50.1-snapshot-
|
|
35
|
-
"@instructure/ui-dom-utils": "8.50.1-snapshot-
|
|
36
|
-
"@instructure/ui-flex": "8.50.1-snapshot-
|
|
37
|
-
"@instructure/ui-focusable": "8.50.1-snapshot-
|
|
38
|
-
"@instructure/ui-icons": "8.50.1-snapshot-
|
|
39
|
-
"@instructure/ui-utils": "8.50.1-snapshot-
|
|
40
|
-
"@instructure/ui-view": "8.50.1-snapshot-
|
|
34
|
+
"@instructure/console": "8.50.1-snapshot-13",
|
|
35
|
+
"@instructure/emotion": "8.50.1-snapshot-13",
|
|
36
|
+
"@instructure/ui-a11y-content": "8.50.1-snapshot-13",
|
|
37
|
+
"@instructure/ui-buttons": "8.50.1-snapshot-13",
|
|
38
|
+
"@instructure/ui-dom-utils": "8.50.1-snapshot-13",
|
|
39
|
+
"@instructure/ui-flex": "8.50.1-snapshot-13",
|
|
40
|
+
"@instructure/ui-focusable": "8.50.1-snapshot-13",
|
|
41
|
+
"@instructure/ui-icons": "8.50.1-snapshot-13",
|
|
42
|
+
"@instructure/ui-utils": "8.50.1-snapshot-13",
|
|
43
|
+
"@instructure/ui-view": "8.50.1-snapshot-13",
|
|
41
44
|
"prop-types": "^15.8.1"
|
|
42
45
|
},
|
|
43
46
|
"peerDependencies": {
|
package/src/Editable/README.md
CHANGED
|
@@ -10,7 +10,7 @@ to the consumer of `Editable` to provide _render properties_ which return the vi
|
|
|
10
10
|
and the edit component.
|
|
11
11
|
|
|
12
12
|
Where `Editable` provides the state management and user interactions, it is not its
|
|
13
|
-
responsibility to provide the DOM structure to layout the component on the page.
|
|
13
|
+
responsibility to provide the DOM structure to layout the component on the page. We provide
|
|
14
14
|
a standard implementation of that in [`InPlaceEdit`](/#InPlaceEdit), which is where you
|
|
15
15
|
will find examples.
|
|
16
16
|
|
|
@@ -19,8 +19,7 @@ should look.
|
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
21
|
---
|
|
22
|
-
|
|
23
|
-
render: false
|
|
22
|
+
type: example
|
|
24
23
|
---
|
|
25
24
|
class Example extends React.Component {
|
|
26
25
|
|
|
@@ -0,0 +1,259 @@
|
|
|
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 { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
27
|
+
import userEvent from '@testing-library/user-event'
|
|
28
|
+
import '@testing-library/jest-dom'
|
|
29
|
+
|
|
30
|
+
import { Editable } from '../index'
|
|
31
|
+
import type { EditableRenderProps } from '../props'
|
|
32
|
+
|
|
33
|
+
const TEXT_VIEW = 'text-view'
|
|
34
|
+
const TEXT_EDIT = 'text-edit'
|
|
35
|
+
|
|
36
|
+
const childRender = jest.fn(
|
|
37
|
+
({
|
|
38
|
+
mode,
|
|
39
|
+
getContainerProps,
|
|
40
|
+
getViewerProps,
|
|
41
|
+
getEditorProps,
|
|
42
|
+
getEditButtonProps
|
|
43
|
+
}: EditableRenderProps) => {
|
|
44
|
+
const { isVisible, buttonRef, ...buttonProps } = getEditButtonProps()
|
|
45
|
+
const { onBlur, editorRef } = getEditorProps()
|
|
46
|
+
return (
|
|
47
|
+
<div data-testid="child-container" {...getContainerProps()}>
|
|
48
|
+
{mode === 'view' && <div {...getViewerProps()}>{TEXT_VIEW}</div>}
|
|
49
|
+
{mode === 'edit' && (
|
|
50
|
+
<input
|
|
51
|
+
ref={editorRef}
|
|
52
|
+
onBlur={onBlur}
|
|
53
|
+
defaultValue="textvalue"
|
|
54
|
+
data-testid="edit-mode-input"
|
|
55
|
+
/>
|
|
56
|
+
)}
|
|
57
|
+
{
|
|
58
|
+
<button
|
|
59
|
+
{...buttonProps}
|
|
60
|
+
className={isVisible ? 'test-visible' : 'test-hidden'}
|
|
61
|
+
>
|
|
62
|
+
{TEXT_EDIT}
|
|
63
|
+
</button>
|
|
64
|
+
}
|
|
65
|
+
</div>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
describe('<Editable />', () => {
|
|
71
|
+
afterEach(() => {
|
|
72
|
+
childRender.mockClear()
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('should render view mode', async () => {
|
|
76
|
+
render(
|
|
77
|
+
<Editable mode="view" onChangeMode={jest.fn()} render={childRender} />
|
|
78
|
+
)
|
|
79
|
+
const currentMode = childRender.mock.calls[0][0].mode
|
|
80
|
+
const viewModeText = screen.getByText(TEXT_VIEW)
|
|
81
|
+
const editButton = screen.getByRole('button', { name: TEXT_EDIT })
|
|
82
|
+
|
|
83
|
+
expect(childRender).toHaveBeenCalled()
|
|
84
|
+
expect(currentMode).toBe('view')
|
|
85
|
+
|
|
86
|
+
expect(viewModeText).toBeInTheDocument()
|
|
87
|
+
expect(editButton).toBeInTheDocument()
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('should render edit mode', async () => {
|
|
91
|
+
render(
|
|
92
|
+
<Editable mode="edit" onChangeMode={jest.fn()} render={childRender} />
|
|
93
|
+
)
|
|
94
|
+
const currentMode = childRender.mock.calls[0][0].mode
|
|
95
|
+
const inputForEdit = screen.getByTestId('edit-mode-input')
|
|
96
|
+
const editButton = screen.getByRole('button', { name: TEXT_EDIT })
|
|
97
|
+
|
|
98
|
+
expect(childRender).toHaveBeenCalled()
|
|
99
|
+
expect(currentMode).toBe('edit')
|
|
100
|
+
|
|
101
|
+
expect(inputForEdit).toBeInTheDocument()
|
|
102
|
+
expect(editButton).toBeInTheDocument()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('should change to edit mode on button click', () => {
|
|
106
|
+
const onChangeModeSpy = jest.fn()
|
|
107
|
+
|
|
108
|
+
render(
|
|
109
|
+
<Editable
|
|
110
|
+
mode="view"
|
|
111
|
+
onChangeMode={onChangeModeSpy}
|
|
112
|
+
render={childRender}
|
|
113
|
+
/>
|
|
114
|
+
)
|
|
115
|
+
const viewModeText = screen.getByText(TEXT_VIEW)
|
|
116
|
+
const editButton = screen.getByRole('button')
|
|
117
|
+
|
|
118
|
+
expect(viewModeText).toBeInTheDocument()
|
|
119
|
+
|
|
120
|
+
fireEvent.click(editButton)
|
|
121
|
+
|
|
122
|
+
expect(onChangeModeSpy).toHaveBeenCalledWith('edit')
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('should change to edit mode on component click', async () => {
|
|
126
|
+
const onChangeModeSpy = jest.fn()
|
|
127
|
+
|
|
128
|
+
render(
|
|
129
|
+
<Editable
|
|
130
|
+
mode="view"
|
|
131
|
+
onChangeMode={onChangeModeSpy}
|
|
132
|
+
render={childRender}
|
|
133
|
+
/>
|
|
134
|
+
)
|
|
135
|
+
const childContainer = screen.getByTestId('child-container')
|
|
136
|
+
|
|
137
|
+
userEvent.click(childContainer)
|
|
138
|
+
|
|
139
|
+
await waitFor(() => expect(onChangeModeSpy).toHaveBeenCalledWith('edit'))
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('should set the button to visible on mouse over', async () => {
|
|
143
|
+
const onChangeModeSpy = jest.fn()
|
|
144
|
+
|
|
145
|
+
render(
|
|
146
|
+
<Editable
|
|
147
|
+
mode="view"
|
|
148
|
+
onChangeMode={onChangeModeSpy}
|
|
149
|
+
render={childRender}
|
|
150
|
+
/>
|
|
151
|
+
)
|
|
152
|
+
const editButton = screen.getByRole('button')
|
|
153
|
+
|
|
154
|
+
expect(editButton).toHaveClass('test-hidden')
|
|
155
|
+
|
|
156
|
+
fireEvent.mouseOver(editButton)
|
|
157
|
+
|
|
158
|
+
await waitFor(() => {
|
|
159
|
+
expect(editButton).toHaveClass('test-visible')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
fireEvent.mouseOut(editButton)
|
|
163
|
+
|
|
164
|
+
await waitFor(() => {
|
|
165
|
+
expect(editButton).toHaveClass('test-hidden')
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('should change to view mode on editor blur', () => {
|
|
170
|
+
const onChangeModeSpy = jest.fn()
|
|
171
|
+
|
|
172
|
+
render(
|
|
173
|
+
<Editable
|
|
174
|
+
mode="edit"
|
|
175
|
+
onChangeMode={onChangeModeSpy}
|
|
176
|
+
render={childRender}
|
|
177
|
+
/>
|
|
178
|
+
)
|
|
179
|
+
const inputForEdit = screen.getByTestId('edit-mode-input')
|
|
180
|
+
|
|
181
|
+
fireEvent.focus(inputForEdit)
|
|
182
|
+
fireEvent.blur(inputForEdit)
|
|
183
|
+
|
|
184
|
+
expect(onChangeModeSpy).toHaveBeenCalledWith('view')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('should change to view mode on escape', () => {
|
|
188
|
+
const onChangeModeSpy = jest.fn()
|
|
189
|
+
|
|
190
|
+
render(
|
|
191
|
+
<Editable
|
|
192
|
+
mode="edit"
|
|
193
|
+
onChangeMode={onChangeModeSpy}
|
|
194
|
+
render={childRender}
|
|
195
|
+
/>
|
|
196
|
+
)
|
|
197
|
+
const inputForEdit = screen.getByTestId('edit-mode-input')
|
|
198
|
+
|
|
199
|
+
fireEvent.keyUp(inputForEdit, { key: 'Escape', code: 'Escape' })
|
|
200
|
+
|
|
201
|
+
expect(onChangeModeSpy).toHaveBeenCalledWith('view')
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('should call onChange when the user is finished editing', () => {
|
|
205
|
+
const onChangeSpy = jest.fn()
|
|
206
|
+
|
|
207
|
+
const { rerender } = render(
|
|
208
|
+
<Editable
|
|
209
|
+
mode="edit"
|
|
210
|
+
onChangeMode={jest.fn()}
|
|
211
|
+
render={childRender}
|
|
212
|
+
value="initial_value"
|
|
213
|
+
onChange={onChangeSpy}
|
|
214
|
+
/>
|
|
215
|
+
)
|
|
216
|
+
const inputForEdit = screen.getByTestId('edit-mode-input')
|
|
217
|
+
|
|
218
|
+
fireEvent.change(inputForEdit, { target: { value: 'updated_value' } })
|
|
219
|
+
|
|
220
|
+
expect(onChangeSpy).not.toHaveBeenCalled()
|
|
221
|
+
|
|
222
|
+
// Simulate mode prop change
|
|
223
|
+
rerender(
|
|
224
|
+
<Editable
|
|
225
|
+
mode="view"
|
|
226
|
+
onChangeMode={jest.fn()}
|
|
227
|
+
render={childRender}
|
|
228
|
+
value="updated_value"
|
|
229
|
+
onChange={onChangeSpy}
|
|
230
|
+
/>
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
expect(onChangeSpy).toHaveBeenCalledWith('updated_value')
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('should warn if readOnly + mode="edit"', () => {
|
|
237
|
+
const consoleWarningSpy = jest
|
|
238
|
+
.spyOn(console, 'warn')
|
|
239
|
+
.mockImplementation(() => {})
|
|
240
|
+
|
|
241
|
+
render(
|
|
242
|
+
<Editable
|
|
243
|
+
mode="edit"
|
|
244
|
+
onChangeMode={jest.fn()}
|
|
245
|
+
render={childRender}
|
|
246
|
+
value="foo"
|
|
247
|
+
readOnly
|
|
248
|
+
/>
|
|
249
|
+
)
|
|
250
|
+
const expectedErrorMessage = 'When readOnly is true, mode must be "view"'
|
|
251
|
+
|
|
252
|
+
expect(consoleWarningSpy).toHaveBeenCalledWith(
|
|
253
|
+
expect.stringContaining(expectedErrorMessage),
|
|
254
|
+
expect.any(String)
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
consoleWarningSpy.mockRestore()
|
|
258
|
+
})
|
|
259
|
+
})
|
|
@@ -33,8 +33,7 @@ the case when the text is empty. Use the checkbox to switch between inline and b
|
|
|
33
33
|
|
|
34
34
|
```js
|
|
35
35
|
---
|
|
36
|
-
|
|
37
|
-
render: false
|
|
36
|
+
type: example
|
|
38
37
|
---
|
|
39
38
|
class Example extends React.Component {
|
|
40
39
|
|
|
@@ -127,8 +126,7 @@ A readOnly `InPlaceEdit`
|
|
|
127
126
|
|
|
128
127
|
```js
|
|
129
128
|
---
|
|
130
|
-
|
|
131
|
-
render: false
|
|
129
|
+
type: example
|
|
132
130
|
---
|
|
133
131
|
class Example extends React.Component {
|
|
134
132
|
|
|
@@ -209,8 +207,7 @@ To edit end-justified text, wrap `<InPlaceEdit />` in a
|
|
|
209
207
|
|
|
210
208
|
```js
|
|
211
209
|
---
|
|
212
|
-
|
|
213
|
-
render: false
|
|
210
|
+
type: example
|
|
214
211
|
---
|
|
215
212
|
class Example extends React.Component {
|
|
216
213
|
|
|
@@ -291,8 +288,7 @@ when the user has finished editing and the value has changed.
|
|
|
291
288
|
|
|
292
289
|
```js
|
|
293
290
|
---
|
|
294
|
-
|
|
295
|
-
render: false
|
|
291
|
+
type: example
|
|
296
292
|
---
|
|
297
293
|
class Example extends React.Component {
|
|
298
294
|
|