import { oneLineTrim } from 'common-tags'; import { DOMParser } from 'prosemirror-model'; import WysiwygEditor from '@/wysiwyg/wwEditor'; import EventEmitter from '@/event/eventEmitter'; import { WwToDOMAdaptor } from '@/wysiwyg/adaptor/wwToDOMAdaptor'; import { createHTMLSchemaMap } from '@/wysiwyg/nodes/html'; import { sanitizeHTML } from '@/sanitizer/htmlSanitizer'; import { createHTMLrenderer } from '../markdown/util'; jest.useFakeTimers(); describe('WysiwygEditor', () => { let wwe: WysiwygEditor, em: EventEmitter, el: HTMLElement; function assertToContainHTML(html: string) { expect(wwe.view.dom.innerHTML).toContain(html); } function setContent(content: string) { const wrapper = document.createElement('div'); wrapper.innerHTML = content; const nodes = DOMParser.fromSchema(wwe.schema).parse(wrapper); wwe.setModel(nodes); } beforeEach(() => { const htmlRenderer = createHTMLrenderer(); const toDOMAdaptor = new WwToDOMAdaptor({}, htmlRenderer); const htmlSchemaMap = createHTMLSchemaMap(htmlRenderer, sanitizeHTML, toDOMAdaptor); em = new EventEmitter(); wwe = new WysiwygEditor(em, { toDOMAdaptor, htmlSchemaMap }); el = wwe.el; document.body.appendChild(el); }); afterEach(() => { jest.clearAllTimers(); if (Object.keys(wwe).length) { wwe.destroy(); } document.body.removeChild(el); }); describe('API', () => { it('destroy() initialize instance object', () => { wwe.destroy(); expect(wwe).toEqual({}); }); it(`focus() enable editor's dom selection state`, () => { wwe.focus(); // run setTimeout function when focusing the editor jest.runAllTimers(); expect(document.activeElement).toEqual(wwe.view.dom); }); it(`blur() disable editor's dom selection state`, () => { wwe.focus(); wwe.blur(); expect(document.activeElement).not.toEqual(wwe.view.dom); }); it('setHeight() change height of editor', () => { wwe.setHeight(50); expect(wwe.el.style.height).toBe('50px'); }); it('setMinHeight() change minimum height of editor', () => { wwe.setMinHeight(50); expect(wwe.el.style.minHeight).toBe('50px'); }); it('setPlaceholder() attach placeholder element', () => { wwe.setPlaceholder('placeholder text'); assertToContainHTML(oneLineTrim` `); }); it('scrollTo() move scroll position', () => { setContent(oneLineTrim`
foo
foo
bar
baz
`); wwe.setSelection(13, 2); expect(wwe.getSelection()).toEqual([2, 13]); }); it('replaceSelection() change text of selection range', () => { setContent(oneLineTrim`foo
bar
`); wwe.setSelection(3, 7); wwe.replaceSelection('new foo\nnew bar'); assertToContainHTML(oneLineTrim`fonew foo
new barar
`); }); it('addWidget API', () => { const ul = document.createElement('ul'); ul.innerHTML = `foo
bar
`); const spy = jest.fn(); em.listen('changeToolbarState', spy); wwe.setSelection(3, 3); expect(spy).toHaveBeenCalled(); }); it('should display html block element properly', () => { setContent( '' ); assertToContainHTML( '' ); }); it('should display html inline element properly', () => { setContent('text'); assertToContainHTML('text
'); }); it('should sanitize html element', () => { setContent(''); assertToContainHTML( '' ); }); });