addlistviewtodropdown.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 { createDropdown } from './../../../src/dropdown/utils';
  11. import addListViewToDropdown from '../../../src/dropdown/helpers/addlistviewtodropdown';
  12. describe( 'addListViewToDropdown()', () => {
  13. let dropdownView, model, locale, items;
  14. beforeEach( () => {
  15. locale = { t() {} };
  16. items = new Collection();
  17. model = new Model( {
  18. isEnabled: true,
  19. items,
  20. isOn: false,
  21. label: 'foo'
  22. } );
  23. dropdownView = createDropdown( model, locale );
  24. addListViewToDropdown( dropdownView, model, locale );
  25. dropdownView.render();
  26. document.body.appendChild( dropdownView.element );
  27. } );
  28. afterEach( () => {
  29. dropdownView.element.remove();
  30. } );
  31. describe( 'view#listView', () => {
  32. it( 'is created', () => {
  33. const panelChildren = dropdownView.panelView.children;
  34. expect( panelChildren ).to.have.length( 1 );
  35. expect( panelChildren.get( 0 ) ).to.equal( dropdownView.listView );
  36. expect( dropdownView.listView ).to.be.instanceof( ListView );
  37. } );
  38. it( 'is bound to model#items', () => {
  39. items.add( new Model( { label: 'a', style: 'b' } ) );
  40. items.add( new Model( { label: 'c', style: 'd' } ) );
  41. expect( dropdownView.listView.items ).to.have.length( 2 );
  42. expect( dropdownView.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
  43. expect( dropdownView.listView.items.get( 1 ).label ).to.equal( 'c' );
  44. expect( dropdownView.listView.items.get( 1 ).style ).to.equal( 'd' );
  45. items.remove( 1 );
  46. expect( dropdownView.listView.items ).to.have.length( 1 );
  47. expect( dropdownView.listView.items.get( 0 ).label ).to.equal( 'a' );
  48. expect( dropdownView.listView.items.get( 0 ).style ).to.equal( 'b' );
  49. } );
  50. it( 'binds all attributes in model#items', () => {
  51. const itemModel = new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } );
  52. items.add( itemModel );
  53. const item = dropdownView.listView.items.get( 0 );
  54. expect( item.foo ).to.equal( 'bar' );
  55. expect( item.baz ).to.equal( 'qux' );
  56. itemModel.baz = 'foo?';
  57. expect( item.baz ).to.equal( 'foo?' );
  58. } );
  59. it( 'delegates view.listView#execute to the view', done => {
  60. items.add( new Model( { label: 'a', style: 'b' } ) );
  61. dropdownView.on( 'execute', evt => {
  62. expect( evt.source ).to.equal( dropdownView.listView.items.get( 0 ) );
  63. expect( evt.path ).to.deep.equal( [ dropdownView.listView.items.get( 0 ), dropdownView ] );
  64. done();
  65. } );
  66. dropdownView.listView.items.get( 0 ).fire( 'execute' );
  67. } );
  68. } );
  69. } );