8
0

model.js 24 KB

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