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() { const scroll = this.initialize(Scroll, ''); expect(scroll.domNode).toEqualHTML('


'); }); it('api change', function() { const 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) { const 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('prevent dragstart', function() { const scroll = this.initialize(Scroll, '

Hello World!

'); const dragstart = new Event('dragstart'); spyOn(dragstart, 'preventDefault'); scroll.domNode.dispatchEvent(dragstart); expect(dragstart.preventDefault).toHaveBeenCalled(); }); describe('leaf()', function() { it('text', function() { const scroll = this.initialize(Scroll, '

Tests

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

01234

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

0123

5678

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

012

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

Test

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