8
0

model.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. it( 'should allow setting attributes with undefined value', () => {
  130. let spy = sinon.spy();
  131. car.on( 'change', spy );
  132. car.set( 'seats', undefined );
  133. sinon.assert.calledOnce( spy );
  134. expect( car._attributes ).to.contain.keys( 'seats' );
  135. expect( car.seats ).to.be.undefined;
  136. car.set( 'seats', 5 );
  137. sinon.assert.calledTwice( spy );
  138. expect( car._attributes ).to.have.property( 'seats', 5 );
  139. } );
  140. } );
  141. describe( 'extend', () => {
  142. it( 'should create new Model based classes', () => {
  143. class Truck extends Car {}
  144. let truck = new Truck();
  145. expect( truck ).to.be.an.instanceof( Car );
  146. expect( truck ).to.be.an.instanceof( Model );
  147. } );
  148. } );
  149. describe( 'bind', () => {
  150. it( 'should chain for a single attribute', () => {
  151. expect( car.bind( 'color' ) ).to.contain.keys( 'to' );
  152. } );
  153. it( 'should chain for multiple attributes', () => {
  154. expect( car.bind( 'color', 'year' ) ).to.contain.keys( 'to' );
  155. } );
  156. it( 'should chain for nonexistent attributes', () => {
  157. expect( car.bind( 'nonexistent' ) ).to.contain.keys( 'to' );
  158. } );
  159. it( 'should throw when attributes are not strings', () => {
  160. expect( () => {
  161. car.bind();
  162. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  163. expect( () => {
  164. car.bind( new Date() );
  165. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  166. expect( () => {
  167. car.bind( 'color', new Date() );
  168. } ).to.throw( CKEditorError, /model-bind-wrong-attrs/ );
  169. } );
  170. it( 'should throw when the same attribute is used than once', () => {
  171. expect( () => {
  172. car.bind( 'color', 'color' );
  173. } ).to.throw( CKEditorError, /model-bind-duplicate-attrs/ );
  174. } );
  175. it( 'should throw when binding the same attribute more than once', () => {
  176. expect( () => {
  177. car.bind( 'color' );
  178. car.bind( 'color' );
  179. } ).to.throw( CKEditorError, /model-bind-rebind/ );
  180. } );
  181. describe( 'to', () => {
  182. it( 'should chain', () => {
  183. const returned = car.bind( 'color' ).to( new Model( { color: 'red' } ) );
  184. expect( returned ).to.have.property( 'to' );
  185. expect( returned ).to.have.property( 'as' );
  186. } );
  187. it( 'should chain multiple times', () => {
  188. const returned = car.bind( 'color' )
  189. .to( new Model( { color: 'red' } ) )
  190. .to( new Model( { color: 'red' } ) )
  191. .to( new Model( { color: 'red' } ) );
  192. expect( returned ).to.have.property( 'to' );
  193. expect( returned ).to.have.property( 'as' );
  194. } );
  195. it( 'should throw when .to() model is not Model', () => {
  196. expect( () => {
  197. car.bind( 'color' ).to();
  198. } ).to.throw( CKEditorError, /model-bind-to-wrong-model/ );
  199. expect( () => {
  200. car.bind( 'year' ).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 no attribute specified and those from bind() don\'t exist in to() 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-attr/ );
  231. expect( () => {
  232. vehicle.bind( 'nonexistent in car' ).to( car );
  233. } ).to.throw( CKEditorError, /model-bind-to-missing-attr/ );
  234. } );
  235. it( 'should throw when to() more than once and multiple attributes', () => {
  236. const car1 = new Car( { color: 'red', year: 2000 } );
  237. const car2 = new Car( { color: 'red', year: 2000 } );
  238. expect( () => {
  239. const vehicle = new Car();
  240. vehicle.bind( 'color', 'year' ).to( car1 ).to( car2 );
  241. } ).to.throw( CKEditorError, /model-bind-to-chain-multiple-attrs/ );
  242. expect( () => {
  243. const vehicle = new Car();
  244. vehicle.bind( 'color', 'year' ).to( car1, 'color', 'year' ).to( car2, 'color', 'year' );
  245. } ).to.throw( CKEditorError, /model-bind-to-chain-multiple-attrs/ );
  246. } );
  247. it( 'should set new model attributes', () => {
  248. const car = new Car( { color: 'green', year: 2001, type: 'pickup' } );
  249. const vehicle = new Car( { 'not involved': true } );
  250. vehicle.bind( 'color', 'year', 'type' ).to( car );
  251. expect( vehicle._attributes ).to.have.keys( 'color', 'year', 'type', 'not involved' );
  252. } );
  253. it( 'should work when no attribute specified #1', () => {
  254. const vehicle = new Car();
  255. vehicle.bind( 'color' ).to( car );
  256. assertBinding( vehicle,
  257. { color: car.color, year: undefined },
  258. [
  259. [ car, { color: 'blue', year: 1969 } ]
  260. ],
  261. { color: 'blue', year: undefined }
  262. );
  263. } );
  264. it( 'should work for a single attribute', () => {
  265. const vehicle = new Car();
  266. vehicle.bind( 'color' ).to( car, 'color' );
  267. assertBinding( vehicle,
  268. { color: car.color, year: undefined },
  269. [
  270. [ car, { color: 'blue', year: 1969 } ]
  271. ],
  272. { color: 'blue', year: undefined }
  273. );
  274. } );
  275. it( 'should work for multiple attributes', () => {
  276. const vehicle = new Car();
  277. vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
  278. assertBinding( vehicle,
  279. { color: car.color, year: car.year },
  280. [
  281. [ car, { color: 'blue', year: 1969 } ]
  282. ],
  283. { color: 'blue', year: 1969 }
  284. );
  285. } );
  286. it( 'should work for attributes that don\'t exist in the model', () => {
  287. const vehicle = new Car();
  288. vehicle.bind( 'nonexistent in vehicle' ).to( car, 'color' );
  289. assertBinding( vehicle,
  290. { 'nonexistent in vehicle': car.color, color: undefined },
  291. [
  292. [ car, { color: 'blue', year: 1969 } ]
  293. ],
  294. { 'nonexistent in vehicle': 'blue', color: undefined }
  295. );
  296. } );
  297. it( 'should work when using the same attribute name more than once', () => {
  298. const vehicle = new Car();
  299. vehicle.bind( 'color', 'year' ).to( car, 'year', 'year' );
  300. assertBinding( vehicle,
  301. { color: car.year, year: car.year },
  302. [
  303. [ car, { color: 'blue', year: 1969 } ]
  304. ],
  305. { color: 1969, year: 1969 }
  306. );
  307. } );
  308. it( 'should not throw when binding more than once but no as() afterwards', () => {
  309. const vehicle = new Car();
  310. const car1 = new Car( { color: 'red' } );
  311. const car2 = new Car( { color: 'red' } );
  312. vehicle.bind( 'color' ).to( car1 ).to( car2 );
  313. assertBinding( vehicle,
  314. { color: undefined, year: undefined },
  315. [
  316. [ car, { color: 'blue', year: 1969 } ]
  317. ],
  318. { color: undefined, year: undefined }
  319. );
  320. } );
  321. it( 'should work when binding more that once', () => {
  322. const vehicle = new Car();
  323. vehicle.bind( 'color' ).to( car, 'color' );
  324. vehicle.bind( 'year' ).to( car, 'year' );
  325. assertBinding( vehicle,
  326. { 'color': car.color, year: car.year },
  327. [
  328. [ car, { color: 'blue', year: 1969 } ]
  329. ],
  330. { color: 'blue', year: 1969 }
  331. );
  332. } );
  333. describe( 'as', () => {
  334. it( 'should not chain', () => {
  335. const car1 = new Car( { year: 1999 } );
  336. const car2 = new Car( { year: 2000 } );
  337. expect(
  338. car.bind( 'year' ).to( car1, 'year' ).to( car2, 'year' ).as( () => {} )
  339. ).to.be.undefined;
  340. } );
  341. it( 'should throw when not a function passed', () => {
  342. const car1 = new Car( { color: 'brown' } );
  343. const car2 = new Car( { color: 'green' } );
  344. expect( () => {
  345. car.bind( 'year' ).to( car1, 'color' ).to( car2, 'color' ).as();
  346. } ).to.throw( CKEditorError, /model-bind-as-wrong-callback/ );
  347. expect( () => {
  348. car.bind( 'color' ).to( car1, 'color' ).to( car2, 'color' ).as( 'not-a-function' );
  349. } ).to.throw( CKEditorError, /model-bind-as-wrong-callback/ );
  350. } );
  351. it( 'should set new model attributes', () => {
  352. const vehicle = new Car();
  353. const car1 = new Car( { type: 'pickup' } );
  354. const car2 = new Car( { type: 'truck' } );
  355. vehicle.bind( 'type' )
  356. .to( car1 )
  357. .to( car2 )
  358. .as( ( ...args ) => args.join( '' ) );
  359. expect( vehicle._attributes ).to.have.keys( [ 'type' ] );
  360. } );
  361. it( 'should work for a single attribute #1', () => {
  362. const vehicle = new Car();
  363. const car1 = new Car( { color: 'black' } );
  364. const car2 = new Car( { color: 'brown' } );
  365. vehicle.bind( 'color' )
  366. .to( car1 )
  367. .to( car2 )
  368. .as( ( ...args ) => args.join( '' ) );
  369. assertBinding( vehicle,
  370. { color: car1.color + car2.color, year: undefined },
  371. [
  372. [ car1, { color: 'black', year: 1930 } ],
  373. [ car2, { color: 'green', year: 1950 } ]
  374. ],
  375. { color: 'blackgreen', year: undefined }
  376. );
  377. } );
  378. it( 'should work for a single attribute #2', () => {
  379. const vehicle = new Car();
  380. const car1 = new Car( { color: 'black' } );
  381. const car2 = new Car( { color: 'brown' } );
  382. vehicle.bind( 'color' )
  383. .to( car1, 'color' )
  384. .to( car2, 'color' )
  385. .as( ( ...args ) => args.join( '' ) );
  386. assertBinding( vehicle,
  387. { color: car1.color + car2.color, year: undefined },
  388. [
  389. [ car1, { color: 'black', year: 1930 } ],
  390. [ car2, { color: 'green', year: 1950 } ]
  391. ],
  392. { color: 'blackgreen', year: undefined }
  393. );
  394. } );
  395. it( 'should work for a single attribute #3', () => {
  396. const vehicle = new Car();
  397. const car1 = new Car( { color: 'black' } );
  398. const car2 = new Car( { color: 'brown' } );
  399. const car3 = new Car( { color: 'yellow' } );
  400. vehicle.bind( 'color' )
  401. .to( car1 )
  402. .to( car2 )
  403. .to( car3 )
  404. .as( ( ...args ) => args.join( '' ) );
  405. assertBinding( vehicle,
  406. { color: car1.color + car2.color + car3.color, year: undefined },
  407. [
  408. [ car1, { color: 'black', year: 1930 } ],
  409. [ car2, { color: 'green', year: 1950 } ]
  410. ],
  411. { color: 'blackgreenyellow', year: undefined }
  412. );
  413. } );
  414. it( 'should work for a single attribute #4', () => {
  415. const vehicle = new Car();
  416. const car1 = new Car( { color: 'black' } );
  417. const car2 = new Car( { lightness: 'bright' } );
  418. const car3 = new Car( { color: 'yellow' } );
  419. vehicle.bind( 'color' )
  420. .to( car1 )
  421. .to( car2, 'lightness' )
  422. .to( car3 )
  423. .as( ( ...args ) => args.join( '' ) );
  424. assertBinding( vehicle,
  425. { color: car1.color + car2.lightness + car3.color, year: undefined },
  426. [
  427. [ car1, { color: 'black', year: 1930 } ],
  428. [ car2, { color: 'green', year: 1950 } ]
  429. ],
  430. { color: 'blackbrightyellow', year: undefined }
  431. );
  432. } );
  433. it( 'should work for a single attribute #5', () => {
  434. const vehicle = new Car();
  435. const car1 = new Car( { hue: 'reds' } );
  436. const car2 = new Car( { lightness: 'bright' } );
  437. vehicle.bind( 'color' )
  438. .to( car1, 'hue' )
  439. .to( car2, 'lightness' )
  440. .as( ( ...args ) => args.join( '' ) );
  441. assertBinding( vehicle,
  442. { color: car1.hue + car2.lightness, year: undefined },
  443. [
  444. [ car1, { hue: 'greens', year: 1930 } ],
  445. [ car2, { lightness: 'dark', year: 1950 } ]
  446. ],
  447. { color: 'greensdark', year: undefined }
  448. );
  449. } );
  450. it( 'should work when binding more that once #1', () => {
  451. const vehicle = new Car();
  452. const car1 = new Car( { hue: 'reds', produced: 1920 } );
  453. const car2 = new Car( { lightness: 'bright', sold: 1921 } );
  454. vehicle.bind( 'color' )
  455. .to( car1, 'hue' )
  456. .to( car2, 'lightness' )
  457. .as( ( ...args ) => args.join( '' ) );
  458. vehicle.bind( 'year' )
  459. .to( car1, 'produced' )
  460. .to( car2, 'sold' )
  461. .as( ( ...args ) => args.join( '/' ) );
  462. assertBinding( vehicle,
  463. { color: car1.hue + car2.lightness, year: car1.produced + '/' + car2.sold },
  464. [
  465. [ car1, { hue: 'greens', produced: 1930 } ],
  466. [ car2, { lightness: 'dark', sold: 2000 } ]
  467. ],
  468. { color: 'greensdark', year: '1930/2000' }
  469. );
  470. } );
  471. it( 'should work when binding more that once #2', () => {
  472. const vehicle = new Car();
  473. const car1 = new Car( { hue: 'reds', produced: 1920 } );
  474. const car2 = new Car( { lightness: 'bright', sold: 1921 } );
  475. vehicle.bind( 'color' )
  476. .to( car1, 'hue' )
  477. .to( car2, 'lightness' )
  478. .as( ( ...args ) => args.join( '' ) );
  479. vehicle.bind( 'year' )
  480. .to( car1, 'produced' )
  481. .to( car2, 'sold' )
  482. .as( ( ...args ) => args.join( '/' ) );
  483. vehicle.bind( 'mix' )
  484. .to( car1, 'hue' )
  485. .to( car2, 'sold' )
  486. .as( ( ...args ) => args.join( '+' ) );
  487. assertBinding( vehicle,
  488. {
  489. color: car1.hue + car2.lightness,
  490. year: car1.produced + '/' + car2.sold,
  491. mix: car1.hue + '+' + car2.sold
  492. },
  493. [
  494. [ car1, { hue: 'greens', produced: 1930 } ],
  495. [ car2, { lightness: 'dark', sold: 2000 } ]
  496. ],
  497. {
  498. color: 'greensdark',
  499. year: '1930/2000',
  500. mix: 'greens+2000'
  501. }
  502. );
  503. } );
  504. it( 'should work when binding more that once #3', () => {
  505. const vehicle = new Car();
  506. const car1 = new Car( { hue: 'reds', produced: 1920 } );
  507. const car2 = new Car( { lightness: 'bright', sold: 1921 } );
  508. vehicle.bind( 'color' )
  509. .to( car1, 'hue' )
  510. .to( car2, 'lightness' )
  511. .as( ( ...args ) => args.join( '' ) );
  512. vehicle.bind( 'custom1' ).to( car1, 'hue' );
  513. vehicle.bind( 'year' )
  514. .to( car1, 'produced' )
  515. .to( car2, 'sold' )
  516. .as( ( ...args ) => args.join( '/' ) );
  517. vehicle.bind( 'custom2', 'custom3' ).to( car1, 'produced', 'hue' );
  518. assertBinding( vehicle,
  519. {
  520. color: car1.hue + car2.lightness,
  521. year: car1.produced + '/' + car2.sold,
  522. custom1: car1.hue,
  523. custom2: car1.produced,
  524. custom3: car1.hue
  525. },
  526. [
  527. [ car1, { hue: 'greens', produced: 1930 } ],
  528. [ car2, { lightness: 'dark', sold: 2000 } ]
  529. ],
  530. {
  531. color: 'greensdark',
  532. year: '1930/2000',
  533. custom1: 'greens',
  534. custom2: 1930,
  535. custom3: 'greens'
  536. }
  537. );
  538. } );
  539. } );
  540. } );
  541. } );
  542. describe( 'unbind', () => {
  543. it( 'should throw when non-string attribute is passed', () => {
  544. expect( () => {
  545. car.unbind( new Date() );
  546. } ).to.throw( CKEditorError, /model-unbind-wrong-attrs/ );
  547. } );
  548. it( 'should remove all bindings', () => {
  549. const vehicle = new Car();
  550. vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
  551. vehicle.unbind();
  552. assertBinding( vehicle,
  553. { color: 'red', year: 2015 },
  554. [
  555. [ car, { color: 'blue', year: 1969 } ]
  556. ],
  557. { color: 'red', year: 2015 }
  558. );
  559. } );
  560. it( 'should remove bindings of certain attributes', () => {
  561. const vehicle = new Car();
  562. const car = new Car( { color: 'red', year: 2000, torque: 160 } );
  563. vehicle.bind( 'color', 'year', 'torque' ).to( car );
  564. vehicle.unbind( 'year', 'torque' );
  565. assertBinding( vehicle,
  566. { color: 'red', year: 2000, torque: 160 },
  567. [
  568. [ car, { color: 'blue', year: 1969, torque: 220 } ]
  569. ],
  570. { color: 'blue', year: 2000, torque: 160 }
  571. );
  572. } );
  573. it( 'should remove bindings of certain attributes, as()', () => {
  574. const vehicle = new Car();
  575. const car1 = new Car( { color: 'red' } );
  576. const car2 = new Car( { color: 'blue' } );
  577. vehicle.bind( 'color' ).to( car1 ).to( car2 ).as( ( c1, c2 ) => c1 + c2 );
  578. vehicle.unbind( 'color' );
  579. assertBinding( vehicle,
  580. { color: 'redblue' },
  581. [
  582. [ car1, { color: 'green' } ],
  583. [ car2, { color: 'violet' } ]
  584. ],
  585. { color: 'redblue' }
  586. );
  587. } );
  588. it( 'should process the internal structure and listeners correctly', () => {
  589. const model = new Model();
  590. const bound1 = new Model( { b1a: 'foo' } );
  591. const bound2 = new Model( { b2b: 42, 'b2c': 'bar' } );
  592. const bound3 = new Model( { b3d: 'baz' } );
  593. model.bind( 'a' ).to( bound1, 'b1a' );
  594. model.bind( 'b', 'c' ).to( bound2, 'b2b', 'b2c' );
  595. model.bind( 'd', 'e' ).to( bound3, 'b3d', 'b3d' );
  596. assertStructure( model,
  597. [ 'a', 'b', 'c', 'd', 'e' ],
  598. [ bound1, bound2, bound3 ],
  599. [
  600. { b1a: [ 'a' ] },
  601. { b2b: [ 'b' ], b2c: [ 'c' ] },
  602. { b3d: [ 'd', 'e' ] }
  603. ]
  604. );
  605. model.unbind( 'c', 'd' );
  606. assertStructure( model,
  607. [ 'a', 'b', 'e' ],
  608. [ bound1, bound2, bound3 ],
  609. [
  610. { b1a: [ 'a' ] },
  611. { b2b: [ 'b' ] },
  612. { b3d: [ 'e' ] }
  613. ]
  614. );
  615. model.unbind( 'b' );
  616. assertStructure( model,
  617. [ 'a', 'e' ],
  618. [ bound1, bound3 ],
  619. [
  620. { b1a: [ 'a' ] },
  621. { b3d: [ 'e' ] }
  622. ]
  623. );
  624. model.unbind();
  625. assertStructure( model, [], [], [] );
  626. } );
  627. } );
  628. // Syntax given that model `A` is bound to models [`B`, `C`, ...]:
  629. //
  630. // assertBinding( A,
  631. // { initial `A` attributes },
  632. // [
  633. // [ B, { new `B` attributes } ],
  634. // [ C, { new `C` attributes } ],
  635. // ...
  636. // ],
  637. // { `A` attributes after [`B`, 'C', ...] changed }
  638. // );
  639. //
  640. function assertBinding( model, stateBefore, data, stateAfter ) {
  641. let key, pair;
  642. for ( key in stateBefore ) {
  643. expect( model[ key ] ).to.be.equal( stateBefore[ key ] );
  644. }
  645. // Change attributes of bound models.
  646. for ( pair of data ) {
  647. for ( key in pair[ 1 ] ) {
  648. pair[ 0 ][ key ] = pair[ 1 ][ key ];
  649. }
  650. }
  651. for ( key in stateAfter ) {
  652. expect( model[ key ] ).to.be.equal( stateAfter[ key ] );
  653. }
  654. }
  655. function assertStructure( model, expectedBoundAttributes, expectedBoundModels, expectedBindings ) {
  656. const boundModels = [ ...model._boundModels.keys() ];
  657. // Check model._boundAttributes object.
  658. if ( expectedBoundAttributes.length ) {
  659. expect( model._boundAttributes ).to.have.keys( expectedBoundAttributes );
  660. } else {
  661. expect( model._boundAttributes ).to.be.empty;
  662. }
  663. // Check model._boundModels models.
  664. expect( boundModels ).to.have.members( expectedBoundModels );
  665. // Check model._listeningTo models.
  666. boundModels.map( boundModel => {
  667. expect( model._listeningTo ).to.have.ownProperty( boundModel._emitterId );
  668. } );
  669. // Check model._boundModels bindings.
  670. expectedBindings.forEach( ( binding, index ) => {
  671. const bindingKeys = Object.keys( binding );
  672. expect( model._boundModels.get( expectedBoundModels[ index ] ) ).to.have.keys( bindingKeys );
  673. bindingKeys.forEach( key => {
  674. const entries = [ ...model._boundModels.get( expectedBoundModels[ index ] )[ key ] ];
  675. expect( entries.map( e => e.attr ) ).to.have.members( binding[ key ] );
  676. } );
  677. } );
  678. }
  679. } );