8
0

createlistdropdown.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 'ckeditor5-ui/src/model';
  7. import createListDropdown from 'ckeditor5-ui/src/dropdown/list/createlistdropdown';
  8. import Collection from 'ckeditor5-utils/src/collection';
  9. import ListView from 'ckeditor5-ui/src/list/listview';
  10. import ListItemView from 'ckeditor5-ui/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. expect( Object.keys( view._listeningTo ) ).to.have.length( 2 );
  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. expect( Object.keys( view._listeningTo ) ).to.have.length( 1 );
  86. // Fire event from outside of the dropdown.
  87. document.body.dispatchEvent( new Event( 'click', {
  88. bubbles: true
  89. } ) );
  90. // Dropdown is still closed.
  91. expect( view.isOpen ).to.be.false;
  92. expect( Object.keys( view._listeningTo ) ).to.have.length( 1 );
  93. } );
  94. it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
  95. // Open the dropdown.
  96. view.isOpen = true;
  97. expect( Object.keys( view._listeningTo ) ).to.have.length( 2 );
  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. expect( Object.keys( view._listeningTo ) ).to.have.length( 2 );
  105. // Event from within view.element should be discarded.
  106. const child = document.createElement( 'div' );
  107. view.element.appendChild( child );
  108. child.dispatchEvent( new Event( 'click', {
  109. bubbles: true
  110. } ) );
  111. // Dropdown is still open.
  112. expect( view.isOpen ).to.be.true;
  113. expect( Object.keys( view._listeningTo ) ).to.have.length( 2 );
  114. } );
  115. } );
  116. } );