region.js 2.8 KB

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