createlistdropdown.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: items,
  20. isOn: false,
  21. label: 'foo'
  22. } );
  23. return ( view = createListDropdown( model, locale ) ).init().then( () => {
  24. document.body.appendChild( view.element );
  25. } );
  26. } );
  27. describe( 'constructor()', () => {
  28. it( 'sets view#locale', () => {
  29. expect( view.locale ).to.equal( locale );
  30. } );
  31. describe( 'view#listView', () => {
  32. it( 'is created', () => {
  33. const panelChildren = view.panelView.children;
  34. expect( panelChildren ).to.have.length( 1 );
  35. expect( panelChildren.get( 0 ) ).to.equal( view.listView );
  36. expect( view.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( view.listView.items ).to.have.length( 2 );
  42. expect( view.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
  43. expect( view.listView.items.get( 1 ).label ).to.equal( 'c' );
  44. expect( view.listView.items.get( 1 ).style ).to.equal( 'd' );
  45. items.remove( 1 );
  46. expect( view.listView.items ).to.have.length( 1 );
  47. expect( view.listView.items.get( 0 ).label ).to.equal( 'a' );
  48. expect( view.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 = view.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. view.on( 'execute', ( evt ) => {
  62. expect( evt.source ).to.equal( view.listView.items.get( 0 ) );
  63. expect( evt.path ).to.deep.equal( [ view.listView.items.get( 0 ), view ] );
  64. done();
  65. } );
  66. view.listView.items.get( 0 ).fire( 'execute' );
  67. } );
  68. } );
  69. it( 'changes view#isOpen on view#execute', () => {
  70. view.isOpen = true;
  71. view.fire( 'execute' );
  72. expect( view.isOpen ).to.be.false;
  73. view.fire( 'execute' );
  74. expect( view.isOpen ).to.be.false;
  75. } );
  76. it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
  77. // Open the dropdown.
  78. view.isOpen = true;
  79. // Fire event from outside of the dropdown.
  80. document.body.dispatchEvent( new Event( 'click', {
  81. bubbles: true
  82. } ) );
  83. // Closed the dropdown.
  84. expect( view.isOpen ).to.be.false;
  85. // Fire event from outside of the dropdown.
  86. document.body.dispatchEvent( new Event( 'click', {
  87. bubbles: true
  88. } ) );
  89. // Dropdown is still closed.
  90. expect( view.isOpen ).to.be.false;
  91. } );
  92. it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
  93. // Open the dropdown.
  94. view.isOpen = true;
  95. // Event from view.element should be discarded.
  96. view.element.dispatchEvent( new Event( 'click', {
  97. bubbles: true
  98. } ) );
  99. // Dropdown is still open.
  100. expect( view.isOpen ).to.be.true;
  101. // Event from within view.element should be discarded.
  102. const child = document.createElement( 'div' );
  103. view.element.appendChild( child );
  104. child.dispatchEvent( new Event( 'click', {
  105. bubbles: true
  106. } ) );
  107. // Dropdown is still open.
  108. expect( view.isOpen ).to.be.true;
  109. } );
  110. describe( 'activates keyboard navigation for the dropdown', () => {
  111. it( 'so "arrowdown" focuses the #listView if dropdown is open', () => {
  112. const keyEvtData = {
  113. keyCode: keyCodes.arrowdown,
  114. preventDefault: sinon.spy(),
  115. stopPropagation: sinon.spy()
  116. };
  117. const spy = sinon.spy( view.listView, 'focus' );
  118. view.isOpen = false;
  119. view.keystrokes.press( keyEvtData );
  120. sinon.assert.notCalled( spy );
  121. view.isOpen = true;
  122. view.keystrokes.press( keyEvtData );
  123. sinon.assert.calledOnce( spy );
  124. } );
  125. it( 'so "arrowup" focuses the last #item in #listView if dropdown is open', () => {
  126. const keyEvtData = {
  127. keyCode: keyCodes.arrowup,
  128. preventDefault: sinon.spy(),
  129. stopPropagation: sinon.spy()
  130. };
  131. const spy = sinon.spy( view.listView, 'focusLast' );
  132. view.isOpen = false;
  133. view.keystrokes.press( keyEvtData );
  134. sinon.assert.notCalled( spy );
  135. view.isOpen = true;
  136. view.keystrokes.press( keyEvtData );
  137. sinon.assert.calledOnce( spy );
  138. } );
  139. } );
  140. } );
  141. } );