scroll.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Emitter from '../../../core/emitter';
  2. import Selection, { Range } from '../../../core/selection';
  3. import Cursor from '../../../blots/cursor';
  4. import Scroll from '../../../blots/scroll';
  5. describe('Scroll', function() {
  6. it('initialize empty document', function() {
  7. const scroll = this.initialize(Scroll, '');
  8. expect(scroll.domNode).toEqualHTML('<p><br></p>');
  9. });
  10. it('api change', function() {
  11. const scroll = this.initialize(Scroll, '<p>Hello World!</p>');
  12. spyOn(scroll.emitter, 'emit').and.callThrough();
  13. scroll.insertAt(5, '!');
  14. expect(scroll.emitter.emit).toHaveBeenCalledWith(
  15. Emitter.events.SCROLL_OPTIMIZE,
  16. jasmine.any(Array),
  17. jasmine.any(Object),
  18. );
  19. });
  20. it('user change', function(done) {
  21. const scroll = this.initialize(Scroll, '<p>Hello World!</p>');
  22. spyOn(scroll.emitter, 'emit').and.callThrough();
  23. scroll.domNode.firstChild.appendChild(document.createTextNode('!'));
  24. setTimeout(function() {
  25. expect(scroll.emitter.emit).toHaveBeenCalledWith(
  26. Emitter.events.SCROLL_OPTIMIZE,
  27. jasmine.any(Array),
  28. jasmine.any(Object),
  29. );
  30. expect(scroll.emitter.emit).toHaveBeenCalledWith(
  31. Emitter.events.SCROLL_UPDATE,
  32. Emitter.sources.USER,
  33. jasmine.any(Array),
  34. );
  35. done();
  36. }, 1);
  37. });
  38. it('prevent dragstart', function() {
  39. const scroll = this.initialize(Scroll, '<p>Hello World!</p>');
  40. const dragstart = new Event('dragstart');
  41. spyOn(dragstart, 'preventDefault');
  42. scroll.domNode.dispatchEvent(dragstart);
  43. expect(dragstart.preventDefault).toHaveBeenCalled();
  44. });
  45. describe('leaf()', function() {
  46. it('text', function() {
  47. const scroll = this.initialize(Scroll, '<p>Tests</p>');
  48. const [leaf, offset] = scroll.leaf(2);
  49. expect(leaf.value()).toEqual('Tests');
  50. expect(offset).toEqual(2);
  51. });
  52. it('precise', function() {
  53. const scroll = this.initialize(
  54. Scroll,
  55. '<p><u>0</u><s>1</s><u>2</u><s>3</s><u>4</u></p>',
  56. );
  57. const [leaf, offset] = scroll.leaf(3);
  58. expect(leaf.value()).toEqual('2');
  59. expect(offset).toEqual(1);
  60. });
  61. it('newline', function() {
  62. const scroll = this.initialize(Scroll, '<p>0123</p><p>5678</p>');
  63. const [leaf, offset] = scroll.leaf(4);
  64. expect(leaf.value()).toEqual('0123');
  65. expect(offset).toEqual(4);
  66. });
  67. it('cursor', function() {
  68. const selection = this.initialize(Selection, '<p><u>0</u>1<u>2</u></p>');
  69. selection.setRange(new Range(2));
  70. selection.format('strike', true);
  71. const [leaf, offset] = selection.scroll.leaf(2);
  72. expect(leaf instanceof Cursor).toBe(true);
  73. expect(offset).toEqual(0);
  74. });
  75. it('beyond document', function() {
  76. const scroll = this.initialize(Scroll, '<p>Test</p>');
  77. const [leaf, offset] = scroll.leaf(10);
  78. expect(leaf).toEqual(null);
  79. expect(offset).toEqual(-1);
  80. });
  81. });
  82. });