region.js 3.3 KB

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