node.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Element from '/ckeditor5/core/treeview/element.js';
  8. import RootElement from '/ckeditor5/core/treeview/rootelement.js';
  9. import Text from '/ckeditor5/core/treeview/text.js';
  10. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  11. describe( 'Node', () => {
  12. let root;
  13. let one, two, three;
  14. let charB, charA, charR, img;
  15. before( () => {
  16. charB = new Text( 'b' );
  17. charA = new Text( 'a' );
  18. img = new Element( 'img' );
  19. charR = new Text( 'r' );
  20. one = new Element( 'one' );
  21. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  22. three = new Element( 'three' );
  23. root = new Element( null, null, [ one, two, three ] );
  24. } );
  25. describe( 'getNextSibling/getPreviousSibling', () => {
  26. it( 'should return next sibling', () => {
  27. expect( root.getNextSibling() ).to.be.null;
  28. expect( one.getNextSibling() ).to.equal( two );
  29. expect( two.getNextSibling() ).to.equal( three );
  30. expect( three.getNextSibling() ).to.be.null;
  31. expect( charB.getNextSibling() ).to.equal( charA );
  32. expect( charA.getNextSibling() ).to.equal( img );
  33. expect( img.getNextSibling() ).to.equal( charR );
  34. expect( charR.getNextSibling() ).to.be.null;
  35. } );
  36. it( 'should return previous sibling', () => {
  37. expect( root.getPreviousSibling() ).to.be.null;
  38. expect( one.getPreviousSibling() ).to.be.null;
  39. expect( two.getPreviousSibling() ).to.equal( one );
  40. expect( three.getPreviousSibling() ).to.equal( two );
  41. expect( charB.getPreviousSibling() ).to.be.null;
  42. expect( charA.getPreviousSibling() ).to.equal( charB );
  43. expect( img.getPreviousSibling() ).to.equal( charA );
  44. expect( charR.getPreviousSibling() ).to.equal( img );
  45. } );
  46. } );
  47. describe( 'getIndex', () => {
  48. it( 'should return null if the parent is null', () => {
  49. expect( root.getIndex() ).to.be.null;
  50. } );
  51. it( 'should return index in the parent', () => {
  52. expect( one.getIndex() ).to.equal( 0 );
  53. expect( two.getIndex() ).to.equal( 1 );
  54. expect( three.getIndex() ).to.equal( 2 );
  55. expect( charB.getIndex() ).to.equal( 0 );
  56. expect( charA.getIndex() ).to.equal( 1 );
  57. expect( img.getIndex() ).to.equal( 2 );
  58. expect( charR.getIndex() ).to.equal( 3 );
  59. } );
  60. it( 'should throw an error if parent does not contains element', () => {
  61. let f = new Text( 'f' );
  62. let bar = new Element( 'bar', [], [] );
  63. f.parent = bar;
  64. expect(
  65. () => {
  66. f.getIndex();
  67. }
  68. ).to.throw( CKEditorError, /treeview-node-not-found-in-parent/ );
  69. } );
  70. } );
  71. describe( 'getTreeView', () => {
  72. it( 'should return null if root is not a RootElement', () => {
  73. expect( charA.getTreeView() ).to.be.null;
  74. } );
  75. it( 'should return TreeView attached to the RootElement', () => {
  76. const tvMock = {};
  77. const parent = new RootElement( 'div', tvMock );
  78. const child = new Element( 'p' );
  79. child.parent = parent;
  80. expect( parent.getTreeView() ).to.equal( tvMock );
  81. expect( child.getTreeView() ).to.equal( tvMock );
  82. } );
  83. } );
  84. describe( 'change event', () => {
  85. let root, text, img;
  86. let rootChangeSpy;
  87. before( () => {
  88. rootChangeSpy = sinon.spy();
  89. } );
  90. beforeEach( () => {
  91. text = new Text( 'foo' );
  92. img = new Element( 'img' );
  93. img.setAttr( 'src', 'img.png' );
  94. root = new RootElement( 'p', { renderer: { markToSync: rootChangeSpy } } );
  95. root.appendChildren( [ text, img ] );
  96. root.on( 'change', ( evt, type, node ) => {
  97. rootChangeSpy( type, node );
  98. } );
  99. rootChangeSpy.reset();
  100. } );
  101. it( 'should be fired on the node', () => {
  102. const imgChangeSpy = sinon.spy();
  103. img.on( 'change', ( evt, type, node ) => {
  104. imgChangeSpy( type, node );
  105. } );
  106. img.setAttr( 'width', 100 );
  107. sinon.assert.calledOnce( imgChangeSpy );
  108. sinon.assert.calledWith( imgChangeSpy, 'ATTRIBUTES', img );
  109. } );
  110. it( 'should be fired on the parent', () => {
  111. img.setAttr( 'width', 100 );
  112. sinon.assert.calledOnce( rootChangeSpy );
  113. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  114. } );
  115. describe( 'setAttr', () => {
  116. it( 'should fire change event', () => {
  117. img.setAttr( 'width', 100 );
  118. sinon.assert.calledOnce( rootChangeSpy );
  119. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  120. } );
  121. } );
  122. describe( 'removeAttr', () => {
  123. it( 'should fire change event', () => {
  124. img.removeAttr( 'src' );
  125. sinon.assert.calledOnce( rootChangeSpy );
  126. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  127. } );
  128. } );
  129. describe( 'insertChildren', () => {
  130. it( 'should fire change event', () => {
  131. root.insertChildren( 1, new Element( 'img' ) );
  132. sinon.assert.calledOnce( rootChangeSpy );
  133. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  134. } );
  135. } );
  136. describe( 'appendChildren', () => {
  137. it( 'should fire change event', () => {
  138. root.appendChildren( new Element( 'img' ) );
  139. sinon.assert.calledOnce( rootChangeSpy );
  140. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  141. } );
  142. } );
  143. describe( 'removeChildren', () => {
  144. it( 'should fire change event', () => {
  145. root.removeChildren( 1, 1 );
  146. sinon.assert.calledOnce( rootChangeSpy );
  147. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  148. } );
  149. } );
  150. describe( 'removeChildren', () => {
  151. it( 'should fire change event', () => {
  152. text.setText( 'bar' );
  153. sinon.assert.calledOnce( rootChangeSpy );
  154. sinon.assert.calledWith( rootChangeSpy, 'TEXT', text );
  155. } );
  156. } );
  157. } );
  158. } );