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