region.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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( 'is destroyed properly', function() {
  63. var view = new TestViewA();
  64. var spy = bender.sinon.spy( view, 'destroy' );
  65. region.views.add( view );
  66. expect( region.views ).to.have.length( 1 );
  67. region.destroy();
  68. expect( region.el ).to.be.null;
  69. expect( region.views ).to.have.length( 0 );
  70. expect( spy.calledOnce ).to.be.true;
  71. } );
  72. } );