8
0

model.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import testUtils from '/tests/_utils/utils.js';
  7. import Model from '/ckeditor5/core/model.js';
  8. import EventInfo from '/ckeditor5/core/eventinfo.js';
  9. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  10. let Car, car;
  11. testUtils.createSinonSandbox();
  12. describe( 'Model', () => {
  13. beforeEach( 'Create a test model instance', () => {
  14. Car = class extends Model {};
  15. car = new Car( {
  16. color: 'red',
  17. year: 2015
  18. } );
  19. } );
  20. //////////
  21. it( 'should set _attributes on creation', () => {
  22. expect( car._attributes ).to.deep.equal( {
  23. color: 'red',
  24. year: 2015
  25. } );
  26. } );
  27. it( 'should get correctly after set', () => {
  28. car.color = 'blue';
  29. expect( car.color ).to.equal( 'blue' );
  30. expect( car._attributes.color ).to.equal( 'blue' );
  31. } );
  32. it( 'should get correctly after setting _attributes', () => {
  33. car._attributes.color = 'blue';
  34. expect( car.color ).to.equal( 'blue' );
  35. } );
  36. it( 'should add properties on creation', () => {
  37. let car = new Car( null, {
  38. prop: 1
  39. } );
  40. expect( car ).to.have.property( 'prop' ).to.equal( 1 );
  41. } );
  42. //////////
  43. describe( 'set', () => {
  44. it( 'should work when passing an object', () => {
  45. car.set( {
  46. color: 'blue', // Override
  47. wheels: 4,
  48. seats: 5
  49. } );
  50. expect( car._attributes ).to.deep.equal( {
  51. color: 'blue',
  52. year: 2015,
  53. wheels: 4,
  54. seats: 5
  55. } );
  56. } );
  57. it( 'should work when passing a key/value pair', () => {
  58. car.set( 'color', 'blue' );
  59. car.set( 'wheels', 4 );
  60. expect( car._attributes ).to.deep.equal( {
  61. color: 'blue',
  62. year: 2015,
  63. wheels: 4
  64. } );
  65. } );
  66. it( 'should fire the "change" event', () => {
  67. let spy = sinon.spy();
  68. let spyColor = sinon.spy();
  69. let spyYear = sinon.spy();
  70. let spyWheels = sinon.spy();
  71. car.on( 'change', spy );
  72. car.on( 'change:color', spyColor );
  73. car.on( 'change:year', spyYear );
  74. car.on( 'change:wheels', spyWheels );
  75. // Set property in all possible ways.
  76. car.color = 'blue';
  77. car.set( { year: 2003 } );
  78. car.set( 'wheels', 4 );
  79. // Check number of calls.
  80. sinon.assert.calledThrice( spy );
  81. sinon.assert.calledOnce( spyColor );
  82. sinon.assert.calledOnce( spyYear );
  83. sinon.assert.calledOnce( spyWheels );
  84. // Check context.
  85. sinon.assert.alwaysCalledOn( spy, car );
  86. sinon.assert.calledOn( spyColor, car );
  87. sinon.assert.calledOn( spyYear, car );
  88. sinon.assert.calledOn( spyWheels, car );
  89. // Check params.
  90. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'color', 'blue', 'red' );
  91. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'year', 2003, 2015 );
  92. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'wheels', 4, sinon.match.typeOf( 'undefined' ) );
  93. sinon.assert.calledWithExactly( spyColor, sinon.match.instanceOf( EventInfo ), 'blue', 'red' );
  94. sinon.assert.calledWithExactly( spyYear, sinon.match.instanceOf( EventInfo ), 2003, 2015 );
  95. sinon.assert.calledWithExactly( spyWheels, sinon.match.instanceOf( EventInfo ), 4, sinon.match.typeOf( 'undefined' ) );
  96. } );
  97. it( 'should not fire the "change" event for the same attribute value', () => {
  98. let spy = sinon.spy();
  99. let spyColor = sinon.spy();
  100. car.on( 'change', spy );
  101. car.on( 'change:color', spyColor );
  102. // Set the "color" property in all possible ways.
  103. car.color = 'red';
  104. car.set( 'color', 'red' );
  105. car.set( { color: 'red' } );
  106. sinon.assert.notCalled( spy );
  107. sinon.assert.notCalled( spyColor );
  108. } );
  109. it( 'should throw when overriding already existing property', () => {
  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. class Car extends Model {
  118. method() {}
  119. }
  120. car = new Car();
  121. expect( () => {
  122. car.set( 'method', 2 );
  123. } ).to.throw( CKEditorError, /^model-set-cannot-override/ );
  124. expect( car.method ).to.be.a( 'function' );
  125. } );
  126. } );
  127. describe( 'extend', () => {
  128. it( 'should create new Model based classes', () => {
  129. class Truck extends Car {}
  130. let truck = new Truck();
  131. expect( truck ).to.be.an.instanceof( Car );
  132. expect( truck ).to.be.an.instanceof( Model );
  133. } );
  134. } );
  135. describe( 'bind', () => {
  136. it( 'should chain for a single attribute', () => {
  137. expect( car.bind( 'color' ) ).to.contain.keys( 'to' );
  138. } );
  139. it( 'should chain for multiple attributes', () => {
  140. expect( car.bind( 'color', 'year' ) ).to.contain.keys( 'to' );
  141. } );
  142. it( 'should chain for nonexistent attributes', () => {
  143. expect( car.bind( 'nonexistent' ) ).to.contain.keys( 'to' );
  144. } );
  145. it( 'should throw when attributes are not strings', () => {
  146. expect( () => {
  147. car.bind();
  148. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  149. expect( () => {
  150. car.bind( new Date() );
  151. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  152. expect( () => {
  153. car.bind( 'color', new Date() );
  154. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  155. } );
  156. it( 'should throw when the same attribute is used than once', () => {
  157. expect( () => {
  158. car.bind( 'color', 'color' );
  159. } ).to.throw( CKEditorError, /model-bind-duplicate-attrs/ );
  160. } );
  161. it( 'should throw when binding the same attribute more than once', () => {
  162. expect( () => {
  163. car.bind( 'color' );
  164. car.bind( 'color' );
  165. } ).to.throw( CKEditorError, /model-bind-rebind/ );
  166. } );
  167. describe( 'to', () => {
  168. it( 'should chain', () => {
  169. const returned = car.bind( 'color' ).to( new Model( { color: 'red' } ) );
  170. expect( returned ).to.have.property( 'to' );
  171. expect( returned ).to.have.property( 'as' );
  172. } );
  173. it( 'should chain multiple times', () => {
  174. const returned = car.bind( 'color' )
  175. .to( new Model( { color: 'red' } ) )
  176. .to( new Model( { color: 'red' } ) )
  177. .to( new Model( { color: 'red' } ) );
  178. expect( returned ).to.have.property( 'to' );
  179. expect( returned ).to.have.property( 'as' );
  180. } );
  181. it( 'should throw when .to() model is not Model', () => {
  182. expect( () => {
  183. car.bind( 'color' ).to();
  184. } ).to.throw( CKEditorError, /model-bind-to-wrong-model/ );
  185. expect( () => {
  186. car.bind( 'year' ).to( 'it\'s not a model' );
  187. } ).to.throw( CKEditorError, /model-bind-to-wrong-model/ );
  188. } );
  189. it( 'should throw when attributes are not strings', () => {
  190. expect( () => {
  191. car.bind( 'color' ).to( new Model(), new Date() );
  192. } ).to.throw( CKEditorError, /model-bind-to-wrong-attrs/ );
  193. expect( () => {
  194. car = new Car( { color: 'red' } );
  195. car.bind( 'color' ).to( new Model(), 'color', new Date() );
  196. } ).to.throw( CKEditorError, /model-bind-to-wrong-attrs/ );
  197. } );
  198. it( 'should throw when a number of attributes does not match', () => {
  199. expect( () => {
  200. const vehicle = new Car();
  201. vehicle.bind( 'color', 'year' ).to( car, 'color' );
  202. } ).to.throw( CKEditorError, /model-bind-to-attrs-length/ );
  203. expect( () => {
  204. const vehicle = new Car();
  205. vehicle.bind( 'color' ).to( car, 'color', 'year' );
  206. } ).to.throw( CKEditorError, /model-bind-to-attrs-length/ );
  207. expect( () => {
  208. const vehicle = new Car();
  209. vehicle.bind( 'color' ).to( car, 'color' ).to( car, 'color', 'year' );
  210. } ).to.throw( CKEditorError, /model-bind-to-attrs-length/ );
  211. } );
  212. it( 'should throw when no attribute specified and those from bind() don\'t exist in to() model', () => {
  213. const vehicle = new Car();
  214. expect( () => {
  215. vehicle.bind( 'color' ).to( car, 'nonexistent in car' );
  216. } ).to.throw( CKEditorError, /model-bind-to-missing-attr/ );
  217. expect( () => {
  218. vehicle.bind( 'nonexistent in car' ).to( car );
  219. } ).to.throw( CKEditorError, /model-bind-to-missing-attr/ );
  220. } );
  221. it( 'should throw when to() more than once and multiple attributes', () => {
  222. const car1 = new Car( { color: 'red', year: 2000 } );
  223. const car2 = new Car( { color: 'red', year: 2000 } );
  224. expect( () => {
  225. const vehicle = new Car();
  226. vehicle.bind( 'color', 'year' ).to( car1 ).to( car2 );
  227. } ).to.throw( CKEditorError, /model-bind-to-chain-multiple-attrs/ );
  228. expect( () => {
  229. const vehicle = new Car();
  230. vehicle.bind( 'color', 'year' ).to( car1, 'color', 'year' ).to( car2, 'color', 'year' );
  231. } ).to.throw( CKEditorError, /model-bind-to-chain-multiple-attrs/ );
  232. } );
  233. it( 'should set new model attributes', () => {
  234. const car = new Car( { color: 'green', year: 2001, type: 'pickup' } );
  235. const vehicle = new Car( { 'not involved': true } );
  236. vehicle.bind( 'color', 'year', 'type' ).to( car );
  237. expect( vehicle._attributes ).to.have.keys( 'color', 'year', 'type', 'not involved' );
  238. } );
  239. it( 'should work when no attribute specified #1', () => {
  240. const vehicle = new Car();
  241. vehicle.bind( 'color' ).to( car );
  242. assertBinding( vehicle,
  243. { color: car.color, year: undefined },
  244. [
  245. [ car, { color: 'blue', year: 1969 } ]
  246. ],
  247. { color: 'blue', year: undefined }
  248. );
  249. } );
  250. it( 'should work for a single attribute', () => {
  251. const vehicle = new Car();
  252. vehicle.bind( 'color' ).to( car, 'color' );
  253. assertBinding( vehicle,
  254. { color: car.color, year: undefined },
  255. [
  256. [ car, { color: 'blue', year: 1969 } ]
  257. ],
  258. { color: 'blue', year: undefined }
  259. );
  260. } );
  261. it( 'should work for multiple attributes', () => {
  262. const vehicle = new Car();
  263. vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
  264. assertBinding( vehicle,
  265. { color: car.color, year: car.year },
  266. [
  267. [ car, { color: 'blue', year: 1969 } ]
  268. ],
  269. { color: 'blue', year: 1969 }
  270. );
  271. } );
  272. it( 'should work for attributes that don\'t exist in the model', () => {
  273. const vehicle = new Car();
  274. vehicle.bind( 'nonexistent in vehicle' ).to( car, 'color' );
  275. assertBinding( vehicle,
  276. { 'nonexistent in vehicle': car.color, color: undefined },
  277. [
  278. [ car, { color: 'blue', year: 1969 } ]
  279. ],
  280. { 'nonexistent in vehicle': 'blue', color: undefined }
  281. );
  282. } );
  283. it( 'should work when using the same attribute name more than once', () => {
  284. const vehicle = new Car();
  285. vehicle.bind( 'color', 'year' ).to( car, 'year', 'year' );
  286. assertBinding( vehicle,
  287. { color: car.year, year: car.year },
  288. [
  289. [ car, { color: 'blue', year: 1969 } ]
  290. ],
  291. { color: 1969, year: 1969 }
  292. );
  293. } );
  294. it( 'should not throw when binding more than once but no as() afterwards', () => {
  295. const vehicle = new Car();
  296. const car1 = new Car( { color: 'red' } );
  297. const car2 = new Car( { color: 'red' } );
  298. vehicle.bind( 'color' ).to( car1 ).to( car2 );
  299. assertBinding( vehicle,
  300. { color: undefined, year: undefined },
  301. [
  302. [ car, { color: 'blue', year: 1969 } ]
  303. ],
  304. { color: undefined, year: undefined }
  305. );
  306. } );
  307. it( 'should work when binding more that once', () => {
  308. const vehicle = new Car();
  309. vehicle.bind( 'color' ).to( car, 'color' );
  310. vehicle.bind( 'year' ).to( car, 'year' );
  311. assertBinding( vehicle,
  312. { 'color': car.color, year: car.year },
  313. [
  314. [ car, { color: 'blue', year: 1969 } ]
  315. ],
  316. { color: 'blue', year: 1969 }
  317. );
  318. } );
  319. describe( 'as', () => {
  320. it( 'should not chain', () => {
  321. const car1 = new Car( { year: 1999 } );
  322. const car2 = new Car( { year: 2000 } );
  323. expect(
  324. car.bind( 'year' ).to( car1, 'year' ).to( car2, 'year' ).as( () => {} )
  325. ).to.be.undefined;
  326. } );
  327. it( 'should throw when not a function passed', () => {
  328. const car1 = new Car( { color: 'brown' } );
  329. const car2 = new Car( { color: 'green' } );
  330. expect( () => {
  331. car.bind( 'year' ).to( car1, 'color' ).to( car2, 'color' ).as();
  332. } ).to.throw( CKEditorError, /model-bind-as-wrong-callback/ );
  333. expect( () => {
  334. car.bind( 'color' ).to( car1, 'color' ).to( car2, 'color' ).as( 'not-a-function' );
  335. } ).to.throw( CKEditorError, /model-bind-as-wrong-callback/ );
  336. } );
  337. it( 'should set new model attributes', () => {
  338. const vehicle = new Car();
  339. const car1 = new Car( { type: 'pickup' } );
  340. const car2 = new Car( { type: 'truck' } );
  341. vehicle.bind( 'type' )
  342. .to( car1 )
  343. .to( car2 )
  344. .as( ( ...args ) => args.join( '' ) );
  345. expect( vehicle._attributes ).to.have.keys( [ 'type' ] );
  346. } );
  347. it( 'should work for a single attribute #1', () => {
  348. const vehicle = new Car();
  349. const car1 = new Car( { color: 'black' } );
  350. const car2 = new Car( { color: 'brown' } );
  351. vehicle.bind( 'color' )
  352. .to( car1 )
  353. .to( car2 )
  354. .as( ( ...args ) => args.join( '' ) );
  355. assertBinding( vehicle,
  356. { color: car1.color + car2.color, year: undefined },
  357. [
  358. [ car1, { color: 'black', year: 1930 } ],
  359. [ car2, { color: 'green', year: 1950 } ]
  360. ],
  361. { color: 'blackgreen', year: undefined }
  362. );
  363. } );
  364. it( 'should work for a single attribute #2', () => {
  365. const vehicle = new Car();
  366. const car1 = new Car( { color: 'black' } );
  367. const car2 = new Car( { color: 'brown' } );
  368. vehicle.bind( 'color' )
  369. .to( car1, 'color' )
  370. .to( car2, 'color' )
  371. .as( ( ...args ) => args.join( '' ) );
  372. assertBinding( vehicle,
  373. { color: car1.color + car2.color, year: undefined },
  374. [
  375. [ car1, { color: 'black', year: 1930 } ],
  376. [ car2, { color: 'green', year: 1950 } ]
  377. ],
  378. { color: 'blackgreen', year: undefined }
  379. );
  380. } );
  381. it( 'should work for a single attribute #3', () => {
  382. const vehicle = new Car();
  383. const car1 = new Car( { color: 'black' } );
  384. const car2 = new Car( { color: 'brown' } );
  385. const car3 = new Car( { color: 'yellow' } );
  386. vehicle.bind( 'color' )
  387. .to( car1 )
  388. .to( car2 )
  389. .to( car3 )
  390. .as( ( ...args ) => args.join( '' ) );
  391. assertBinding( vehicle,
  392. { color: car1.color + car2.color + car3.color, year: undefined },
  393. [
  394. [ car1, { color: 'black', year: 1930 } ],
  395. [ car2, { color: 'green', year: 1950 } ]
  396. ],
  397. { color: 'blackgreenyellow', year: undefined }
  398. );
  399. } );
  400. it( 'should work for a single attribute #4', () => {
  401. const vehicle = new Car();
  402. const car1 = new Car( { color: 'black' } );
  403. const car2 = new Car( { lightness: 'bright' } );
  404. const car3 = new Car( { color: 'yellow' } );
  405. vehicle.bind( 'color' )
  406. .to( car1 )
  407. .to( car2, 'lightness' )
  408. .to( car3 )
  409. .as( ( ...args ) => args.join( '' ) );
  410. assertBinding( vehicle,
  411. { color: car1.color + car2.lightness + car3.color, year: undefined },
  412. [
  413. [ car1, { color: 'black', year: 1930 } ],
  414. [ car2, { color: 'green', year: 1950 } ]
  415. ],
  416. { color: 'blackbrightyellow', year: undefined }
  417. );
  418. } );
  419. it( 'should work for a single attribute #5', () => {
  420. const vehicle = new Car();
  421. const car1 = new Car( { hue: 'reds' } );
  422. const car2 = new Car( { lightness: 'bright' } );
  423. vehicle.bind( 'color' )
  424. .to( car1, 'hue' )
  425. .to( car2, 'lightness' )
  426. .as( ( ...args ) => args.join( '' ) );
  427. assertBinding( vehicle,
  428. { color: car1.hue + car2.lightness, year: undefined },
  429. [
  430. [ car1, { hue: 'greens', year: 1930 } ],
  431. [ car2, { lightness: 'dark', year: 1950 } ]
  432. ],
  433. { color: 'greensdark', year: undefined }
  434. );
  435. } );
  436. it( 'should work when binding more that once #1', () => {
  437. const vehicle = new Car();
  438. const car1 = new Car( { hue: 'reds', produced: 1920 } );
  439. const car2 = new Car( { lightness: 'bright', sold: 1921 } );
  440. vehicle.bind( 'color' )
  441. .to( car1, 'hue' )
  442. .to( car2, 'lightness' )
  443. .as( ( ...args ) => args.join( '' ) );
  444. vehicle.bind( 'year' )
  445. .to( car1, 'produced' )
  446. .to( car2, 'sold' )
  447. .as( ( ...args ) => args.join( '/' ) );
  448. assertBinding( vehicle,
  449. { color: car1.hue + car2.lightness, year: car1.produced + '/' + car2.sold },
  450. [
  451. [ car1, { hue: 'greens', produced: 1930 } ],
  452. [ car2, { lightness: 'dark', sold: 2000 } ]
  453. ],
  454. { color: 'greensdark', year: '1930/2000' }
  455. );
  456. } );
  457. it( 'should work when binding more that once #2', () => {
  458. const vehicle = new Car();
  459. const car1 = new Car( { hue: 'reds', produced: 1920 } );
  460. const car2 = new Car( { lightness: 'bright', sold: 1921 } );
  461. vehicle.bind( 'color' )
  462. .to( car1, 'hue' )
  463. .to( car2, 'lightness' )
  464. .as( ( ...args ) => args.join( '' ) );
  465. vehicle.bind( 'year' )
  466. .to( car1, 'produced' )
  467. .to( car2, 'sold' )
  468. .as( ( ...args ) => args.join( '/' ) );
  469. vehicle.bind( 'mix' )
  470. .to( car1, 'hue' )
  471. .to( car2, 'sold' )
  472. .as( ( ...args ) => args.join( '+' ) );
  473. assertBinding( vehicle,
  474. {
  475. color: car1.hue + car2.lightness,
  476. year: car1.produced + '/' + car2.sold,
  477. mix: car1.hue + '+' + car2.sold
  478. },
  479. [
  480. [ car1, { hue: 'greens', produced: 1930 } ],
  481. [ car2, { lightness: 'dark', sold: 2000 } ]
  482. ],
  483. {
  484. color: 'greensdark',
  485. year: '1930/2000',
  486. mix: 'greens+2000'
  487. }
  488. );
  489. } );
  490. it( 'should work when binding more that once #3', () => {
  491. const vehicle = new Car();
  492. const car1 = new Car( { hue: 'reds', produced: 1920 } );
  493. const car2 = new Car( { lightness: 'bright', sold: 1921 } );
  494. vehicle.bind( 'color' )
  495. .to( car1, 'hue' )
  496. .to( car2, 'lightness' )
  497. .as( ( ...args ) => args.join( '' ) );
  498. vehicle.bind( 'custom1' ).to( car1, 'hue' );
  499. vehicle.bind( 'year' )
  500. .to( car1, 'produced' )
  501. .to( car2, 'sold' )
  502. .as( ( ...args ) => args.join( '/' ) );
  503. vehicle.bind( 'custom2', 'custom3' ).to( car1, 'produced', 'hue' );
  504. assertBinding( vehicle,
  505. {
  506. color: car1.hue + car2.lightness,
  507. year: car1.produced + '/' + car2.sold,
  508. custom1: car1.hue,
  509. custom2: car1.produced,
  510. custom3: car1.hue
  511. },
  512. [
  513. [ car1, { hue: 'greens', produced: 1930 } ],
  514. [ car2, { lightness: 'dark', sold: 2000 } ]
  515. ],
  516. {
  517. color: 'greensdark',
  518. year: '1930/2000',
  519. custom1: 'greens',
  520. custom2: 1930,
  521. custom3: 'greens'
  522. }
  523. );
  524. } );
  525. } );
  526. } );
  527. } );
  528. describe( 'unbind', () => {
  529. it( 'should throw when non-string attribute is passed', () => {
  530. expect( () => {
  531. car.unbind( new Date() );
  532. } ).to.throw( CKEditorError, /model-unbind-wrong-attrs/ );
  533. } );
  534. it( 'should remove all bindings', () => {
  535. const vehicle = new Car();
  536. vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
  537. vehicle.unbind();
  538. assertBinding( vehicle,
  539. { color: 'red', year: 2015 },
  540. [
  541. [ car, { color: 'blue', year: 1969 } ]
  542. ],
  543. { color: 'red', year: 2015 }
  544. );
  545. } );
  546. it( 'should remove bindings of certain attributes', () => {
  547. const vehicle = new Car();
  548. const car = new Car( { color: 'red', year: 2000, torque: 160 } );
  549. vehicle.bind( 'color', 'year', 'torque' ).to( car );
  550. vehicle.unbind( 'year', 'torque' );
  551. assertBinding( vehicle,
  552. { color: 'red', year: 2000, torque: 160 },
  553. [
  554. [ car, { color: 'blue', year: 1969, torque: 220 } ]
  555. ],
  556. { color: 'blue', year: 2000, torque: 160 }
  557. );
  558. } );
  559. it( 'should remove bindings of certain attributes, as()', () => {
  560. const vehicle = new Car();
  561. const car1 = new Car( { color: 'red' } );
  562. const car2 = new Car( { color: 'blue' } );
  563. vehicle.bind( 'color' ).to( car1 ).to( car2 ).as( ( c1, c2 ) => c1 + c2 );
  564. vehicle.unbind( 'color' );
  565. assertBinding( vehicle,
  566. { color: 'redblue' },
  567. [
  568. [ car1, { color: 'green' } ],
  569. [ car2, { color: 'violet' } ]
  570. ],
  571. { color: 'redblue' }
  572. );
  573. } );
  574. it( 'should process the internal structure and listeners correctly', () => {
  575. const model = new Model();
  576. const bound1 = new Model( { b1a: 'foo' } );
  577. const bound2 = new Model( { b2b: 42, 'b2c': 'bar' } );
  578. const bound3 = new Model( { b3d: 'baz' } );
  579. model.bind( 'a' ).to( bound1, 'b1a' );
  580. model.bind( 'b', 'c' ).to( bound2, 'b2b', 'b2c' );
  581. model.bind( 'd', 'e' ).to( bound3, 'b3d', 'b3d' );
  582. assertStructure( model,
  583. [ 'a', 'b', 'c', 'd', 'e' ],
  584. [ bound1, bound2, bound3 ],
  585. [
  586. { b1a: [ 'a' ] },
  587. { b2b: [ 'b' ], b2c: [ 'c' ] },
  588. { b3d: [ 'd', 'e' ] }
  589. ]
  590. );
  591. model.unbind( 'c', 'd' );
  592. assertStructure( model,
  593. [ 'a', 'b', 'e' ],
  594. [ bound1, bound2, bound3 ],
  595. [
  596. { b1a: [ 'a' ] },
  597. { b2b: [ 'b' ] },
  598. { b3d: [ 'e' ] }
  599. ]
  600. );
  601. model.unbind( 'b' );
  602. assertStructure( model,
  603. [ 'a', 'e' ],
  604. [ bound1, bound3 ],
  605. [
  606. { b1a: [ 'a' ] },
  607. { b3d: [ 'e' ] }
  608. ]
  609. );
  610. model.unbind();
  611. assertStructure( model, [], [], [] );
  612. } );
  613. } );
  614. // Syntax given that model `A` is bound to models [`B`, `C`, ...]:
  615. //
  616. // assertBinding( A,
  617. // { initial `A` attributes },
  618. // [
  619. // [ B, { new `B` attributes } ],
  620. // [ C, { new `C` attributes } ],
  621. // ...
  622. // ],
  623. // { `A` attributes after [`B`, 'C', ...] changed }
  624. // );
  625. //
  626. function assertBinding( model, stateBefore, data, stateAfter ) {
  627. let key, pair;
  628. for ( key in stateBefore ) {
  629. expect( model[ key ] ).to.be.equal( stateBefore[ key ] );
  630. }
  631. // Change attributes of bound models.
  632. for ( pair of data ) {
  633. for ( key in pair[ 1 ] ) {
  634. pair[ 0 ][ key ] = pair[ 1 ][ key ];
  635. }
  636. }
  637. for ( key in stateAfter ) {
  638. expect( model[ key ] ).to.be.equal( stateAfter[ key ] );
  639. }
  640. }
  641. function assertStructure( model, expectedBoundAttributes, expectedBoundModels, expectedBindings ) {
  642. const boundModels = [ ...model._boundModels.keys() ];
  643. // Check model._boundAttributes object.
  644. if ( expectedBoundAttributes.length ) {
  645. expect( model._boundAttributes ).to.have.keys( expectedBoundAttributes );
  646. } else {
  647. expect( model._boundAttributes ).to.be.empty;
  648. }
  649. // Check model._boundModels models.
  650. expect( boundModels ).to.have.members( expectedBoundModels );
  651. // Check model._listeningTo models.
  652. boundModels.map( boundModel => {
  653. expect( model._listeningTo ).to.have.ownProperty( boundModel._emitterId );
  654. } );
  655. // Check model._boundModels bindings.
  656. expectedBindings.forEach( ( binding, index ) => {
  657. const bindingKeys = Object.keys( binding );
  658. expect( model._boundModels.get( expectedBoundModels[ index ] ) ).to.have.keys( bindingKeys );
  659. bindingKeys.forEach( key => {
  660. const entries = [ ...model._boundModels.get( expectedBoundModels[ index ] )[ key ] ];
  661. expect( entries.map( e => e.attr ) ).to.have.members( binding[ key ] );
  662. } );
  663. } );
  664. }
  665. } );