addlistviewtodropdown.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  7. import Model from '../../../src/model';
  8. import ListItemView from '../../../src/list/listitemview';
  9. import ListView from '../../../src/list/listview';
  10. import createButtonForDropdown from '../../../src/dropdown/helpers/createbuttonfordropdown';
  11. import createDropdownView from '../../../src/dropdown/helpers/createdropdownview';
  12. import addListViewToDropdown from '../../../src/dropdown/helpers/addlistviewtodropdown';
  13. describe( 'addListViewToDropdown()', () => {
  14. let dropdownView, buttonView, model, locale, items;
  15. beforeEach( () => {
  16. locale = { t() {} };
  17. items = new Collection();
  18. model = new Model( {
  19. isEnabled: true,
  20. items,
  21. isOn: false,
  22. label: 'foo'
  23. } );
  24. buttonView = createButtonForDropdown( model, locale );
  25. dropdownView = createDropdownView( model, buttonView, locale );
  26. addListViewToDropdown( dropdownView, model, locale );
  27. dropdownView.render();
  28. document.body.appendChild( dropdownView.element );
  29. } );
  30. afterEach( () => {
  31. dropdownView.element.remove();
  32. } );
  33. describe( 'view#listView', () => {
  34. it( 'is created', () => {
  35. const panelChildren = dropdownView.panelView.children;
  36. expect( panelChildren ).to.have.length( 1 );
  37. expect( panelChildren.get( 0 ) ).to.equal( dropdownView.listView );
  38. expect( dropdownView.listView ).to.be.instanceof( ListView );
  39. } );
  40. it( 'is bound to model#items', () => {
  41. items.add( new Model( { label: 'a', style: 'b' } ) );
  42. items.add( new Model( { label: 'c', style: 'd' } ) );
  43. expect( dropdownView.listView.items ).to.have.length( 2 );
  44. expect( dropdownView.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
  45. expect( dropdownView.listView.items.get( 1 ).label ).to.equal( 'c' );
  46. expect( dropdownView.listView.items.get( 1 ).style ).to.equal( 'd' );
  47. items.remove( 1 );
  48. expect( dropdownView.listView.items ).to.have.length( 1 );
  49. expect( dropdownView.listView.items.get( 0 ).label ).to.equal( 'a' );
  50. expect( dropdownView.listView.items.get( 0 ).style ).to.equal( 'b' );
  51. } );
  52. it( 'binds all attributes in model#items', () => {
  53. const itemModel = new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } );
  54. items.add( itemModel );
  55. const item = dropdownView.listView.items.get( 0 );
  56. expect( item.foo ).to.equal( 'bar' );
  57. expect( item.baz ).to.equal( 'qux' );
  58. itemModel.baz = 'foo?';
  59. expect( item.baz ).to.equal( 'foo?' );
  60. } );
  61. it( 'delegates view.listView#execute to the view', done => {
  62. items.add( new Model( { label: 'a', style: 'b' } ) );
  63. dropdownView.on( 'execute', evt => {
  64. expect( evt.source ).to.equal( dropdownView.listView.items.get( 0 ) );
  65. expect( evt.path ).to.deep.equal( [ dropdownView.listView.items.get( 0 ), dropdownView ] );
  66. done();
  67. } );
  68. dropdownView.listView.items.get( 0 ).fire( 'execute' );
  69. } );
  70. } );
  71. } );