8
0

node.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. const modules = bender.amd.require(
  9. 'core/treeview/element',
  10. 'core/treeview/rootelement',
  11. 'core/treeview/node',
  12. 'core/treeview/text',
  13. 'core/ckeditorerror'
  14. );
  15. describe( 'Node', () => {
  16. let Element, Text, Node, RootElement, CKEditorError;
  17. let root;
  18. let one, two, three;
  19. let charB, charA, charR, img;
  20. before( () => {
  21. Element = modules[ 'core/treeview/element' ];
  22. Node = modules[ 'core/treeview/node' ];
  23. Text = modules[ 'core/treeview/text' ];
  24. CKEditorError = modules[ 'ckeditorerror ' ];
  25. RootElement = modules[ 'core/treeview/rootelement' ];
  26. charB = new Text( 'b' );
  27. charA = new Text( 'a' );
  28. img = new Element( 'img' );
  29. charR = new Text( 'r' );
  30. one = new Element( 'one' );
  31. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  32. three = new Element( 'three' );
  33. root = new Element( null, null, [ one, two, three ] );
  34. } );
  35. describe( 'getNextSibling/getPreviousSibling', () => {
  36. it( 'should return next sibling', () => {
  37. expect( root.getNextSibling() ).to.be.null;
  38. expect( one.getNextSibling() ).to.equal( two );
  39. expect( two.getNextSibling() ).to.equal( three );
  40. expect( three.getNextSibling() ).to.be.null;
  41. expect( charB.getNextSibling() ).to.equal( charA );
  42. expect( charA.getNextSibling() ).to.equal( img );
  43. expect( img.getNextSibling() ).to.equal( charR );
  44. expect( charR.getNextSibling() ).to.be.null;
  45. } );
  46. it( 'should return previous sibling', () => {
  47. expect( root.getPreviousSibling() ).to.be.null;
  48. expect( one.getPreviousSibling() ).to.be.null;
  49. expect( two.getPreviousSibling() ).to.equal( one );
  50. expect( three.getPreviousSibling() ).to.equal( two );
  51. expect( charB.getPreviousSibling() ).to.be.null;
  52. expect( charA.getPreviousSibling() ).to.equal( charB );
  53. expect( img.getPreviousSibling() ).to.equal( charA );
  54. expect( charR.getPreviousSibling() ).to.equal( img );
  55. } );
  56. } );
  57. describe( 'getIndex', () => {
  58. it( 'should return null if the parent is null', () => {
  59. expect( root.getIndex() ).to.be.null;
  60. } );
  61. it( 'should return index in the parent', () => {
  62. expect( one.getIndex() ).to.equal( 0 );
  63. expect( two.getIndex() ).to.equal( 1 );
  64. expect( three.getIndex() ).to.equal( 2 );
  65. expect( charB.getIndex() ).to.equal( 0 );
  66. expect( charA.getIndex() ).to.equal( 1 );
  67. expect( img.getIndex() ).to.equal( 2 );
  68. expect( charR.getIndex() ).to.equal( 3 );
  69. } );
  70. it( 'should throw an error if parent does not contains element', () => {
  71. let f = new Text( 'f' );
  72. let bar = new Element( 'bar', [], [] );
  73. f.parent = bar;
  74. expect(
  75. () => {
  76. f.getIndex();
  77. }
  78. ).to.throw( CKEditorError, /treeview-node-not-found-in-parent/ );
  79. } );
  80. } );
  81. describe( 'getTreeView', () => {
  82. it( 'should return null if root is not a RootElement', () => {
  83. expect( charA.getTreeView() ).to.be.null;
  84. } );
  85. it( 'should return TreeView attached to the RootElement', () => {
  86. const tvMock = {};
  87. const parent = new RootElement( 'div', tvMock );
  88. const child = new Element( 'p' );
  89. child.parent = parent;
  90. expect( parent.getTreeView() ).to.equal( tvMock );
  91. expect( child.getTreeView() ).to.equal( tvMock );
  92. } );
  93. } );
  94. } );