view.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. /* bender-tags: core, ui */
  7. 'use strict';
  8. var modules = bender.amd.require( 'ckeditor', 'ui/view', 'ui/region', 'ckeditorerror', 'model' );
  9. bender.tools.createSinonSandbox();
  10. describe( 'View', function() {
  11. var view;
  12. var View;
  13. var TestView;
  14. beforeEach( 'Create a test view instance', function() {
  15. View = modules[ 'ui/view' ];
  16. view = new View( {
  17. a: 'foo',
  18. b: 42
  19. } );
  20. class T extends View {
  21. constructor() {
  22. super();
  23. this.template = { tag: 'a' };
  24. }
  25. }
  26. TestView = T;
  27. } );
  28. it( 'accepts the model', function() {
  29. expect( view.model ).to.be.an.instanceof( modules.model );
  30. expect( view ).to.have.deep.property( 'model.a', 'foo' );
  31. expect( view ).to.have.deep.property( 'model.b', 42 );
  32. } );
  33. it( 'has no default element', function() {
  34. expect( () => view.el ).to.throw( modules.ckeditorerror );
  35. } );
  36. it( 'has no default template', function() {
  37. expect( view.template ).to.be.undefined();
  38. } );
  39. it( 'has no default regions', function() {
  40. expect( view.regions ).to.have.length( 0 );
  41. } );
  42. it( 'provides binding to the model', function() {
  43. var spy = sinon.spy();
  44. var callback = view.bind( 'a', spy );
  45. expect( spy.called ).to.be.false;
  46. callback( 'el', 'updater' );
  47. sinon.assert.calledOnce( spy );
  48. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  49. spy.reset();
  50. view.model.a = 'bar';
  51. sinon.assert.calledOnce( spy );
  52. sinon.assert.calledWithExactly( spy, 'el', 'bar' );
  53. } );
  54. it( 'renders element from template', function() {
  55. view = new TestView( { a: 1 } );
  56. expect( view.el ).to.be.an.instanceof( HTMLElement );
  57. expect( view.el.nodeName ).to.be.equal( 'A' );
  58. } );
  59. it( 'destroys properly', function() {
  60. view = new TestView( { a: 1 } );
  61. // Append the views's element to some container.
  62. var container = document.createElement( 'div' );
  63. container.appendChild( view.el );
  64. expect( view.el.nodeName ).to.be.equal( 'A' );
  65. expect( view.el ).to.be.an.instanceof( HTMLElement );
  66. expect( view.el.parentNode ).to.be.equal( container );
  67. expect( view.model ).to.be.an.instanceof( modules.model );
  68. view.destroy();
  69. expect( view.el ).to.be.an.instanceof( HTMLElement );
  70. expect( view.el.parentNode ).to.be.null;
  71. expect( view.model ).to.be.null;
  72. } );
  73. it( 'destroys child regions', function() {
  74. var Region = modules[ 'ui/region' ];
  75. var region = new Region();
  76. var spy = bender.sinon.spy( region, 'destroy' );
  77. view.regions.add( region );
  78. view.destroy();
  79. expect( view.regions ).to.have.length( 0 );
  80. expect( spy.calledOnce ).to.be.true;
  81. } );
  82. } );