8
0

text.js 4.6 KB

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