model.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require( 'model', 'eventinfo', 'ckeditorerror' );
  7. let Car, car;
  8. bender.tools.createSinonSandbox();
  9. describe( 'Model', () => {
  10. beforeEach( 'Create a test model instance', () => {
  11. const Model = modules.model;
  12. Car = class extends Model {};
  13. car = new Car( {
  14. color: 'red',
  15. year: 2015
  16. } );
  17. } );
  18. //////////
  19. it( 'should set _attributes on creation', () => {
  20. expect( car._attributes ).to.deep.equal( {
  21. color: 'red',
  22. year: 2015
  23. } );
  24. } );
  25. it( 'should get correctly after set', () => {
  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', () => {
  31. car._attributes.color = 'blue';
  32. expect( car.color ).to.equal( 'blue' );
  33. } );
  34. it( 'should add properties on creation', () => {
  35. let car = new Car( null, {
  36. prop: 1
  37. } );
  38. expect( car ).to.have.property( 'prop' ).to.equal( 1 );
  39. } );
  40. //////////
  41. describe( 'set', () => {
  42. it( 'should work when passing an object', () => {
  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', () => {
  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', () => {
  65. const EventInfo = modules.eventinfo;
  66. let spy = sinon.spy();
  67. let spyColor = sinon.spy();
  68. let spyYear = sinon.spy();
  69. let 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', () => {
  97. let spy = sinon.spy();
  98. let 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. it( 'should throw when overriding already existing property', () => {
  109. const CKEditorError = modules.ckeditorerror;
  110. car.normalProperty = 1;
  111. expect( () => {
  112. car.set( 'normalProperty', 2 );
  113. } ).to.throw( CKEditorError, /^model-set-cannot-override/ );
  114. expect( car ).to.have.property( 'normalProperty', 1 );
  115. } );
  116. it( 'should throw when overriding already existing property (in the prototype)', () => {
  117. const CKEditorError = modules.ckeditorerror;
  118. const Model = modules.model;
  119. class Car extends Model {
  120. method() {}
  121. }
  122. car = new Car();
  123. expect( () => {
  124. car.set( 'method', 2 );
  125. } ).to.throw( CKEditorError, /^model-set-cannot-override/ );
  126. expect( car.method ).to.be.a( 'function' );
  127. } );
  128. } );
  129. describe( 'extend', () => {
  130. it( 'should create new Model based classes', () => {
  131. const Model = modules.model;
  132. class Truck extends Car {}
  133. let truck = new Truck();
  134. expect( truck ).to.be.an.instanceof( Car );
  135. expect( truck ).to.be.an.instanceof( Model );
  136. } );
  137. } );
  138. describe( 'bind', () => {
  139. let Model, EventInfo, CKEditorError;
  140. beforeEach( () => {
  141. Model = modules.model;
  142. EventInfo = modules.eventinfo;
  143. CKEditorError = modules.ckeditorerror;
  144. } );
  145. it( 'should chain for a single attribute', () => {
  146. expect( car.bind( 'color' ) ).to.contain.keys( 'to' );
  147. } );
  148. it( 'should chain for multiple attributes', () => {
  149. expect( car.bind( 'color', 'year' ) ).to.contain.keys( 'to' );
  150. } );
  151. it( 'should chain for nonexistent attributes', () => {
  152. expect( car.bind( 'nonexistent' ) ).to.contain.keys( 'to' );
  153. } );
  154. it( 'should throw when no attributes specified', () => {
  155. const CKEditorError = modules.ckeditorerror;
  156. expect( () => {
  157. car.bind();
  158. } ).to.throw( CKEditorError, /model-bind-no-attrs/ );
  159. } );
  160. it( 'should throw when attributes are not strings', () => {
  161. expect( () => {
  162. car.bind( new Date() );
  163. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  164. expect( () => {
  165. car.bind( 'color', new Date() );
  166. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  167. } );
  168. it( 'should throw when the same attribute is used than once', () => {
  169. expect( () => {
  170. car.bind( 'color', 'color' );
  171. } ).to.throw( CKEditorError, /model-bind-duplicate-attrs/ );
  172. } );
  173. it( 'should throw when binding the same attribute more than once', () => {
  174. expect( () => {
  175. car.bind( 'color' );
  176. car.bind( 'color' );
  177. } ).to.throw( CKEditorError, /model-bind-rebind/ );
  178. } );
  179. describe( 'to', () => {
  180. it( 'should chain', () => {
  181. const returned = car.bind( 'color' ).to( new Model( { color: 'red' } ) );
  182. expect( returned ).to.have.property( 'to' );
  183. expect( returned ).to.have.property( 'as' );
  184. } );
  185. it( 'should chain multiple times', () => {
  186. const returned = car.bind( 'color' )
  187. .to( new Model( { color: 'red' } ) )
  188. .to( new Model( { color: 'red' } ) )
  189. .to( new Model( { color: 'red' } ) );
  190. expect( returned ).to.have.property( 'to' );
  191. expect( returned ).to.have.property( 'as' );
  192. } );
  193. it( 'should throw when no .to() model', () => {
  194. expect( () => {
  195. car.bind( 'color' ).to();
  196. } ).to.throw( CKEditorError, /model-bind-to-no-model/ );
  197. } );
  198. it( 'should throw when .to() model is not Model', () => {
  199. expect( () => {
  200. car.bind( 'color' ).to( 'it\'s not a model' );
  201. } ).to.throw( CKEditorError, /model-bind-to-wrong-model/ );
  202. } );
  203. it( 'should throw when attributes are not strings', () => {
  204. expect( () => {
  205. car.bind( 'color' ).to( new Model(), new Date() );
  206. } ).to.throw( CKEditorError, /model-bind-to-wrong-attrs/ );
  207. expect( () => {
  208. car = new Car( { color: 'red' } );
  209. car.bind( 'color' ).to( new Model(), 'color', new Date() );
  210. } ).to.throw( CKEditorError, /model-bind-to-wrong-attrs/ );
  211. } );
  212. it( 'should throw when a number of attributes does not match', () => {
  213. expect( () => {
  214. const vehicle = new Car();
  215. vehicle.bind( 'color', 'year' ).to( car, 'color' );
  216. } ).to.throw( CKEditorError, /model-bind-to-attrs-length/ );
  217. expect( () => {
  218. const vehicle = new Car();
  219. vehicle.bind( 'color' ).to( car, 'color', 'year' );
  220. } ).to.throw( CKEditorError, /model-bind-to-attrs-length/ );
  221. expect( () => {
  222. const vehicle = new Car();
  223. vehicle.bind( 'color' ).to( car, 'color' ).to( car, 'color', 'year' );
  224. } ).to.throw( CKEditorError, /model-bind-to-attrs-length/ );
  225. } );
  226. it( 'should throw when binding to an nonexistent attribute in the model', () => {
  227. const vehicle = new Car();
  228. expect( () => {
  229. vehicle.bind( 'color' ).to( car, 'nonexistent in car' );
  230. } ).to.throw( CKEditorError, /model-bind-to-missing-model-attr/ );
  231. } );
  232. it( 'should throw when no attribute specified and those from bind() don\'t exist in to() model', () => {
  233. const vehicle = new Car();
  234. expect( () => {
  235. vehicle.bind( 'nonexistent in car' ).to( car );
  236. } ).to.throw( CKEditorError, /model-bind-to-missing-to-attr/ );
  237. } );
  238. it( 'should throw when to() more than once and multiple attributes', () => {
  239. const car1 = new Car( { color: 'red', year: 2000 } );
  240. const car2 = new Car( { color: 'red', year: 2000 } );
  241. expect( () => {
  242. const vehicle = new Car();
  243. vehicle.bind( 'color', 'year' ).to( car1 ).to( car2 );
  244. } ).to.throw( CKEditorError, /model-bind-to-chain-multiple-attrs/ );
  245. expect( () => {
  246. const vehicle = new Car();
  247. vehicle.bind( 'color', 'year' ).to( car1, 'color', 'year' ).to( car2, 'color', 'year' );
  248. } ).to.throw( CKEditorError, /model-bind-to-chain-multiple-attrs/ );
  249. } );
  250. it( 'should set new model attributes', () => {
  251. const car = new Car( { color: 'green', year: 2001, type: 'pickup' } );
  252. const vehicle = new Car( { 'not involved': true } );
  253. vehicle.bind( 'color', 'year', 'type' ).to( car );
  254. expect( vehicle._attributes ).to.have.keys( 'color', 'year', 'type', 'not involved' );
  255. } );
  256. it( 'should work when no attribute specified #1', () => {
  257. const vehicle = new Car();
  258. vehicle.bind( 'color' ).to( car );
  259. assertBinding( vehicle,
  260. { color: car.color, year: undefined },
  261. [
  262. [ car, { color: 'blue', year: 1969 } ]
  263. ],
  264. { color: 'blue', year: undefined }
  265. );
  266. } );
  267. it( 'should work for a single attribute', () => {
  268. const vehicle = new Car();
  269. vehicle.bind( 'color' ).to( car, 'color' );
  270. assertBinding( vehicle,
  271. { color: car.color, year: undefined },
  272. [
  273. [ car, { color: 'blue', year: 1969 } ]
  274. ],
  275. { color: 'blue', year: undefined }
  276. );
  277. } );
  278. it( 'should work for multiple attributes', () => {
  279. const vehicle = new Car();
  280. vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
  281. assertBinding( vehicle,
  282. { color: car.color, year: car.year },
  283. [
  284. [ car, { color: 'blue', year: 1969 } ]
  285. ],
  286. { color: 'blue', year: 1969 }
  287. );
  288. } );
  289. it( 'should work for attributes that don\'t exist in the model', () => {
  290. const vehicle = new Car();
  291. vehicle.bind( 'nonexistent in vehicle' ).to( car, 'color' );
  292. assertBinding( vehicle,
  293. { 'nonexistent in vehicle': car.color, color: undefined },
  294. [
  295. [ car, { color: 'blue', year: 1969 } ]
  296. ],
  297. { 'nonexistent in vehicle': 'blue', color: undefined }
  298. );
  299. } );
  300. it( 'should work when using the same attribute name more than once', () => {
  301. const vehicle = new Car();
  302. vehicle.bind( 'color', 'year' ).to( car, 'year', 'year' );
  303. assertBinding( vehicle,
  304. { color: car.year, year: car.year },
  305. [
  306. [ car, { color: 'blue', year: 1969 } ]
  307. ],
  308. { color: 1969, year: 1969 }
  309. );
  310. } );
  311. it( 'should not throw when binding more than once but no as() afterwards', () => {
  312. const vehicle = new Car();
  313. const car1 = new Car( { color: 'red' } );
  314. const car2 = new Car( { color: 'red' } );
  315. vehicle.bind( 'color' ).to( car1 ).to( car2 );
  316. assertBinding( vehicle,
  317. { color: undefined, year: undefined },
  318. [
  319. [ car, { color: 'blue', year: 1969 } ]
  320. ],
  321. { color: undefined, year: undefined }
  322. );
  323. } );
  324. describe( 'as', () => {
  325. it( 'should not chain', () => {
  326. const car1 = new Car( { year: 1999 } );
  327. const car2 = new Car( { year: 2000 } );
  328. expect(
  329. car.bind( 'year' ).to( car1, 'year' ).to( car2, 'year' ).as( () => {} )
  330. ).to.be.undefined;
  331. } );
  332. it( 'should throw when no function specified', () => {
  333. const car1 = new Car( { color: 'brown' } );
  334. const car2 = new Car( { color: 'green' } );
  335. expect( () => {
  336. car.bind( 'color' ).to( car1, 'color' ).to( car2, 'color' ).as();
  337. } ).to.throw( CKEditorError, /model-bind-as-no-callback/ );
  338. } );
  339. it( 'should throw when not a function passed', () => {
  340. const car1 = new Car( { color: 'brown' } );
  341. const car2 = new Car( { color: 'green' } );
  342. expect( () => {
  343. car.bind( 'color' ).to( car1, 'color' ).to( car2, 'color' ).as( 'not-a-function' );
  344. } ).to.throw( CKEditorError, /model-bind-as-wrong-callback/ );
  345. } );
  346. it( 'should set new model attributes', () => {
  347. const vehicle = new Car();
  348. const car1 = new Car( { type: 'pickup' } );
  349. const car2 = new Car( { type: 'truck' } );
  350. vehicle.bind( 'type' )
  351. .to( car1 )
  352. .to( car2 )
  353. .as( ( col1, col2 ) => col1 + col2 );
  354. expect( vehicle._attributes ).to.have.keys( [ 'type' ] );
  355. } );
  356. it( 'should work for a single attribute #1', () => {
  357. const vehicle = new Car();
  358. const car1 = new Car( { color: 'black' } );
  359. const car2 = new Car( { color: 'brown' } );
  360. vehicle.bind( 'color' )
  361. .to( car1 )
  362. .to( car2 )
  363. .as( ( col1, col2 ) => col1 + col2 );
  364. assertBinding( vehicle,
  365. { color: car1.color + car2.color, year: undefined },
  366. [
  367. [ car1, { color: 'black', year: 1930 } ],
  368. [ car2, { color: 'green', year: 1950 } ]
  369. ],
  370. { color: 'blackgreen', year: undefined }
  371. );
  372. } );
  373. it( 'should work for a single attribute #2', () => {
  374. const vehicle = new Car();
  375. const car1 = new Car( { color: 'black' } );
  376. const car2 = new Car( { color: 'brown' } );
  377. vehicle.bind( 'color' )
  378. .to( car1, 'color' )
  379. .to( car2, 'color' )
  380. .as( ( col1, col2 ) => col1 + col2 );
  381. assertBinding( vehicle,
  382. { color: car1.color + car2.color, year: undefined },
  383. [
  384. [ car1, { color: 'black', year: 1930 } ],
  385. [ car2, { color: 'green', year: 1950 } ]
  386. ],
  387. { color: 'blackgreen', year: undefined }
  388. );
  389. } );
  390. it( 'should work for a single attribute #3', () => {
  391. const vehicle = new Car();
  392. const car1 = new Car( { color: 'black' } );
  393. const car2 = new Car( { color: 'brown' } );
  394. const car3 = new Car( { color: 'yellow' } );
  395. vehicle.bind( 'color' )
  396. .to( car1 )
  397. .to( car2 )
  398. .to( car3 )
  399. .as( ( col1, col2, col3 ) => col1 + col2 + col3 );
  400. assertBinding( vehicle,
  401. { color: car1.color + car2.color + car3.color, year: undefined },
  402. [
  403. [ car1, { color: 'black', year: 1930 } ],
  404. [ car2, { color: 'green', year: 1950 } ]
  405. ],
  406. { color: 'blackgreenyellow', year: undefined }
  407. );
  408. } );
  409. it( 'should work for a single attribute #4', () => {
  410. const vehicle = new Car();
  411. const car1 = new Car( { color: 'black' } );
  412. const car2 = new Car( { lightness: 'bright' } );
  413. const car3 = new Car( { color: 'yellow' } );
  414. vehicle.bind( 'color' )
  415. .to( car1 )
  416. .to( car2, 'lightness' )
  417. .to( car3 )
  418. .as( ( col1, lightness, col3 ) => col1 + lightness + col3 );
  419. assertBinding( vehicle,
  420. { color: car1.color + car2.lightness + car3.color, year: undefined },
  421. [
  422. [ car1, { color: 'black', year: 1930 } ],
  423. [ car2, { color: 'green', year: 1950 } ]
  424. ],
  425. { color: 'blackbrightyellow', year: undefined }
  426. );
  427. } );
  428. } );
  429. } );
  430. } );
  431. describe( 'unbind', () => {
  432. let Model, EventInfo, CKEditorError;
  433. beforeEach( () => {
  434. Model = modules.model;
  435. EventInfo = modules.eventinfo;
  436. CKEditorError = modules.ckeditorerror;
  437. } );
  438. it( 'should throw when non-string attribute is passed', () => {
  439. expect( () => {
  440. car.unbind( new Date() );
  441. } ).to.throw( CKEditorError, /model-unbind-wrong-attrs/ );
  442. } );
  443. it( 'should remove all bindings', () => {
  444. const vehicle = new Car();
  445. vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
  446. vehicle.unbind();
  447. assertBinding( vehicle,
  448. { color: 'red', year: 2015 },
  449. [
  450. [ car, { color: 'blue', year: 1969 } ]
  451. ],
  452. { color: 'red', year: 2015 }
  453. );
  454. } );
  455. it( 'should remove bindings of certain attributes', () => {
  456. const vehicle = new Car();
  457. const car = new Car( { color: 'red', year: 2000, torque: 160 } );
  458. vehicle.bind( 'color', 'year', 'torque' ).to( car );
  459. vehicle.unbind( 'year', 'torque' );
  460. assertBinding( vehicle,
  461. { color: 'red', year: 2000, torque: 160 },
  462. [
  463. [ car, { color: 'blue', year: 1969, torque: 220 } ]
  464. ],
  465. { color: 'blue', year: 2000, torque: 160 }
  466. );
  467. } );
  468. it( 'should remove bindings of certain attributes, as()', () => {
  469. const vehicle = new Car();
  470. const car1 = new Car( { color: 'red' } );
  471. const car2 = new Car( { color: 'blue' } );
  472. vehicle.bind( 'color' ).to( car1 ).to( car2 ).as( ( c1, c2 ) => c1 + c2 );
  473. vehicle.unbind( 'color' );
  474. assertBinding( vehicle,
  475. { color: 'redblue' },
  476. [
  477. [ car1, { color: 'green' } ],
  478. [ car2, { color: 'violet' } ]
  479. ],
  480. { color: 'redblue' }
  481. );
  482. } );
  483. it( 'should process the internal structure and listeners correctly', () => {
  484. const model = new Model();
  485. const bound1 = new Model( { b1a: 'foo' } );
  486. const bound2 = new Model( { b2b: 42, 'b2c': 'bar' } );
  487. const bound3 = new Model( { b3d: 'baz' } );
  488. model.bind( 'a' ).to( bound1, 'b1a' );
  489. model.bind( 'b', 'c' ).to( bound2, 'b2b', 'b2c' );
  490. model.bind( 'd', 'e' ).to( bound3, 'b3d', 'b3d' );
  491. assertStructure( model,
  492. { a: true, b: true, c: true, d: true, e: true },
  493. [ bound1, bound2, bound3 ],
  494. [
  495. { b1a: [ 'a' ] },
  496. { b2b: [ 'b' ], b2c: [ 'c' ] },
  497. { b3d: [ 'd', 'e' ] }
  498. ]
  499. );
  500. model.unbind( 'c', 'd' );
  501. assertStructure( model,
  502. { a: true, b: true, e: true },
  503. [ bound1, bound2, bound3 ],
  504. [
  505. { b1a: [ 'a' ] },
  506. { b2b: [ 'b' ] },
  507. { b3d: [ 'e' ] }
  508. ]
  509. );
  510. model.unbind( 'b' );
  511. assertStructure( model,
  512. { a: true, e: true },
  513. [ bound1, bound3 ],
  514. [
  515. { b1a: [ 'a' ] },
  516. { b3d: [ 'e' ] }
  517. ]
  518. );
  519. model.unbind();
  520. assertStructure( model, {}, [], [] );
  521. } );
  522. } );
  523. // Syntax given that model `A` is bound to models [`B`, `C`, ...]:
  524. //
  525. // assertBinding( A,
  526. // { initial `A` attributes },
  527. // [
  528. // [ B, { new `B` attributes } ],
  529. // [ C, { new `C` attributes } ],
  530. // ...
  531. // ],
  532. // { `A` attributes after [`B`, 'C', ...] changed }
  533. // );
  534. //
  535. function assertBinding( model, stateBefore, data, stateAfter ) {
  536. let key, pair;
  537. for ( key in stateBefore ) {
  538. expect( model[ key ] ).to.be.equal( stateBefore[ key ] );
  539. }
  540. // Change attributes of bound models.
  541. for ( pair of data ) {
  542. for ( key in pair[ 1 ] ) {
  543. pair[ 0 ][ key ] = pair[ 1 ][ key ];
  544. }
  545. }
  546. for ( key in stateAfter ) {
  547. expect( model[ key ] ).to.be.equal( stateAfter[ key ] );
  548. }
  549. }
  550. function assertStructure( model, expectedBound, expectedModels, expectedBindings ) {
  551. // Check model._bound object.
  552. expect( model._bound ).to.be.deep.equal( expectedBound );
  553. // TODO: Should be boundModels = [ ...model._boundTo.boundModels() ]
  554. const boundModels = [];
  555. for ( let boundModel of model._boundTo.keys() ) {
  556. boundModels.push( boundModel );
  557. }
  558. // Check model._boundTo models.
  559. expect( boundModels ).to.have.members( expectedModels );
  560. // Check model._listeningTo models.
  561. boundModels.map( boundModel => {
  562. expect( model._listeningTo ).to.have.ownProperty( boundModel._emitterId );
  563. } );
  564. // Check model._boundTo model bindings.
  565. expectedBindings.forEach( ( binding, index ) => {
  566. expect( model._boundTo.get( expectedModels[ index ] ) )
  567. .to.have.keys( Object.keys( binding ) );
  568. Object.keys( binding ).forEach( b => {
  569. expect( Array.from( model._boundTo.get( expectedModels[ index ] )[ b ] ) )
  570. .to.have.members( binding[ b ] );
  571. } );
  572. } );
  573. }
  574. } );