createlistdropdown.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import Model from '../../../src/model';
  7. import createListDropdown from '../../../src/dropdown/list/createlistdropdown';
  8. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  9. import ListView from '../../../src/list/listview';
  10. import ListItemView from '../../../src/list/listitemview';
  11. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. describe( 'createListDropdown', () => {
  13. let view, 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. view = createListDropdown( model, locale );
  24. view.render();
  25. document.body.appendChild( view.element );
  26. } );
  27. afterEach( () => {
  28. view.element.remove();
  29. } );
  30. describe( 'constructor()', () => {
  31. it( 'sets view#locale', () => {
  32. expect( view.locale ).to.equal( locale );
  33. } );
  34. describe( 'view#listView', () => {
  35. it( 'is created', () => {
  36. const panelChildren = view.panelView.children;
  37. expect( panelChildren ).to.have.length( 1 );
  38. expect( panelChildren.get( 0 ) ).to.equal( view.listView );
  39. expect( view.listView ).to.be.instanceof( ListView );
  40. } );
  41. it( 'is bound to model#items', () => {
  42. items.add( new Model( { label: 'a', style: 'b' } ) );
  43. items.add( new Model( { label: 'c', style: 'd' } ) );
  44. expect( view.listView.items ).to.have.length( 2 );
  45. expect( view.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
  46. expect( view.listView.items.get( 1 ).label ).to.equal( 'c' );
  47. expect( view.listView.items.get( 1 ).style ).to.equal( 'd' );
  48. items.remove( 1 );
  49. expect( view.listView.items ).to.have.length( 1 );
  50. expect( view.listView.items.get( 0 ).label ).to.equal( 'a' );
  51. expect( view.listView.items.get( 0 ).style ).to.equal( 'b' );
  52. } );
  53. it( 'binds all attributes in model#items', () => {
  54. const itemModel = new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } );
  55. items.add( itemModel );
  56. const item = view.listView.items.get( 0 );
  57. expect( item.foo ).to.equal( 'bar' );
  58. expect( item.baz ).to.equal( 'qux' );
  59. itemModel.baz = 'foo?';
  60. expect( item.baz ).to.equal( 'foo?' );
  61. } );
  62. it( 'delegates view.listView#execute to the view', done => {
  63. items.add( new Model( { label: 'a', style: 'b' } ) );
  64. view.on( 'execute', evt => {
  65. expect( evt.source ).to.equal( view.listView.items.get( 0 ) );
  66. expect( evt.path ).to.deep.equal( [ view.listView.items.get( 0 ), view ] );
  67. done();
  68. } );
  69. view.listView.items.get( 0 ).fire( 'execute' );
  70. } );
  71. } );
  72. it( 'changes view#isOpen on view#execute', () => {
  73. view.isOpen = true;
  74. view.fire( 'execute' );
  75. expect( view.isOpen ).to.be.false;
  76. view.fire( 'execute' );
  77. expect( view.isOpen ).to.be.false;
  78. } );
  79. it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
  80. // Open the dropdown.
  81. view.isOpen = true;
  82. // Fire event from outside of the dropdown.
  83. document.body.dispatchEvent( new Event( 'click', {
  84. bubbles: true
  85. } ) );
  86. // Closed the dropdown.
  87. expect( view.isOpen ).to.be.false;
  88. // Fire event from outside of the dropdown.
  89. document.body.dispatchEvent( new Event( 'click', {
  90. bubbles: true
  91. } ) );
  92. // Dropdown is still closed.
  93. expect( view.isOpen ).to.be.false;
  94. } );
  95. it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
  96. // Open the dropdown.
  97. view.isOpen = true;
  98. // Event from view.element should be discarded.
  99. view.element.dispatchEvent( new Event( 'click', {
  100. bubbles: true
  101. } ) );
  102. // Dropdown is still open.
  103. expect( view.isOpen ).to.be.true;
  104. // Event from within view.element should be discarded.
  105. const child = document.createElement( 'div' );
  106. view.element.appendChild( child );
  107. child.dispatchEvent( new Event( 'click', {
  108. bubbles: true
  109. } ) );
  110. // Dropdown is still open.
  111. expect( view.isOpen ).to.be.true;
  112. } );
  113. describe( 'activates keyboard navigation for the dropdown', () => {
  114. it( 'so "arrowdown" focuses the #listView if dropdown is open', () => {
  115. const keyEvtData = {
  116. keyCode: keyCodes.arrowdown,
  117. preventDefault: sinon.spy(),
  118. stopPropagation: sinon.spy()
  119. };
  120. const spy = sinon.spy( view.listView, 'focus' );
  121. view.isOpen = false;
  122. view.keystrokes.press( keyEvtData );
  123. sinon.assert.notCalled( spy );
  124. view.isOpen = true;
  125. view.keystrokes.press( keyEvtData );
  126. sinon.assert.calledOnce( spy );
  127. } );
  128. it( 'so "arrowup" focuses the last #item in #listView if dropdown is open', () => {
  129. const keyEvtData = {
  130. keyCode: keyCodes.arrowup,
  131. preventDefault: sinon.spy(),
  132. stopPropagation: sinon.spy()
  133. };
  134. const spy = sinon.spy( view.listView, 'focusLast' );
  135. view.isOpen = false;
  136. view.keystrokes.press( keyEvtData );
  137. sinon.assert.notCalled( spy );
  138. view.isOpen = true;
  139. view.keystrokes.press( keyEvtData );
  140. sinon.assert.calledOnce( spy );
  141. } );
  142. } );
  143. } );
  144. } );