8
0

region.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. /* bender-tags: core, ui */
  7. 'use strict';
  8. const modules = bender.amd.require( 'ckeditor', 'ui/region', 'ui/view', 'collection' );
  9. bender.tools.createSinonSandbox();
  10. let TestViewA, TestViewB;
  11. let region, el;
  12. beforeEach( createRegionInstance );
  13. describe( 'constructor', () => {
  14. it( 'accepts name and element', () => {
  15. expect( region ).to.have.property( 'name', 'foo' );
  16. expect( region ).to.have.property( 'el', el );
  17. } );
  18. } );
  19. describe( 'views collection', () => {
  20. it( 'is an instance of Collection', () => {
  21. const Collection = modules.collection;
  22. expect( region.views ).to.be.an.instanceof( Collection );
  23. } );
  24. it( 'updates DOM when adding views', () => {
  25. expect( region.el.childNodes.length ).to.be.equal( 0 );
  26. region.views.add( new TestViewA() );
  27. expect( region.el.childNodes.length ).to.be.equal( 1 );
  28. region.views.add( new TestViewA() );
  29. expect( region.el.childNodes.length ).to.be.equal( 2 );
  30. } );
  31. it( 'updates DOM when removing views', () => {
  32. let viewA = new TestViewA();
  33. let viewB = new TestViewB();
  34. region.views.add( viewA );
  35. region.views.add( viewB );
  36. expect( el.childNodes.length ).to.be.equal( 2 );
  37. expect( el.firstChild.nodeName ).to.be.equal( 'A' );
  38. expect( el.lastChild.nodeName ).to.be.equal( 'B' );
  39. region.views.remove( viewA );
  40. expect( el.childNodes.length ).to.be.equal( 1 );
  41. expect( el.firstChild.nodeName ).to.be.equal( 'B' );
  42. region.views.remove( viewB );
  43. expect( el.childNodes.length ).to.be.equal( 0 );
  44. } );
  45. } );
  46. describe( 'destroy', () => {
  47. it( 'destroys the region', () => {
  48. // Append the region's element to some container.
  49. let container = document.createElement( 'div' );
  50. container.appendChild( el );
  51. expect( el.parentNode ).to.be.equal( container );
  52. region.destroy();
  53. // Make sure destruction of the region does affect passed element.
  54. expect( el.parentNode ).to.be.equal( container );
  55. expect( region.el ).to.be.null;
  56. } );
  57. it( 'destroys children views', () => {
  58. let view = new TestViewA();
  59. let spy = bender.sinon.spy( view, 'destroy' );
  60. // Append the view to the region.
  61. region.views.add( view );
  62. expect( region.views ).to.have.length( 1 );
  63. region.destroy();
  64. expect( region.views ).to.have.length( 0 );
  65. expect( spy.calledOnce ).to.be.true;
  66. } );
  67. } );
  68. function createRegionInstance() {
  69. const Region = modules[ 'ui/region' ];
  70. const View = modules[ 'ui/view' ];
  71. class A extends View {
  72. constructor() {
  73. super();
  74. this.template = { tag: 'a' };
  75. }
  76. }
  77. class B extends View {
  78. constructor() {
  79. super();
  80. this.template = { tag: 'b' };
  81. }
  82. }
  83. TestViewA = A;
  84. TestViewB = B;
  85. el = document.createElement( 'div' );
  86. region = new Region( 'foo', el );
  87. }