node.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Text from '/ckeditor5/core/treeview/text.js';
  9. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  10. describe( 'Node', () => {
  11. let root;
  12. let one, two, three;
  13. let charB, charA, charR, img;
  14. before( () => {
  15. charB = new Text( 'b' );
  16. charA = new Text( 'a' );
  17. img = new Element( 'img' );
  18. charR = new Text( 'r' );
  19. one = new Element( 'one' );
  20. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  21. three = new Element( 'three' );
  22. root = new Element( null, null, [ one, two, three ] );
  23. } );
  24. describe( 'getNextSibling/getPreviousSibling', () => {
  25. it( 'should return next sibling', () => {
  26. expect( root.getNextSibling() ).to.be.null;
  27. expect( one.getNextSibling() ).to.equal( two );
  28. expect( two.getNextSibling() ).to.equal( three );
  29. expect( three.getNextSibling() ).to.be.null;
  30. expect( charB.getNextSibling() ).to.equal( charA );
  31. expect( charA.getNextSibling() ).to.equal( img );
  32. expect( img.getNextSibling() ).to.equal( charR );
  33. expect( charR.getNextSibling() ).to.be.null;
  34. } );
  35. it( 'should return previous sibling', () => {
  36. expect( root.getPreviousSibling() ).to.be.null;
  37. expect( one.getPreviousSibling() ).to.be.null;
  38. expect( two.getPreviousSibling() ).to.equal( one );
  39. expect( three.getPreviousSibling() ).to.equal( two );
  40. expect( charB.getPreviousSibling() ).to.be.null;
  41. expect( charA.getPreviousSibling() ).to.equal( charB );
  42. expect( img.getPreviousSibling() ).to.equal( charA );
  43. expect( charR.getPreviousSibling() ).to.equal( img );
  44. } );
  45. } );
  46. describe( 'getIndex', () => {
  47. it( 'should return null if the parent is null', () => {
  48. expect( root.getIndex() ).to.be.null;
  49. } );
  50. it( 'should return index in the parent', () => {
  51. expect( one.getIndex() ).to.equal( 0 );
  52. expect( two.getIndex() ).to.equal( 1 );
  53. expect( three.getIndex() ).to.equal( 2 );
  54. expect( charB.getIndex() ).to.equal( 0 );
  55. expect( charA.getIndex() ).to.equal( 1 );
  56. expect( img.getIndex() ).to.equal( 2 );
  57. expect( charR.getIndex() ).to.equal( 3 );
  58. } );
  59. it( 'should throw an error if parent does not contain element', () => {
  60. let f = new Text( 'f' );
  61. let bar = new Element( 'bar', [], [] );
  62. f.parent = bar;
  63. expect(
  64. () => {
  65. f.getIndex();
  66. }
  67. ).to.throw( CKEditorError, /treeview-node-not-found-in-parent/ );
  68. } );
  69. } );
  70. describe( 'getTreeView', () => {
  71. it( 'should return null if any parent has not set treeview', () => {
  72. expect( charA.getTreeView() ).to.be.null;
  73. } );
  74. it( 'should return TreeView attached to the element', () => {
  75. const tvMock = {};
  76. const element = new Element( 'p' );
  77. element.setTreeView( tvMock );
  78. expect( element.getTreeView() ).to.equal( tvMock );
  79. } );
  80. it( 'should return TreeView attached to the parent element', () => {
  81. const tvMock = {};
  82. const parent = new Element( 'div' );
  83. const child = new Element( 'p' );
  84. child.parent = parent;
  85. parent.setTreeView( tvMock );
  86. expect( parent.getTreeView() ).to.equal( tvMock );
  87. expect( child.getTreeView() ).to.equal( tvMock );
  88. } );
  89. } );
  90. describe( 'remove', () => {
  91. it( 'should remove node from its parent', () => {
  92. const char = new Text( 'a' );
  93. const parent = new Element( 'p', null, [ char ] );
  94. char.remove();
  95. expect( parent.getChildIndex( char ) ).to.equal( -1 );
  96. } );
  97. it( 'uses parent.removeChildren method', () => {
  98. const char = new Text( 'a' );
  99. const parent = new Element( 'p', null, [ char ] );
  100. const removeChildrenSpy = sinon.spy( parent, 'removeChildren' );
  101. const index = char.getIndex();
  102. char.remove();
  103. removeChildrenSpy.restore();
  104. sinon.assert.calledOnce( removeChildrenSpy );
  105. sinon.assert.calledWithExactly( removeChildrenSpy, index );
  106. } );
  107. } );
  108. describe( 'change event', () => {
  109. let root, text, img;
  110. let rootChangeSpy;
  111. before( () => {
  112. rootChangeSpy = sinon.spy();
  113. } );
  114. beforeEach( () => {
  115. text = new Text( 'foo' );
  116. img = new Element( 'img' );
  117. img.setAttribute( 'src', 'img.png' );
  118. root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
  119. root.appendChildren( [ text, img ] );
  120. root.on( 'change', ( evt, type, node ) => {
  121. rootChangeSpy( type, node );
  122. } );
  123. rootChangeSpy.reset();
  124. } );
  125. it( 'should be fired on the node', () => {
  126. const imgChangeSpy = sinon.spy();
  127. img.on( 'change', ( evt, type, node ) => {
  128. imgChangeSpy( type, node );
  129. } );
  130. img.setAttribute( 'width', 100 );
  131. sinon.assert.calledOnce( imgChangeSpy );
  132. sinon.assert.calledWith( imgChangeSpy, 'ATTRIBUTES', img );
  133. } );
  134. it( 'should be fired on the parent', () => {
  135. img.setAttribute( 'width', 100 );
  136. sinon.assert.calledOnce( rootChangeSpy );
  137. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  138. } );
  139. describe( 'setAttr', () => {
  140. it( 'should fire change event', () => {
  141. img.setAttribute( 'width', 100 );
  142. sinon.assert.calledOnce( rootChangeSpy );
  143. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  144. } );
  145. } );
  146. describe( 'removeAttr', () => {
  147. it( 'should fire change event', () => {
  148. img.removeAttribute( 'src' );
  149. sinon.assert.calledOnce( rootChangeSpy );
  150. sinon.assert.calledWith( rootChangeSpy, 'ATTRIBUTES', img );
  151. } );
  152. } );
  153. describe( 'insertChildren', () => {
  154. it( 'should fire change event', () => {
  155. root.insertChildren( 1, new Element( 'img' ) );
  156. sinon.assert.calledOnce( rootChangeSpy );
  157. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  158. } );
  159. } );
  160. describe( 'appendChildren', () => {
  161. it( 'should fire change event', () => {
  162. root.appendChildren( new Element( 'img' ) );
  163. sinon.assert.calledOnce( rootChangeSpy );
  164. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  165. } );
  166. } );
  167. describe( 'removeChildren', () => {
  168. it( 'should fire change event', () => {
  169. root.removeChildren( 1, 1 );
  170. sinon.assert.calledOnce( rootChangeSpy );
  171. sinon.assert.calledWith( rootChangeSpy, 'CHILDREN', root );
  172. } );
  173. } );
  174. describe( 'removeChildren', () => {
  175. it( 'should fire change event', () => {
  176. text.data = 'bar';
  177. sinon.assert.calledOnce( rootChangeSpy );
  178. sinon.assert.calledWith( rootChangeSpy, 'TEXT', text );
  179. } );
  180. } );
  181. } );
  182. } );