8
0

model.js 23 KB

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