Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 99c3ff6

Browse files
committed
tests
1 parent 1763ffd commit 99c3ff6

File tree

11 files changed

+891
-717
lines changed

11 files changed

+891
-717
lines changed

packages/core/test/ContextManager.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,18 @@ describe('core', () => {
288288
contentBefore:
289289
'<div><div><p>cd</p></div></div>' +
290290
'<ul><li></li><li style="list-style: none;"><ul><li><p>c</p></li></ul></li><li><div>[]</div></li></ul>',
291-
stepFunction: (editor: BasicEditor) => {
291+
stepFunction: async (editor: BasicEditor) => {
292292
const callback = (): void => {};
293293
const check = (context: CheckingContext): boolean => !!context;
294294
const checkSpy1 = sinon.spy(check);
295295
const checkSpy2 = sinon.spy(check);
296296
const newSelection = new VSelection(editor);
297297

298-
const domEngine = editor.plugins.get(Layout).engines.dom;
299-
const editable = domEngine.components.get('editable')[0];
300-
newSelection.setAt(editable);
298+
await editor.execBatch(async () => {
299+
const domEngine = editor.plugins.get(Layout).engines.dom;
300+
const editable = domEngine.components.get('editable')[0];
301+
newSelection.setAt(editable);
302+
});
301303
const commands: CommandImplementation[] = [
302304
{
303305
title: 'paragraph',

packages/core/test/JWeditor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ describe('core', () => {
234234
await testEditor(BasicEditor, {
235235
contentBefore: '<div>ab[]</div>',
236236
stepFunction: async editor => {
237-
await editor.execCustomCommand(async () => {
237+
await editor.execBatch(async () => {
238238
const layout = editor.plugins.get(Layout);
239239
const domEngine = layout.engines.dom;
240240
domEngine.components
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* eslint-disable max-nested-callbacks */
2+
import { expect } from 'chai';
3+
import { BasicEditor } from '../../bundle-basic-editor/BasicEditor';
4+
import { DomEditable } from '../../plugin-dom-editable/src/DomEditable';
5+
import { DomLayout } from '../../plugin-dom-layout/src/DomLayout';
6+
import { Layout } from '../../plugin-layout/src/Layout';
7+
import { DomLayoutEngine } from '../../plugin-dom-layout/src/DomLayoutEngine';
8+
9+
describe('test performances', () => {
10+
describe('stores', () => {
11+
describe('Memory / VDocument', () => {
12+
let wrapper: HTMLElement;
13+
let editor: BasicEditor;
14+
15+
beforeEach(async () => {
16+
wrapper = document.createElement('test-wrapper');
17+
wrapper.style.display = 'block';
18+
document.body.appendChild(wrapper);
19+
const root = document.createElement('div');
20+
root.innerHTML = `<h1>Jabberwocky</h1>
21+
<h3>by Lewis Carroll</h3>
22+
<p><i>’Twas brillig, and the slithy toves<br/>
23+
Did gyre and gimble in the wabe:<br/>
24+
All mimsy were the borogoves,<br/>
25+
And the mome raths outgrabe.<br/>
26+
<br/>
27+
“Beware the Jabberwock, my son!<br/>
28+
The jaws that bite, the claws that catch!<br/>
29+
Beware the Jubjub bird, and shun<br/>
30+
The frumious Bandersnatch!”<br/>
31+
<br/>
32+
He took his vorpal sword in hand;<br/>
33+
Long time the manxome foe he sought—<br/>
34+
So rested he by the Tumtum tree<br/>
35+
And stood awhile in thought.<br/>
36+
<br/>
37+
And, as in uffish thought he stood,<br/>
38+
The Jabberwock, with eyes of flame,<br/>
39+
Came whiffling through the tulgey wood,<br/>
40+
And burbled as it came!<br/>
41+
<br/>
42+
One, two! One, two! And through and through<br/>
43+
The vorpal blade went snicker-snack!<br/>
44+
He left it dead, and with its head<br/>
45+
He went galumphing back.<br/>
46+
<br/>
47+
“And hast thou slain the Jabberwock?<br/>
48+
Come to my arms, my beamish boy!<br/>
49+
O frabjous day! Callooh! Callay!”<br/>
50+
He chortled in his joy.<br/>
51+
<br/>
52+
’Twas brillig, and the slithy toves<br/>
53+
Did gyre and gimble in the wabe:<br/>
54+
All mimsy were the borogoves,<br/>
55+
And the mome raths outgrabe.<br/></i></p>`;
56+
wrapper.appendChild(root);
57+
58+
editor = new BasicEditor();
59+
editor.configure(DomLayout, { location: [root, 'replace'] });
60+
editor.configure(DomEditable, { source: root });
61+
await editor.start();
62+
});
63+
afterEach(async () => {
64+
editor.stop();
65+
document.body.removeChild(wrapper);
66+
});
67+
68+
it('should split a paragraph in two', async () => {
69+
// Parse the editable in the internal format of the editor.
70+
const memory = editor.memory;
71+
const domEngine = editor.plugins.get(Layout).engines.dom as DomLayoutEngine;
72+
const editable = domEngine.components.get('editable')[0];
73+
editor.selection.setAt(editable.children()[2].children()[500]);
74+
75+
memory.linkToMemory(editable);
76+
memory.create('root').switchTo('root');
77+
78+
expect(editable.children().length).to.equal(3);
79+
memory.create('test').switchTo('test');
80+
await editor.execCommand('insertParagraphBreak');
81+
expect(editable.children().length).to.equal(4);
82+
83+
const t1 = [];
84+
const t2 = [];
85+
for (let k = 2; k < 26; k++) {
86+
let d = Date.now();
87+
memory
88+
.switchTo('root')
89+
.create(k.toString())
90+
.switchTo(k.toString());
91+
t1.push(Date.now() - d);
92+
93+
d = Date.now();
94+
await editor.execCommand('insertParagraphBreak');
95+
t2.push(Date.now() - d);
96+
}
97+
98+
// We remove the first load because it does not represent time in
99+
// use. In fact, time is much longer because the functions and
100+
// object are not yet loaded. The loading test is done separately.
101+
t1.shift();
102+
t2.shift();
103+
104+
const averageInsert = Math.round(t2.reduce((a, b) => a + b) / t2.length);
105+
expect(averageInsert).to.lessThan(30, 'Time to compute the insert paragraph');
106+
107+
const averageSwitch = Math.round(t1.reduce((a, b) => a + b) / t1.length);
108+
expect(averageSwitch).to.lessThan(1, 'Time to switch the memory');
109+
});
110+
});
111+
});
112+
});

packages/core/test/VDocument.test.ts

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { BasicEditor } from '../../bundle-basic-editor/BasicEditor';
77
import { Core } from '../../core/src/Core';
88
import { Layout } from '../../plugin-layout/src/Layout';
99

10-
const deleteForward = async (editor: JWEditor): Promise<void> =>
10+
const deleteForward = async (editor: JWEditor): Promise<void> => {
1111
await editor.execCommand<Core>('deleteForward');
12+
};
1213
const deleteBackward = async (editor: JWEditor): Promise<void> =>
1314
await editor.execCommand<Core>('deleteBackward');
1415
const insertParagraphBreak = async (editor: JWEditor): Promise<void> =>
@@ -548,9 +549,11 @@ describe('VDocument', () => {
548549
stepFunction: (editor: JWEditor) => {
549550
const domEngine = editor.plugins.get(Layout).engines.dom;
550551
const editable = domEngine.components.get('editable')[0];
551-
editable.firstChild().breakable = false;
552-
editable.lastChild().breakable = false;
553-
return deleteForward(editor);
552+
return editor.execBatch(() => {
553+
editable.firstChild().breakable = false;
554+
editable.lastChild().breakable = false;
555+
return deleteForward(editor);
556+
});
554557
},
555558
contentAfter: '<p>a[</p><p>]f</p>',
556559
});
@@ -1506,15 +1509,17 @@ describe('VDocument', () => {
15061509
await testEditor(BasicEditor, {
15071510
contentBefore: 'ab[]',
15081511
stepFunction: async (editor: JWEditor) => {
1509-
const domEngine = editor.plugins.get(Layout).engines.dom;
1510-
const editable = domEngine.components.get('editable')[0];
1511-
const aNode = editable.next(node => node.name === 'a');
1512-
await withRange(editor, VRange.at(aNode), async range => {
1513-
await editor.execCommand<Char>('insertText', {
1514-
text: 'c',
1515-
context: {
1516-
range: range,
1517-
},
1512+
await editor.execBatch(async () => {
1513+
const domEngine = editor.plugins.get(Layout).engines.dom;
1514+
const editable = domEngine.components.get('editable')[0];
1515+
const aNode = editable.next(node => node.name === 'a');
1516+
await withRange(editor, VRange.at(aNode), async range => {
1517+
await editor.execCommand<Char>('insertText', {
1518+
text: 'c',
1519+
context: {
1520+
range: range,
1521+
},
1522+
});
15181523
});
15191524
});
15201525
},
@@ -1525,15 +1530,17 @@ describe('VDocument', () => {
15251530
await testEditor(BasicEditor, {
15261531
contentBefore: 'ab[]',
15271532
stepFunction: async (editor: JWEditor) => {
1528-
const domEngine = editor.plugins.get(Layout).engines.dom;
1529-
const editable = domEngine.components.get('editable')[0];
1530-
const aNode = editable.next(node => node.name === 'a');
1531-
await withRange(editor, VRange.selecting(aNode, aNode), async range => {
1532-
await editor.execCommand<Char>('insertText', {
1533-
text: 'c',
1534-
context: {
1535-
range: range,
1536-
},
1533+
await editor.execBatch(async () => {
1534+
const domEngine = editor.plugins.get(Layout).engines.dom;
1535+
const editable = domEngine.components.get('editable')[0];
1536+
const aNode = editable.next(node => node.name === 'a');
1537+
await withRange(editor, VRange.selecting(aNode, aNode), async range => {
1538+
await editor.execCommand<Char>('insertText', {
1539+
text: 'c',
1540+
context: {
1541+
range: range,
1542+
},
1543+
});
15371544
});
15381545
});
15391546
},
@@ -1551,12 +1558,14 @@ describe('VDocument', () => {
15511558
[aNode, RelativePosition.BEFORE],
15521559
[aNode, RelativePosition.BEFORE],
15531560
];
1554-
await withRange(editor, rangeParams, async range => {
1555-
await editor.execCommand<Char>('insertText', {
1556-
text: 'c',
1557-
context: {
1558-
range: range,
1559-
},
1561+
await editor.execBatch(async () => {
1562+
await withRange(editor, rangeParams, async range => {
1563+
await editor.execCommand<Char>('insertText', {
1564+
text: 'c',
1565+
context: {
1566+
range: range,
1567+
},
1568+
});
15601569
});
15611570
});
15621571
},
@@ -1574,12 +1583,14 @@ describe('VDocument', () => {
15741583
[aNode, RelativePosition.BEFORE],
15751584
[aNode, RelativePosition.AFTER],
15761585
];
1577-
await withRange(editor, rangeParams, async range => {
1578-
await editor.execCommand<Char>('insertText', {
1579-
text: 'c',
1580-
context: {
1581-
range: range,
1582-
},
1586+
await editor.execBatch(async () => {
1587+
await withRange(editor, rangeParams, async range => {
1588+
await editor.execCommand<Char>('insertText', {
1589+
text: 'c',
1590+
context: {
1591+
range: range,
1592+
},
1593+
});
15831594
});
15841595
});
15851596
},
@@ -1597,12 +1608,14 @@ describe('VDocument', () => {
15971608
[aNode, RelativePosition.AFTER],
15981609
[aNode, RelativePosition.AFTER],
15991610
];
1600-
await withRange(editor, rangeParams, async range => {
1601-
await editor.execCommand<Char>('insertText', {
1602-
text: 'c',
1603-
context: {
1604-
range: range,
1605-
},
1611+
await editor.execBatch(async () => {
1612+
await withRange(editor, rangeParams, async range => {
1613+
await editor.execCommand<Char>('insertText', {
1614+
text: 'c',
1615+
context: {
1616+
range: range,
1617+
},
1618+
});
16061619
});
16071620
});
16081621
},
@@ -1621,12 +1634,14 @@ describe('VDocument', () => {
16211634
[aNode, RelativePosition.AFTER],
16221635
[bNode, RelativePosition.BEFORE],
16231636
];
1624-
await withRange(editor, rangeParams, async range => {
1625-
await editor.execCommand<Char>('insertText', {
1626-
text: 'c',
1627-
context: {
1628-
range: range,
1629-
},
1637+
await editor.execBatch(async () => {
1638+
await withRange(editor, rangeParams, async range => {
1639+
await editor.execCommand<Char>('insertText', {
1640+
text: 'c',
1641+
context: {
1642+
range: range,
1643+
},
1644+
});
16301645
});
16311646
});
16321647
},
@@ -1645,12 +1660,14 @@ describe('VDocument', () => {
16451660
[aNode, RelativePosition.AFTER],
16461661
[bNode, RelativePosition.AFTER],
16471662
];
1648-
await withRange(editor, rangeParams, async range => {
1649-
await editor.execCommand<Char>('insertText', {
1650-
text: 'c',
1651-
context: {
1652-
range: range,
1653-
},
1663+
await editor.execBatch(async () => {
1664+
await withRange(editor, rangeParams, async range => {
1665+
await editor.execCommand<Char>('insertText', {
1666+
text: 'c',
1667+
context: {
1668+
range: range,
1669+
},
1670+
});
16541671
});
16551672
});
16561673
},

packages/core/test/VRange.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ describe('VRange', () => {
99
contentBefore: '<v>abc<w>de[f<x>ghi<y>jkl<z>mno</z>pqr</y>stu</x>vw</w>xy]z</v>',
1010
stepFunction: async (editor: BasicEditor) => {
1111
editor.dispatcher.registerCommand('refresh', { handler: () => {} });
12-
const nodes = editor.selection.range.split();
13-
const domEngine = editor.plugins.get(Layout).engines.dom;
14-
const editable = domEngine.components.get('editable')[0];
15-
editable.lastChild().after(nodes[0]);
12+
await editor.execBatch(async () => {
13+
const nodes = editor.selection.range.split();
14+
const domEngine = editor.plugins.get(Layout).engines.dom;
15+
const editable = domEngine.components.get('editable')[0];
16+
editable.lastChild().after(nodes[0]);
17+
});
1618
await editor.execCommand('refresh');
1719
},
1820
contentAfter:

packages/plugin-align/test/Align.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ describePlugin(Align, testEditor => {
4040
const domEngine = editor.plugins.get(Layout).engines.dom;
4141
const editable = domEngine.components.get('editable')[0];
4242
const root = editable;
43-
root.lastChild().editable = false;
44-
return align(AlignType.LEFT)(editor);
43+
return editor.execBatch(() => {
44+
root.lastChild().editable = false;
45+
return align(AlignType.LEFT)(editor);
46+
});
4547
},
4648
contentAfter: '<p>ab</p><p style="text-align: right;">c[]d</p>',
4749
});

0 commit comments

Comments
 (0)