import Parchment from 'parchment'; import Emitter from '../../../core/emitter'; import Selection, { Range } from '../../../core/selection'; import Cursor from '../../../blots/cursor'; import Scroll from '../../../blots/scroll'; describe('Scroll', function() { it('initialize empty document', function() { let scroll = this.initialize(Scroll, ''); expect(scroll.domNode).toEqualHTML('


'); }); it('api change', function() { let scroll = this.initialize(Scroll, '

Hello World!

'); spyOn(scroll.emitter, 'emit').and.callThrough(); scroll.insertAt(5, '!'); expect(scroll.emitter.emit).toHaveBeenCalledWith(Emitter.events.SCROLL_OPTIMIZE, jasmine.any(Array), jasmine.any(Object)); }); it('user change', function(done) { let scroll = this.initialize(Scroll, '

Hello World!

'); spyOn(scroll.emitter, 'emit').and.callThrough(); scroll.domNode.firstChild.appendChild(document.createTextNode('!')); setTimeout(function() { expect(scroll.emitter.emit).toHaveBeenCalledWith(Emitter.events.SCROLL_OPTIMIZE, jasmine.any(Array), jasmine.any(Object)); expect(scroll.emitter.emit).toHaveBeenCalledWith(Emitter.events.SCROLL_UPDATE, Emitter.sources.USER, jasmine.any(Array)); done(); }, 1); }); it('whitelist', function() { let scroll = Parchment.create('scroll', { emitter: new Emitter(), whitelist: ['bold'] }); scroll.insertAt(0, 'Hello World!'); scroll.formatAt(0, 5, 'bold', true); scroll.formatAt(6, 5, 'italic', true); expect(scroll.domNode.firstChild).toEqualHTML('Hello World!'); }); describe('leaf()', function() { it('text', function() { let scroll = this.initialize(Scroll, '

Tests

'); let [leaf, offset] = scroll.leaf(2); expect(leaf.value()).toEqual('Tests'); expect(offset).toEqual(2); }); it('precise', function() { let scroll = this.initialize(Scroll, '

01234

'); let [leaf, offset] = scroll.leaf(3); expect(leaf.value()).toEqual('2'); expect(offset).toEqual(1); }); it('newline', function() { let scroll = this.initialize(Scroll, '

0123

5678

'); let [leaf, offset] = scroll.leaf(4); expect(leaf.value()).toEqual('0123'); expect(offset).toEqual(4); }); it('cursor', function() { let selection = this.initialize(Selection, '

012

'); selection.setRange(new Range(2)); selection.format('strike', true); let [leaf, offset] = selection.scroll.leaf(2); expect(leaf instanceof Cursor).toBe(true); expect(offset).toEqual(0); }); it('beyond document', function() { let scroll = this.initialize(Scroll, '

Test

'); let [leaf, offset] = scroll.leaf(10); expect(leaf).toEqual(null); expect(offset).toEqual(-1); }); }); });