createlistdropdown.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. describe( 'createListDropdown', () => {
  12. let view, model, locale, items;
  13. beforeEach( () => {
  14. locale = { t() {} };
  15. items = new Collection();
  16. model = new Model( {
  17. isEnabled: true,
  18. items: items,
  19. isOn: false,
  20. label: 'foo'
  21. } );
  22. return ( view = createListDropdown( model, locale ) ).init().then( () => {
  23. document.body.appendChild( view.element );
  24. } );
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'sets view#locale', () => {
  28. expect( view.locale ).to.equal( locale );
  29. } );
  30. describe( 'view#listView', () => {
  31. it( 'is created', () => {
  32. const panelChildren = view.panelView.children;
  33. expect( panelChildren ).to.have.length( 1 );
  34. expect( panelChildren.get( 0 ) ).to.equal( view.listView );
  35. expect( view.listView ).to.be.instanceof( ListView );
  36. } );
  37. it( 'is bound to model#items', () => {
  38. items.add( new Model( { label: 'a', style: 'b' } ) );
  39. items.add( new Model( { label: 'c', style: 'd' } ) );
  40. expect( view.listView.items ).to.have.length( 2 );
  41. expect( view.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
  42. expect( view.listView.items.get( 1 ).label ).to.equal( 'c' );
  43. expect( view.listView.items.get( 1 ).style ).to.equal( 'd' );
  44. items.remove( 1 );
  45. expect( view.listView.items ).to.have.length( 1 );
  46. expect( view.listView.items.get( 0 ).label ).to.equal( 'a' );
  47. expect( view.listView.items.get( 0 ).style ).to.equal( 'b' );
  48. } );
  49. it( 'binds all attributes in model#items', () => {
  50. const itemModel = new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } );
  51. items.add( itemModel );
  52. const item = view.listView.items.get( 0 );
  53. expect( item.foo ).to.equal( 'bar' );
  54. expect( item.baz ).to.equal( 'qux' );
  55. itemModel.baz = 'foo?';
  56. expect( item.baz ).to.equal( 'foo?' );
  57. } );
  58. it( 'delegates view.listView#execute to the view', ( done ) => {
  59. items.add( new Model( { label: 'a', style: 'b' } ) );
  60. view.on( 'execute', ( evt ) => {
  61. expect( evt.source ).to.equal( view.listView.items.get( 0 ) );
  62. expect( evt.path ).to.deep.equal( [ view.listView.items.get( 0 ), view ] );
  63. done();
  64. } );
  65. view.listView.items.get( 0 ).fire( 'execute' );
  66. } );
  67. } );
  68. it( 'changes view#isOpen on view#execute', () => {
  69. view.isOpen = true;
  70. view.fire( 'execute' );
  71. expect( view.isOpen ).to.be.false;
  72. view.fire( 'execute' );
  73. expect( view.isOpen ).to.be.false;
  74. } );
  75. it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
  76. // Open the dropdown.
  77. view.isOpen = true;
  78. // Fire event from outside of the dropdown.
  79. document.body.dispatchEvent( new Event( 'click', {
  80. bubbles: true
  81. } ) );
  82. // Closed the dropdown.
  83. expect( view.isOpen ).to.be.false;
  84. // Fire event from outside of the dropdown.
  85. document.body.dispatchEvent( new Event( 'click', {
  86. bubbles: true
  87. } ) );
  88. // Dropdown is still closed.
  89. expect( view.isOpen ).to.be.false;
  90. } );
  91. it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
  92. // Open the dropdown.
  93. view.isOpen = true;
  94. // Event from view.element should be discarded.
  95. view.element.dispatchEvent( new Event( 'click', {
  96. bubbles: true
  97. } ) );
  98. // Dropdown is still open.
  99. expect( view.isOpen ).to.be.true;
  100. // Event from within view.element should be discarded.
  101. const child = document.createElement( 'div' );
  102. view.element.appendChild( child );
  103. child.dispatchEvent( new Event( 'click', {
  104. bubbles: true
  105. } ) );
  106. // Dropdown is still open.
  107. expect( view.isOpen ).to.be.true;
  108. } );
  109. } );
  110. } );