|
| 1 | +/* eslint-disable max-nested-callbacks */ |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { BasicEditor } from '../../../bundles/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/ui/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 | + memory.linkToMemory(editable); |
| 74 | + editor.selection.setAt(editable.children[2].children[500]); |
| 75 | + memory.create('0').switchTo('0'); |
| 76 | + |
| 77 | + expect(editable.children.length).to.equal(3); |
| 78 | + memory.create('test').switchTo('test'); |
| 79 | + await editor.execCommand('insertParagraphBreak'); |
| 80 | + expect(editable.children.length).to.equal(4); |
| 81 | + |
| 82 | + const t1 = []; |
| 83 | + const t2 = []; |
| 84 | + for (let k = 1; k < 25; k++) { |
| 85 | + let d = Date.now(); |
| 86 | + memory |
| 87 | + .switchTo('0') |
| 88 | + .create(k.toString()) |
| 89 | + .switchTo(k.toString()); |
| 90 | + t1.push(Date.now() - d); |
| 91 | + |
| 92 | + d = Date.now(); |
| 93 | + await editor.execCommand('insertParagraphBreak'); |
| 94 | + t2.push(Date.now() - d); |
| 95 | + } |
| 96 | + |
| 97 | + // We remove the first load because it does not represent time in |
| 98 | + // use. In fact, time is much longer because the functions and |
| 99 | + // object are not yet loaded. The loading test is done separately. |
| 100 | + t1.shift(); |
| 101 | + t2.shift(); |
| 102 | + |
| 103 | + const averageInsert = Math.round(t2.reduce((a, b) => a + b) / t2.length); |
| 104 | + expect(averageInsert).to.lessThan(30, 'Time to compute the insert paragraph'); |
| 105 | + |
| 106 | + const averageSwitch = Math.round(t1.reduce((a, b) => a + b) / t1.length); |
| 107 | + expect(averageSwitch).to.lessThan(1, 'Time to switch the memory'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments