utils.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ButtonView from '../../src/button/buttonview';
  9. import ToolbarView from '../../src/toolbar/toolbarview';
  10. import ListItemView from '../../src/list/listitemview';
  11. import ListView from '../../src/list/listview';
  12. import createButtonForDropdown from '../../src/dropdown/helpers/createbuttonfordropdown';
  13. import createDropdownView from '../../src/dropdown/helpers/createdropdownview';
  14. import {
  15. addListViewToDropdown,
  16. addToolbarToDropdown
  17. } from '../../src/dropdown/utils';
  18. describe( 'utils', () => {
  19. let dropdownView, buttonView, model, locale;
  20. beforeEach( () => {
  21. locale = { t() {} };
  22. model = new Model();
  23. buttonView = createButtonForDropdown( model, locale );
  24. dropdownView = createDropdownView( model, buttonView, locale );
  25. } );
  26. afterEach( () => {
  27. if ( dropdownView.element ) {
  28. dropdownView.element.remove();
  29. }
  30. } );
  31. describe( 'addListViewToDropdown()', () => {
  32. let items;
  33. beforeEach( () => {
  34. items = new Collection();
  35. model = new Model( {
  36. isEnabled: true,
  37. items,
  38. isOn: false,
  39. label: 'foo'
  40. } );
  41. buttonView = createButtonForDropdown( model, locale );
  42. dropdownView = createDropdownView( model, buttonView, locale );
  43. addListViewToDropdown( dropdownView, model, locale );
  44. dropdownView.render();
  45. document.body.appendChild( dropdownView.element );
  46. } );
  47. describe( 'view#listView', () => {
  48. it( 'is created', () => {
  49. const panelChildren = dropdownView.panelView.children;
  50. expect( panelChildren ).to.have.length( 1 );
  51. expect( panelChildren.get( 0 ) ).to.equal( dropdownView.listView );
  52. expect( dropdownView.listView ).to.be.instanceof( ListView );
  53. } );
  54. it( 'is bound to model#items', () => {
  55. items.add( new Model( { label: 'a', style: 'b' } ) );
  56. items.add( new Model( { label: 'c', style: 'd' } ) );
  57. expect( dropdownView.listView.items ).to.have.length( 2 );
  58. expect( dropdownView.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
  59. expect( dropdownView.listView.items.get( 1 ).label ).to.equal( 'c' );
  60. expect( dropdownView.listView.items.get( 1 ).style ).to.equal( 'd' );
  61. items.remove( 1 );
  62. expect( dropdownView.listView.items ).to.have.length( 1 );
  63. expect( dropdownView.listView.items.get( 0 ).label ).to.equal( 'a' );
  64. expect( dropdownView.listView.items.get( 0 ).style ).to.equal( 'b' );
  65. } );
  66. it( 'binds all attributes in model#items', () => {
  67. const itemModel = new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } );
  68. items.add( itemModel );
  69. const item = dropdownView.listView.items.get( 0 );
  70. expect( item.foo ).to.equal( 'bar' );
  71. expect( item.baz ).to.equal( 'qux' );
  72. itemModel.baz = 'foo?';
  73. expect( item.baz ).to.equal( 'foo?' );
  74. } );
  75. it( 'delegates view.listView#execute to the view', done => {
  76. items.add( new Model( { label: 'a', style: 'b' } ) );
  77. dropdownView.on( 'execute', evt => {
  78. expect( evt.source ).to.equal( dropdownView.listView.items.get( 0 ) );
  79. expect( evt.path ).to.deep.equal( [ dropdownView.listView.items.get( 0 ), dropdownView ] );
  80. done();
  81. } );
  82. dropdownView.listView.items.get( 0 ).fire( 'execute' );
  83. } );
  84. } );
  85. } );
  86. describe( 'addToolbarToDropdown()', () => {
  87. let buttons;
  88. beforeEach( () => {
  89. buttons = [ '<svg>foo</svg>', '<svg>bar</svg>' ].map( icon => {
  90. const button = new ButtonView();
  91. button.icon = icon;
  92. return button;
  93. } );
  94. model = new Model( {
  95. isVertical: true,
  96. buttons
  97. } );
  98. buttonView = createButtonForDropdown( model, locale );
  99. dropdownView = createDropdownView( model, buttonView, locale );
  100. addToolbarToDropdown( dropdownView, model );
  101. dropdownView.render();
  102. document.body.appendChild( dropdownView.element );
  103. } );
  104. it( 'sets view#locale', () => {
  105. expect( dropdownView.locale ).to.equal( locale );
  106. } );
  107. it( 'sets view class', () => {
  108. expect( dropdownView.element.classList.contains( 'ck-toolbar-dropdown' ) ).to.be.true;
  109. } );
  110. describe( 'view#toolbarView', () => {
  111. it( 'is created', () => {
  112. const panelChildren = dropdownView.panelView.children;
  113. expect( panelChildren ).to.have.length( 1 );
  114. expect( panelChildren.get( 0 ) ).to.equal( dropdownView.toolbarView );
  115. expect( dropdownView.toolbarView ).to.be.instanceof( ToolbarView );
  116. } );
  117. it.skip( 'delegates view.toolbarView.items#execute to the view', done => {
  118. dropdownView.on( 'execute', evt => {
  119. expect( evt.source ).to.equal( dropdownView.toolbarView.items.get( 0 ) );
  120. expect( evt.path ).to.deep.equal( [ dropdownView.toolbarView.items.get( 0 ), dropdownView ] );
  121. done();
  122. } );
  123. dropdownView.toolbarView.items.get( 0 ).fire( 'execute' );
  124. } );
  125. it( 'reacts on model#isVertical', () => {
  126. model.isVertical = false;
  127. expect( dropdownView.toolbarView.isVertical ).to.be.false;
  128. model.isVertical = true;
  129. expect( dropdownView.toolbarView.isVertical ).to.be.true;
  130. } );
  131. } );
  132. describe( 'buttons', () => {
  133. // TODO: test me!
  134. } );
  135. } );
  136. describe( 'getBindingTargets()', () => {} );
  137. } );