8
0

model.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals describe, it, expect, beforeEach, bender, sinon */
  6. 'use strict';
  7. var modules = bender.amd.require( 'mvc/model', 'eventinfo' );
  8. var car;
  9. describe( 'Model', function() {
  10. beforeEach( 'Create a test model instance', function() {
  11. var Model = modules[ 'mvc/model' ];
  12. car = new Model( {
  13. color: 'red',
  14. year: 2015
  15. } );
  16. } );
  17. //////////
  18. it( 'should set _attributes on creation', function() {
  19. expect( car._attributes ).to.deep.equal( {
  20. color: 'red',
  21. year: 2015
  22. } );
  23. } );
  24. it( 'should get correctly after set', function() {
  25. car.color = 'blue';
  26. expect( car.color ).to.equal( 'blue' );
  27. expect( car._attributes.color ).to.equal( 'blue' );
  28. } );
  29. it( 'should get correctly after setting _attributes', function() {
  30. car._attributes.color = 'blue';
  31. expect( car.color ).to.equal( 'blue' );
  32. } );
  33. //////////
  34. describe( 'set()', function() {
  35. it( 'should work when passing an object', function() {
  36. car.set( {
  37. color: 'blue', // Override
  38. wheels: 4,
  39. seats: 5
  40. } );
  41. expect( car._attributes ).to.deep.equal( {
  42. color: 'blue',
  43. year: 2015,
  44. wheels: 4,
  45. seats: 5
  46. } );
  47. } );
  48. it( 'should work when passing key/value', function() {
  49. car.set( 'color', 'blue' );
  50. car.set( 'wheels', 4 );
  51. expect( car._attributes ).to.deep.equal( {
  52. color: 'blue',
  53. year: 2015,
  54. wheels: 4
  55. } );
  56. } );
  57. it( 'should fire "change"', function() {
  58. var EventInfo = modules.eventinfo;
  59. var spy = sinon.spy();
  60. var spyColor = sinon.spy();
  61. var spyYear = sinon.spy();
  62. var spyWheels = sinon.spy();
  63. car.on( 'change', spy );
  64. car.on( 'change:color', spyColor );
  65. car.on( 'change:year', spyYear );
  66. car.on( 'change:wheels', spyWheels );
  67. // Set property in all possible ways.
  68. car.color = 'blue';
  69. car.set( { year: 2003 } );
  70. car.set( 'wheels', 4 );
  71. // Check number of calls.
  72. sinon.assert.calledThrice( spy );
  73. sinon.assert.calledOnce( spyColor );
  74. sinon.assert.calledOnce( spyYear );
  75. sinon.assert.calledOnce( spyWheels );
  76. // Check context.
  77. sinon.assert.alwaysCalledOn( spy, car );
  78. sinon.assert.calledOn( spyColor, car );
  79. sinon.assert.calledOn( spyYear, car );
  80. sinon.assert.calledOn( spyWheels, car );
  81. // Check params.
  82. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), car, 'color', 'blue', 'red' );
  83. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), car, 'year', 2003, 2015 );
  84. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), car, 'wheels', 4, sinon.match.typeOf( 'undefined' ) );
  85. sinon.assert.calledWithExactly( spyColor, sinon.match.instanceOf( EventInfo ), car, 'blue', 'red' );
  86. sinon.assert.calledWithExactly( spyYear, sinon.match.instanceOf( EventInfo ), car, 2003, 2015 );
  87. sinon.assert.calledWithExactly( spyWheels, sinon.match.instanceOf( EventInfo ), car, 4, sinon.match.typeOf( 'undefined' ) );
  88. } );
  89. } );
  90. } );