controllercollection.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: ui */
  7. import testUtils from '/tests/core/_utils/utils.js';
  8. import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
  9. import Controller from '/ckeditor5/ui/controller.js';
  10. import Collection from '/ckeditor5/utils/collection.js';
  11. import Model from '/ckeditor5/ui/model.js';
  12. import View from '/ckeditor5/ui/view.js';
  13. import Template from '/ckeditor5/ui/template.js';
  14. testUtils.createSinonSandbox();
  15. let ParentView, ItemController, ItemView;
  16. let models;
  17. describe( 'ControllerCollection', () => {
  18. beforeEach( () => {
  19. defineParentViewClass();
  20. defineItemControllerClass();
  21. defineItemViewClass();
  22. createModelCollection();
  23. } );
  24. describe( 'constructor', () => {
  25. it( 'should throw when no name is passed', () => {
  26. expect( () => {
  27. new ControllerCollection();
  28. } ).to.throw( /^ui-controllercollection-no-name/ );
  29. } );
  30. it( 'accepts locale', () => {
  31. const locale = {};
  32. const collection = new ControllerCollection( 'foo', locale );
  33. expect( collection.locale ).to.equal( locale );
  34. } );
  35. } );
  36. describe( 'add', () => {
  37. it( 'should add a child controller and return promise', () => {
  38. const parentController = new Controller();
  39. const childController = new Controller();
  40. const collection = parentController.addCollection( 'x' );
  41. const returned = collection.add( childController );
  42. expect( returned ).to.be.an.instanceof( Promise );
  43. expect( collection.get( 0 ) ).to.be.equal( childController );
  44. } );
  45. it( 'should add a child controller at given position', () => {
  46. const parentController = new Controller();
  47. const childController1 = new Controller();
  48. const childController2 = new Controller();
  49. const collection = parentController.addCollection( 'x' );
  50. collection.add( childController1 );
  51. collection.add( childController2, 0 );
  52. expect( collection.get( 0 ) ).to.be.equal( childController2 );
  53. expect( collection.get( 1 ) ).to.be.equal( childController1 );
  54. } );
  55. it( 'should initialize child controller if parent is ready', () => {
  56. const parentController = new Controller( null, new ParentView() );
  57. const childController = new Controller( null, new View() );
  58. const spy = testUtils.sinon.spy( childController, 'init' );
  59. const collection = parentController.addCollection( 'x' );
  60. collection.add( childController );
  61. collection.remove( childController );
  62. sinon.assert.notCalled( spy );
  63. return parentController.init()
  64. .then( () => {
  65. return collection.add( childController );
  66. } )
  67. .then( () => {
  68. sinon.assert.calledOnce( spy );
  69. } );
  70. } );
  71. it( 'should not initialize child controller twice', () => {
  72. const parentController = new Controller( null, new ParentView() );
  73. const childController = new Controller( null, new View() );
  74. const spy = testUtils.sinon.spy( childController, 'init' );
  75. const collection = parentController.addCollection( 'x' );
  76. return parentController.init()
  77. .then( () => {
  78. return childController.init();
  79. } )
  80. .then( () => {
  81. return collection.add( childController );
  82. } )
  83. .then( () => {
  84. sinon.assert.calledOnce( spy );
  85. } );
  86. } );
  87. } );
  88. describe( 'bind', () => {
  89. it( 'returns object', () => {
  90. expect( new ControllerCollection( 'foo' ).bind( {} ) ).to.be.an( 'object' );
  91. } );
  92. it( 'provides "as" interface', () => {
  93. const bind = new ControllerCollection( 'foo' ).bind( {} );
  94. expect( bind ).to.have.keys( 'as' );
  95. expect( bind.as ).to.be.a( 'function' );
  96. } );
  97. describe( 'as', () => {
  98. it( 'does not chain', () => {
  99. const controllers = new ControllerCollection( 'synced' );
  100. const returned = controllers.bind( models ).as( ItemController, ItemView );
  101. expect( returned ).to.be.undefined;
  102. } );
  103. describe( 'standard factory', () => {
  104. it( 'expands the initial collection of the models', () => {
  105. const controllers = new ControllerCollection( 'synced' );
  106. controllers.bind( models ).as( ItemController, ItemView );
  107. expect( controllers ).to.have.length( 5 );
  108. expect( controllers.get( 0 ).model.uid ).to.equal( '0' );
  109. expect( controllers.get( 4 ).model.uid ).to.equal( '4' );
  110. } );
  111. it( 'uses the controller and view classes to expand the collection', () => {
  112. const controllers = new ControllerCollection( 'synced' );
  113. controllers.bind( models ).as( ItemController, ItemView );
  114. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  115. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  116. } );
  117. it( 'supports adding new models to the collection', () => {
  118. const controllers = new ControllerCollection( 'synced' );
  119. controllers.bind( models ).as( ItemController, ItemView );
  120. models.add( new Model( { uid: '6' } ) );
  121. models.add( new Model( { uid: '5' } ), 5 );
  122. expect( controllers.get( 5 ).model.uid ).to.equal( '5' );
  123. expect( controllers.get( 6 ).model.uid ).to.equal( '6' );
  124. expect( controllers ).to.have.length( 7 );
  125. } );
  126. it( 'supports removing models from the collection', () => {
  127. const controllers = new ControllerCollection( 'synced' );
  128. controllers.bind( models ).as( ItemController, ItemView );
  129. models.remove( 2 );
  130. models.remove( 3 );
  131. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3' ] );
  132. } );
  133. it( 'passes controller collection\'s locale to the views', () => {
  134. const locale = {};
  135. const controllers = new ControllerCollection( 'synced', locale );
  136. controllers.bind( models ).as( ItemController, ItemView );
  137. expect( controllers.get( 0 ).view.locale ).to.equal( locale );
  138. } );
  139. } );
  140. describe( 'custom factory', () => {
  141. it( 'expands the initial collection of the models', () => {
  142. const controllers = new ControllerCollection( 'synced' );
  143. controllers.bind( models ).as( ( model, locale ) => {
  144. return new ItemController( model, new ItemView( locale ) );
  145. } );
  146. expect( controllers ).to.have.length( 5 );
  147. expect( controllers.get( 0 ).model.uid ).to.equal( '0' );
  148. expect( controllers.get( 4 ).model.uid ).to.equal( '4' );
  149. } );
  150. it( 'uses the controller and view classes to expand the collection', () => {
  151. const controllers = new ControllerCollection( 'synced' );
  152. controllers.bind( models ).as( ( model, locale ) => {
  153. return new ItemController( model, new ItemView( locale ) );
  154. } );
  155. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  156. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  157. } );
  158. it( 'supports adding new models to the collection', () => {
  159. const controllers = new ControllerCollection( 'synced' );
  160. controllers.bind( models ).as( ( model, locale ) => {
  161. return new ItemController( model, new ItemView( locale ) );
  162. } );
  163. models.add( new Model( { uid: '6' } ) );
  164. models.add( new Model( { uid: '5' } ), 5 );
  165. expect( controllers.get( 5 ).model.uid ).to.equal( '5' );
  166. expect( controllers.get( 6 ).model.uid ).to.equal( '6' );
  167. expect( controllers ).to.have.length( 7 );
  168. } );
  169. it( 'supports removing models from the collection', () => {
  170. const controllers = new ControllerCollection( 'synced' );
  171. controllers.bind( models ).as( ( model, locale ) => {
  172. return new ItemController( model, new ItemView( locale ) );
  173. } );
  174. models.remove( 2 );
  175. models.remove( 3 );
  176. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3' ] );
  177. } );
  178. it( 'passes controller collection\'s locale to the views', () => {
  179. const locale = {};
  180. const controllers = new ControllerCollection( 'synced', locale );
  181. controllers.bind( models ).as( ( model, locale ) => {
  182. return new ItemController( model, new ItemView( locale ) );
  183. } );
  184. expect( controllers.get( 0 ).view.locale ).to.equal( locale );
  185. } );
  186. } );
  187. describe( 'custom data format with custom factory', () => {
  188. it( 'expands the initial collection of the models', () => {
  189. const controllers = new ControllerCollection( 'synced' );
  190. const data = new Collection();
  191. data.add( { foo: 'a' } );
  192. data.add( { foo: 'b' } );
  193. controllers.bind( data ).as( ( item, locale ) => {
  194. const model = new Model( {
  195. custom: item.foo
  196. } );
  197. return new ItemController( model, new ItemView( locale ) );
  198. } );
  199. expect( controllers ).to.have.length( 2 );
  200. expect( controllers.get( 0 ).model.custom ).to.equal( 'a' );
  201. expect( controllers.get( 1 ).model.custom ).to.equal( 'b' );
  202. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  203. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  204. } );
  205. } );
  206. } );
  207. } );
  208. } );
  209. function defineParentViewClass() {
  210. ParentView = class extends View {
  211. constructor() {
  212. super();
  213. this.element = document.createElement( 'span' );
  214. this.register( 'x', true );
  215. }
  216. };
  217. }
  218. function defineItemControllerClass() {
  219. ItemController = class extends Controller {
  220. constructor( model, view ) {
  221. super( model, view );
  222. view.model.bind( 'uid' ).to( model );
  223. }
  224. };
  225. }
  226. function defineItemViewClass() {
  227. ItemView = class extends View {
  228. constructor( locale ) {
  229. super( locale );
  230. const bind = this.bind;
  231. this.template = new Template( {
  232. tag: 'li',
  233. attributes: {
  234. id: bind.to( 'uid' )
  235. }
  236. } );
  237. }
  238. };
  239. }
  240. function createModelCollection() {
  241. models = new Collection( { idProperty: 'uid' } );
  242. for ( let i = 0; i < 5; i++ ) {
  243. models.add( new Model( {
  244. uid: Number( i ).toString()
  245. } ) );
  246. }
  247. }