text.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/node',
  10. 'core/treeview/element',
  11. 'core/treeview/text'
  12. );
  13. describe( 'Element', () => {
  14. let ViewText, ViewElement, ViewNode;
  15. before( () => {
  16. ViewText = modules[ 'core/treeview/text' ];
  17. ViewElement = modules[ 'core/treeview/element' ];
  18. ViewNode = modules[ 'core/treeview/node' ];
  19. } );
  20. describe( 'constructor', () => {
  21. it( 'should create element without attributes', () => {
  22. const text = new ViewText( 'foo' );
  23. expect( text ).to.be.an.instanceof( ViewNode );
  24. expect( text.getText() ).to.equal( 'foo' );
  25. expect( text ).to.have.property( 'parent' ).that.is.null;
  26. } );
  27. } );
  28. describe( 'setText', () => {
  29. it( 'should change the text', () => {
  30. const text = new ViewText( 'foo' );
  31. text.setText( 'bar' );
  32. expect( text.getText() ).to.equal( 'bar' );
  33. } );
  34. } );
  35. describe( 'DOM-View binding', () => {
  36. describe( 'getCorespondingView', () => {
  37. it( 'should return coresponding view element based on sibling', () => {
  38. const domImg = document.createElement( 'img' );
  39. const domText = document.createTextNode( 'foo' );
  40. const domP = document.createElement( 'p' );
  41. domP.appendChild( domImg );
  42. domP.appendChild( domText );
  43. const viewImg = new ViewElement( 'img' );
  44. viewImg.bindDomElement( domImg );
  45. const viewP = ViewElement.createFromDom( domP );
  46. const viewText = viewP.getChild( 1 );
  47. expect( ViewText.getCorespondingView( domText ) ).to.equal( viewText );
  48. } );
  49. it( 'should return coresponding view element based on parent', () => {
  50. const domText = document.createTextNode( 'foo' );
  51. const domP = document.createElement( 'p' );
  52. domP.appendChild( domText );
  53. const viewP = ViewElement.createFromDom( domP );
  54. const viewText = viewP.getChild( 0 );
  55. viewP.bindDomElement( domP );
  56. expect( ViewText.getCorespondingView( domText ) ).to.equal( viewText );
  57. } );
  58. it( 'should return null if sibling is not binded', () => {
  59. const domImg = document.createElement( 'img' );
  60. const domText = document.createTextNode( 'foo' );
  61. const domP = document.createElement( 'p' );
  62. domP.appendChild( domImg );
  63. domP.appendChild( domText );
  64. const viewP = ViewElement.createFromDom( domP );
  65. viewP.bindDomElement( domP );
  66. expect( ViewText.getCorespondingView( domText ) ).to.be.null;
  67. } );
  68. it( 'should return null if parent is not binded', () => {
  69. const domText = document.createTextNode( 'foo' );
  70. const domP = document.createElement( 'p' );
  71. domP.appendChild( domText );
  72. expect( ViewText.getCorespondingView( domText ) ).to.be.null;
  73. } );
  74. } );
  75. describe( 'getCorespondingDom', () => {
  76. it( 'should return coresponding DOM element based on sibling', () => {
  77. const domImg = document.createElement( 'img' );
  78. const domText = document.createTextNode( 'foo' );
  79. const domP = document.createElement( 'p' );
  80. domP.appendChild( domImg );
  81. domP.appendChild( domText );
  82. const viewImg = new ViewElement( 'img' );
  83. viewImg.bindDomElement( domImg );
  84. const viewP = ViewElement.createFromDom( domP );
  85. const viewText = viewP.getChild( 1 );
  86. expect( viewText.getCorespondingDom() ).to.equal( domText );
  87. } );
  88. it( 'should return coresponding DOM element based on parent', () => {
  89. const domText = document.createTextNode( 'foo' );
  90. const domP = document.createElement( 'p' );
  91. domP.appendChild( domText );
  92. const viewP = ViewElement.createFromDom( domP );
  93. const viewText = viewP.getChild( 0 );
  94. viewP.bindDomElement( domP );
  95. expect( viewText.getCorespondingDom() ).to.equal( domText );
  96. } );
  97. it( 'should return null if sibling is not binded', () => {
  98. const domImg = document.createElement( 'img' );
  99. const domText = document.createTextNode( 'foo' );
  100. const domP = document.createElement( 'p' );
  101. domP.appendChild( domImg );
  102. domP.appendChild( domText );
  103. const viewP = ViewElement.createFromDom( domP );
  104. const viewText = viewP.getChild( 1 );
  105. viewP.bindDomElement( domP );
  106. expect( viewText.getCorespondingDom() ).to.be.null;
  107. } );
  108. it( 'should return null if parent is not binded', () => {
  109. const domText = document.createTextNode( 'foo' );
  110. const domP = document.createElement( 'p' );
  111. domP.appendChild( domText );
  112. const viewP = ViewElement.createFromDom( domP );
  113. const viewText = viewP.getChild( 0 );
  114. expect( viewText.getCorespondingDom() ).to.be.null;
  115. } );
  116. } );
  117. } );
  118. } );