8
0

model.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. it( 'should add properties on creation', function() {
  35. var car = new Car( null, {
  36. prop: 1
  37. } );
  38. expect( car ).to.have.property( 'prop' ).to.equals( 1 );
  39. } );
  40. //////////
  41. describe( 'set', function() {
  42. it( 'should work when passing an object', function() {
  43. car.set( {
  44. color: 'blue', // Override
  45. wheels: 4,
  46. seats: 5
  47. } );
  48. expect( car._attributes ).to.deep.equal( {
  49. color: 'blue',
  50. year: 2015,
  51. wheels: 4,
  52. seats: 5
  53. } );
  54. } );
  55. it( 'should work when passing a key/value pair', function() {
  56. car.set( 'color', 'blue' );
  57. car.set( 'wheels', 4 );
  58. expect( car._attributes ).to.deep.equal( {
  59. color: 'blue',
  60. year: 2015,
  61. wheels: 4
  62. } );
  63. } );
  64. it( 'should fire the "change" event', function() {
  65. var EventInfo = modules.eventinfo;
  66. var spy = sinon.spy();
  67. var spyColor = sinon.spy();
  68. var spyYear = sinon.spy();
  69. var spyWheels = sinon.spy();
  70. car.on( 'change', spy );
  71. car.on( 'change:color', spyColor );
  72. car.on( 'change:year', spyYear );
  73. car.on( 'change:wheels', spyWheels );
  74. // Set property in all possible ways.
  75. car.color = 'blue';
  76. car.set( { year: 2003 } );
  77. car.set( 'wheels', 4 );
  78. // Check number of calls.
  79. sinon.assert.calledThrice( spy );
  80. sinon.assert.calledOnce( spyColor );
  81. sinon.assert.calledOnce( spyYear );
  82. sinon.assert.calledOnce( spyWheels );
  83. // Check context.
  84. sinon.assert.alwaysCalledOn( spy, car );
  85. sinon.assert.calledOn( spyColor, car );
  86. sinon.assert.calledOn( spyYear, car );
  87. sinon.assert.calledOn( spyWheels, car );
  88. // Check params.
  89. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'color', 'blue', 'red' );
  90. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'year', 2003, 2015 );
  91. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'wheels', 4, sinon.match.typeOf( 'undefined' ) );
  92. sinon.assert.calledWithExactly( spyColor, sinon.match.instanceOf( EventInfo ), 'blue', 'red' );
  93. sinon.assert.calledWithExactly( spyYear, sinon.match.instanceOf( EventInfo ), 2003, 2015 );
  94. sinon.assert.calledWithExactly( spyWheels, sinon.match.instanceOf( EventInfo ), 4, sinon.match.typeOf( 'undefined' ) );
  95. } );
  96. it( 'should not fire the "change" event for the same attribute value', function() {
  97. var spy = sinon.spy();
  98. var spyColor = sinon.spy();
  99. car.on( 'change', spy );
  100. car.on( 'change:color', spyColor );
  101. // Set the "color" property in all possible ways.
  102. car.color = 'red';
  103. car.set( 'color', 'red' );
  104. car.set( { color: 'red' } );
  105. sinon.assert.notCalled( spy );
  106. sinon.assert.notCalled( spyColor );
  107. } );
  108. } );
  109. describe( 'extend', function() {
  110. it( 'should create new Model based classes', function() {
  111. var Model = modules[ 'mvc/model' ];
  112. var Truck = Car.extend();
  113. var truck = new Truck();
  114. expect( truck ).to.be.an.instanceof( Car );
  115. expect( truck ).to.be.an.instanceof( Model );
  116. } );
  117. } );
  118. } );