Skip to content

Commit f5048c1

Browse files
committed
fine tune tests
1 parent a2203dd commit f5048c1

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

web_src/js/features/comp/EditorMarkdown.test.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,36 @@ test('EditorMarkdown', () => {
44
const textarea = document.createElement('textarea');
55
initTextareaMarkdown(textarea);
66

7-
const testInput = (value, expected) => {
8-
textarea.value = value;
9-
textarea.setSelectionRange(value.length, value.length);
7+
type ValueWithCursor = string | {
8+
value: string;
9+
pos: number;
10+
}
11+
const testInput = (input: ValueWithCursor, result: ValueWithCursor) => {
12+
const intputValue = typeof input === 'string' ? input : input.value;
13+
const inputPos = typeof input === 'string' ? intputValue.length : input.pos;
14+
textarea.value = intputValue;
15+
textarea.setSelectionRange(inputPos, inputPos);
16+
1017
const e = new KeyboardEvent('keydown', {key: 'Enter', cancelable: true});
1118
textarea.dispatchEvent(e);
12-
if (!e.defaultPrevented) textarea.value += '\n';
13-
expect(textarea.value).toEqual(expected);
19+
if (!e.defaultPrevented) textarea.value += '\n'; // simulate default behavior
20+
21+
const expectedValue = typeof result === 'string' ? result : result.value;
22+
const expectedPos = typeof result === 'string' ? expectedValue.length : result.pos;
23+
expect(textarea.value).toEqual(expectedValue);
24+
expect(textarea.selectionStart).toEqual(expectedPos);
1425
};
1526

1627
testInput('-', '-\n');
1728
testInput('1.', '1.\n');
1829

1930
testInput('- ', '');
2031
testInput('1. ', '');
32+
testInput({value: '1. \n2. ', pos: 3}, {value: '\n2. ', pos: 0});
2133

2234
testInput('- x', '- x\n- ');
2335
testInput('1. foo', '1. foo\n2. ');
36+
testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n2. \n2. b\n3. c', pos: 8});
2437
testInput('- [ ]', '- [ ]\n- ');
2538
testInput('- [ ] foo', '- [ ] foo\n- [ ] ');
2639
testInput('* [x] foo', '* [x] foo\n* [ ] ');

0 commit comments

Comments
 (0)