8
0

region.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. var modules = bender.amd.require( 'ckeditor', 'ui/region', 'ui/view', 'collection' );
  9. bender.tools.createSinonSandbox();
  10. describe( 'Region', function() {
  11. var region;
  12. var el;
  13. var TestViewA;
  14. var TestViewB;
  15. beforeEach( 'Create a test region instance', function() {
  16. var Region = modules[ 'ui/region' ];
  17. var View = modules[ 'ui/view' ];
  18. class A extends View {
  19. constructor() {
  20. super();
  21. this.template = { tag: 'a' };
  22. }
  23. }
  24. class B extends View {
  25. constructor() {
  26. super();
  27. this.template = { tag: 'b' };
  28. }
  29. }
  30. TestViewA = A;
  31. TestViewB = B;
  32. el = document.createElement( 'div' );
  33. region = new Region( 'foo', el );
  34. } );
  35. it( 'accepts constructor paramaters', function() {
  36. expect( region ).to.have.property( 'name', 'foo' );
  37. expect( region ).to.have.property( 'el', el );
  38. } );
  39. it( 'has views collection', function() {
  40. var Collection = modules.collection;
  41. expect( region.views ).to.be.an.instanceof( Collection );
  42. } );
  43. it( 'adds views to collection', function() {
  44. expect( region.el.childNodes.length ).to.be.equal( 0 );
  45. region.views.add( new TestViewA() );
  46. expect( region.el.childNodes.length ).to.be.equal( 1 );
  47. region.views.add( new TestViewA() );
  48. expect( region.el.childNodes.length ).to.be.equal( 2 );
  49. } );
  50. it( 'removes views from collection', function() {
  51. var viewA = new TestViewA();
  52. var viewB = new TestViewB();
  53. region.views.add( viewA );
  54. region.views.add( viewB );
  55. var childNodes = region.el.childNodes;
  56. expect( [].map.call( childNodes, n => n.nodeName ).join( ',' ) ).to.be.equal( 'A,B' );
  57. region.views.remove( viewA );
  58. expect( [].map.call( childNodes, n => n.nodeName ).join( ',' ) ).to.be.equal( 'B' );
  59. region.views.remove( viewB );
  60. expect( childNodes.length ).to.be.equal( 0 );
  61. } );
  62. it( 'destroys properly', function() {
  63. // Append the region's element to some container.
  64. var container = document.createElement( 'div' );
  65. container.appendChild( el );
  66. expect( el.parentNode ).to.be.equal( container );
  67. region.destroy();
  68. // Make sure destruction of the region does affect passed element.
  69. expect( el.parentNode ).to.be.equal( container );
  70. expect( region.el ).to.be.null;
  71. } );
  72. it( 'destroys children views', function() {
  73. var view = new TestViewA();
  74. var spy = bender.sinon.spy( view, 'destroy' );
  75. // Append the view to the region.
  76. region.views.add( view );
  77. expect( region.views ).to.have.length( 1 );
  78. region.destroy();
  79. expect( region.views ).to.have.length( 0 );
  80. expect( spy.calledOnce ).to.be.true;
  81. } );
  82. } );