model.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, car;
  9. describe( 'Model', function() {
  10. beforeEach( 'Create a test model instance', function() {
  11. var Model = modules[ 'mvc/model' ];
  12. Car = Model.extend();
  13. car = new Car( {
  14. color: 'red',
  15. year: 2015
  16. } );
  17. } );
  18. //////////
  19. it( 'should set _attributes on creation', function() {
  20. expect( car._attributes ).to.deep.equal( {
  21. color: 'red',
  22. year: 2015
  23. } );
  24. } );
  25. it( 'should get correctly after set', function() {
  26. car.color = 'blue';
  27. expect( car.color ).to.equal( 'blue' );
  28. expect( car._attributes.color ).to.equal( 'blue' );
  29. } );
  30. it( 'should get correctly after setting _attributes', function() {
  31. car._attributes.color = 'blue';
  32. expect( car.color ).to.equal( 'blue' );
  33. } );
  34. //////////
  35. describe( 'set', function() {
  36. it( 'should work when passing an object', function() {
  37. car.set( {
  38. color: 'blue', // Override
  39. wheels: 4,
  40. seats: 5
  41. } );
  42. expect( car._attributes ).to.deep.equal( {
  43. color: 'blue',
  44. year: 2015,
  45. wheels: 4,
  46. seats: 5
  47. } );
  48. } );
  49. it( 'should work when passing a key/value pair', function() {
  50. car.set( 'color', 'blue' );
  51. car.set( 'wheels', 4 );
  52. expect( car._attributes ).to.deep.equal( {
  53. color: 'blue',
  54. year: 2015,
  55. wheels: 4
  56. } );
  57. } );
  58. it( 'should fire the "change" event', function() {
  59. var EventInfo = modules.eventinfo;
  60. var spy = sinon.spy();
  61. var spyColor = sinon.spy();
  62. var spyYear = sinon.spy();
  63. var spyWheels = sinon.spy();
  64. car.on( 'change', spy );
  65. car.on( 'change:color', spyColor );
  66. car.on( 'change:year', spyYear );
  67. car.on( 'change:wheels', spyWheels );
  68. // Set property in all possible ways.
  69. car.color = 'blue';
  70. car.set( { year: 2003 } );
  71. car.set( 'wheels', 4 );
  72. // Check number of calls.
  73. sinon.assert.calledThrice( spy );
  74. sinon.assert.calledOnce( spyColor );
  75. sinon.assert.calledOnce( spyYear );
  76. sinon.assert.calledOnce( spyWheels );
  77. // Check context.
  78. sinon.assert.alwaysCalledOn( spy, car );
  79. sinon.assert.calledOn( spyColor, car );
  80. sinon.assert.calledOn( spyYear, car );
  81. sinon.assert.calledOn( spyWheels, car );
  82. // Check params.
  83. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'color', 'blue', 'red' );
  84. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'year', 2003, 2015 );
  85. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'wheels', 4, sinon.match.typeOf( 'undefined' ) );
  86. sinon.assert.calledWithExactly( spyColor, sinon.match.instanceOf( EventInfo ), 'blue', 'red' );
  87. sinon.assert.calledWithExactly( spyYear, sinon.match.instanceOf( EventInfo ), 2003, 2015 );
  88. sinon.assert.calledWithExactly( spyWheels, sinon.match.instanceOf( EventInfo ), 4, sinon.match.typeOf( 'undefined' ) );
  89. } );
  90. it( 'should not fire the "change" event for the same attribute value', function() {
  91. var spy = sinon.spy();
  92. var spyColor = sinon.spy();
  93. car.on( 'change', spy );
  94. car.on( 'change:color', spyColor );
  95. // Set the "color" property in all possible ways.
  96. car.color = 'red';
  97. car.set( 'color', 'red' );
  98. car.set( { color: 'red' } );
  99. sinon.assert.notCalled( spy );
  100. sinon.assert.notCalled( spyColor );
  101. } );
  102. } );
  103. describe( 'extend', function() {
  104. it( 'should create new Model based classes', function() {
  105. var Model = modules[ 'mvc/model' ];
  106. var Truck = Car.extend();
  107. var truck = new Truck();
  108. expect( truck ).to.be.an.instanceof( Car );
  109. expect( truck ).to.be.an.instanceof( Model );
  110. } );
  111. } );
  112. } );