8
0

region.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. /* bender-tags: ui */
  7. import Region from 'ckeditor5/ui/region.js';
  8. import View from 'ckeditor5/ui/view.js';
  9. import Template from 'ckeditor5/ui/template.js';
  10. let TestViewA, TestViewB;
  11. let region, el;
  12. describe( 'View', () => {
  13. beforeEach( createRegionInstance );
  14. describe( 'constructor()', () => {
  15. it( 'accepts name', () => {
  16. expect( region.name ).to.be.equal( 'foo' );
  17. expect( region.element ).to.be.null;
  18. expect( region.views.length ).to.be.equal( 0 );
  19. } );
  20. } );
  21. describe( 'init', () => {
  22. it( 'accepts region element', () => {
  23. region.init( el );
  24. expect( region.element ).to.be.equal( el );
  25. } );
  26. } );
  27. describe( 'views collection', () => {
  28. it( 'updates DOM when adding views', () => {
  29. let view;
  30. region.init( el );
  31. expect( region.element.childNodes.length ).to.be.equal( 0 );
  32. view = new TestViewA();
  33. region.views.add( view, 0 );
  34. expect( region.element.childNodes.length ).to.be.equal( 1 );
  35. region.views.add( new TestViewA() );
  36. expect( region.element.childNodes.length ).to.be.equal( 2 );
  37. view = new TestViewA();
  38. region.views.add( view, 1 );
  39. expect( region.element.childNodes.length ).to.be.equal( 3 );
  40. expect( region.element.childNodes[ 1 ] ).to.equal( view.element );
  41. view = new TestViewA();
  42. region.views.add( view, 0 );
  43. expect( region.element.childNodes.length ).to.be.equal( 4 );
  44. expect( region.element.childNodes[ 0 ] ).to.equal( view.element );
  45. } );
  46. it( 'does not update DOM when no region element', () => {
  47. region.init();
  48. expect( () => {
  49. region.views.add( new TestViewA() );
  50. region.views.add( new TestViewA() );
  51. } ).to.not.throw();
  52. } );
  53. it( 'updates DOM when removing views', () => {
  54. region.init( el );
  55. let viewA = new TestViewA();
  56. let viewB = new TestViewB();
  57. region.views.add( viewA );
  58. region.views.add( viewB );
  59. expect( el.childNodes.length ).to.be.equal( 2 );
  60. expect( el.firstChild.nodeName ).to.be.equal( 'A' );
  61. expect( el.lastChild.nodeName ).to.be.equal( 'B' );
  62. region.views.remove( viewA );
  63. expect( el.childNodes.length ).to.be.equal( 1 );
  64. expect( el.firstChild.nodeName ).to.be.equal( 'B' );
  65. region.views.remove( viewB );
  66. expect( el.childNodes.length ).to.be.equal( 0 );
  67. } );
  68. } );
  69. describe( 'destroy', () => {
  70. it( 'destroys the region', () => {
  71. region.init( el );
  72. region.views.add( new TestViewA() );
  73. const elRef = region.element;
  74. const viewsRef = region.views;
  75. region.destroy();
  76. expect( elRef.parentNode ).to.be.null;
  77. expect( region.name ).to.be.equal( 'foo' );
  78. expect( region.views ).to.be.null;
  79. expect( viewsRef.length ).to.be.equal( 0 );
  80. expect( region.element ).to.be.null;
  81. } );
  82. it( 'destroys an element–less region', () => {
  83. region.init();
  84. expect( () => {
  85. region.destroy();
  86. } ).to.not.throw();
  87. } );
  88. } );
  89. } );
  90. function createRegionInstance() {
  91. class A extends View {
  92. constructor() {
  93. super();
  94. this.template = new Template( { tag: 'a' } );
  95. }
  96. }
  97. class B extends View {
  98. constructor() {
  99. super();
  100. this.template = new Template( { tag: 'b' } );
  101. }
  102. }
  103. TestViewA = A;
  104. TestViewB = B;
  105. el = document.createElement( 'div' );
  106. region = new Region( 'foo' );
  107. }