Skip to content

Commit 08ff1d9

Browse files
committed
test(editor): add test case.
1 parent bd1eae1 commit 08ff1d9

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

core/src/editor/value.test.tsx

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import renderer from 'react-test-renderer';
2+
import { cleanup, screen, fireEvent, render } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
24
import { ReValue } from './value';
35

46
it('renders <ReValue /> type="string" test case', () => {
@@ -144,4 +146,88 @@ it('renders <ReValue /> onDelete test case', () => {
144146
expect(child1).toHaveProperty('props.fill', 'var(--w-rjv-delete-color, #dc3545)');
145147
expect(child1).toHaveProperty('props.viewBox', '0 0 40 40');
146148
}
147-
});
149+
});
150+
151+
it('renders <ReValue /> `onEdit` test case', async () => {
152+
const user = userEvent.setup();
153+
const { container } = render(
154+
<ReValue
155+
type="string"
156+
displayDataTypes
157+
quotes="'"
158+
value="test"
159+
visible={true}
160+
editableValue={true}
161+
onEdit={() => true}
162+
/>
163+
);
164+
expect(container.firstElementChild).toBeInstanceOf(Element);
165+
expect(container.firstElementChild).toHaveProperty('style');
166+
expect(container.firstElementChild).toHaveProperty('style.opacity', '0.8');
167+
expect(container.firstElementChild).toHaveProperty('style.padding-right', '4px');
168+
expect(container.firstElementChild).toHaveProperty('style.font-size', '11px');
169+
expect(container.firstElementChild?.nextElementSibling).toHaveProperty('children');
170+
expect(screen.getAllByText("'")[0]).toBeInstanceOf(Element);
171+
const $contentEditable = container.firstElementChild?.nextElementSibling?.nextElementSibling;
172+
expect($contentEditable?.getAttribute('contentEditable')).toBeNull();
173+
await user.click(container.lastElementChild!);
174+
expect($contentEditable?.getAttribute('contentEditable')).toEqual('true');
175+
expect($contentEditable?.getAttribute('spellcheck')).toEqual('false');
176+
expect($contentEditable?.getAttribute('style')).toEqual('min-width: 34px; min-height: 18px; padding-inline: 3px; display: inline-block;');
177+
await user.keyboard('{Enter}');
178+
expect($contentEditable?.getAttribute('contentEditable')).toEqual('false');
179+
await user.click(container.lastElementChild!);
180+
expect($contentEditable?.getAttribute('contentEditable')).toEqual('true');
181+
await user.tab() // blur
182+
expect($contentEditable?.getAttribute('contentEditable')).toEqual('false');
183+
// screen.debug();
184+
});
185+
186+
it('renders <ReValue /> `type=boolean` test case', async () => {
187+
const user = userEvent.setup();
188+
const { container } = render(
189+
<ReValue
190+
type="boolean"
191+
displayDataTypes
192+
quotes="'"
193+
value="false"
194+
visible={true}
195+
editableValue={true}
196+
onEdit={() => true}
197+
/>
198+
);
199+
expect(screen.getAllByText("'")[0]).toBeInstanceOf(Element);
200+
const $contentEditable = container.firstElementChild?.nextElementSibling?.nextElementSibling;
201+
expect($contentEditable?.getAttribute('contentEditable')).toBeNull();
202+
await user.click(container.lastElementChild!);
203+
await user.type($contentEditable!, '123{Enter}');
204+
expect(screen.getByText('false123')).toBeInstanceOf(Element);
205+
expect($contentEditable?.getAttribute('contentEditable')).toEqual('false');
206+
await user.tab() // blur
207+
expect(screen.getByText('false123')).toBeInstanceOf(Element);
208+
});
209+
210+
it('renders <ReValue /> `onEdit={() => false}` test case', async () => {
211+
const user = userEvent.setup();
212+
const { container } = render(
213+
<ReValue
214+
type="boolean"
215+
displayDataTypes
216+
quotes="'"
217+
value="false"
218+
visible={true}
219+
editableValue={true}
220+
onEdit={() => false}
221+
/>
222+
);
223+
expect(screen.getAllByText("'")[0]).toBeInstanceOf(Element);
224+
const $contentEditable = container.firstElementChild?.nextElementSibling?.nextElementSibling;
225+
expect($contentEditable?.getAttribute('contentEditable')).toBeNull();
226+
await user.click(container.lastElementChild!);
227+
await user.type($contentEditable!, '123{Enter}');
228+
expect(screen.getByText('false123')).toBeInstanceOf(Element);
229+
expect($contentEditable?.getAttribute('contentEditable')).toEqual('false');
230+
await user.tab() // blur
231+
expect(screen.getByText('"false"')).toBeInstanceOf(Element);
232+
// screen.debug();
233+
});

core/src/editor/value.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
3737
if (!editableValue) return;
3838
if ($edit.current) {
3939
setEditable(true);
40-
$edit.current!.contentEditable = 'true';
40+
$edit.current.setAttribute('contentEditable', 'true');
4141
$edit.current?.focus();
4242
}
4343
}
@@ -47,15 +47,17 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
4747
evn.stopPropagation();
4848
evn.preventDefault();
4949
setEditable(false);
50-
$edit.current!.contentEditable = 'false';
50+
if ($edit.current) {
51+
$edit.current.setAttribute('contentEditable', 'false');
52+
}
5153
}
5254
}
5355
const blur = async () => {
5456
if (!editableValue) return;
5557
setEditable(false);
5658
if ($edit.current) {
57-
$edit.current.contentEditable = 'false';
58-
let text: unknown = $edit.current.innerText as string;
59+
$edit.current.setAttribute('contentEditable', 'false');
60+
let text: unknown = $edit.current.innerHTML as string;
5961
let typeStr = curentType;
6062
if (curentType === 'number' || curentType === 'float') {
6163
text = Number(text);

0 commit comments

Comments
 (0)