view.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: core, ui */
  6. 'use strict';
  7. var modules = bender.amd.require( 'ckeditor', 'ui/view', 'ui/region', 'ckeditorerror' );
  8. bender.tools.createSinonSandbox();
  9. describe( 'View', function() {
  10. var view;
  11. var View;
  12. beforeEach( 'Create a test view instance', function() {
  13. View = modules[ 'ui/view' ];
  14. view = new View( {
  15. a: 'foo',
  16. b: 42
  17. } );
  18. } );
  19. it( 'accepts the model', function() {
  20. expect( view ).to.have.deep.property( 'model.a', 'foo' );
  21. expect( view ).to.have.deep.property( 'model.b', 42 );
  22. } );
  23. it( 'has no default element', function() {
  24. expect( () => view.el ).to.throw( modules.ckeditorerror );
  25. } );
  26. it( 'has no default template', function() {
  27. expect( view.template ).to.be.undefined();
  28. } );
  29. it( 'has no default regions', function() {
  30. expect( view.regions.length ).to.be.equal( 0 );
  31. } );
  32. it( 'provides binding to the model', function() {
  33. var spy = sinon.spy();
  34. var callback = view.bind( 'a', spy );
  35. expect( spy.called ).to.be.false;
  36. callback( 'el', 'updater' );
  37. sinon.assert.calledOnce( spy );
  38. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  39. spy.reset();
  40. view.model.a = 'bar';
  41. sinon.assert.calledOnce( spy );
  42. sinon.assert.calledWithExactly( spy, 'el', 'bar' );
  43. } );
  44. it( 'is destroyed properly', function() {
  45. var Region = modules[ 'ui/region' ];
  46. var region = new Region();
  47. var spy = bender.sinon.spy( region, 'destroy' );
  48. class TestView extends View {
  49. constructor() {
  50. super();
  51. this.template = { tag: 'a' };
  52. }
  53. }
  54. view = new TestView();
  55. view.regions.add( region );
  56. view.destroy();
  57. expect( view.el.parentNode ).to.be.null;
  58. expect( view.regions ).to.have.length( 0 );
  59. expect( spy.calledOnce ).to.be.true;
  60. } );
  61. } );