|
1 | 1 | /* eslint-env qunit */
|
| 2 | +import * as DomData from '../../src/js/utils/dom-data'; |
2 | 3 | import MenuButton from '../../src/js/menu/menu-button.js';
|
| 4 | +import Menu from '../../src/js/menu/menu.js'; |
| 5 | +import CaptionSettingsMenuItem from '../../src/js/control-bar/text-track-controls/caption-settings-menu-item'; |
3 | 6 | import MenuItem from '../../src/js/menu/menu-item.js';
|
4 | 7 | import TestHelpers from './test-helpers.js';
|
5 | 8 | import * as Events from '../../src/js/utils/events.js';
|
| 9 | +import sinon from 'sinon'; |
6 | 10 |
|
7 | 11 | QUnit.module('MenuButton');
|
8 | 12 |
|
@@ -106,3 +110,88 @@ QUnit.test('should keep all the added menu items', function(assert) {
|
106 | 110 | assert.ok(menuButton.el().contains(menuItem1.el()), 'the menu button contains the DOM element of `menuItem1` after second update');
|
107 | 111 | assert.ok(menuButton.el().contains(menuItem2.el()), 'the menu button contains the DOM element of `menuItem2` after second update');
|
108 | 112 | });
|
| 113 | + |
| 114 | +QUnit.test('should remove old event listeners when the menu item adds to the new menu', function(assert) { |
| 115 | + const player = TestHelpers.makePlayer(); |
| 116 | + const menuButton = new MenuButton(player, {}); |
| 117 | + const oldMenu = new Menu(player, { menuButton }); |
| 118 | + const newMenu = new Menu(player, { menuButton }); |
| 119 | + |
| 120 | + oldMenu.addItem('MenuItem'); |
| 121 | + |
| 122 | + const menuItem = oldMenu.children()[0]; |
| 123 | + |
| 124 | + assert.ok(menuItem instanceof MenuItem, '`menuItem` should be the instanceof of `MenuItem`'); |
| 125 | + |
| 126 | + /** |
| 127 | + * A reusable collection of assertions. |
| 128 | + */ |
| 129 | + function validateMenuEventListeners(watchedMenu) { |
| 130 | + const eventData = DomData.getData(menuItem.eventBusEl_); |
| 131 | + // `MenuButton`.`unpressButton` will be called when triggering click event on the menu item. |
| 132 | + const unpressButtonSpy = sinon.spy(menuButton, 'unpressButton'); |
| 133 | + // `MenuButton`.`focus` will be called when triggering click event on the menu item. |
| 134 | + const focusSpy = sinon.spy(menuButton, 'focus'); |
| 135 | + |
| 136 | + // `Menu`.`children` will be called when triggering blur event on the menu item. |
| 137 | + const menuChildrenSpy = sinon.spy(watchedMenu, 'children'); |
| 138 | + |
| 139 | + // The number of blur listeners is two because `ClickableComponent` |
| 140 | + // adds the blur event listener during the construction and |
| 141 | + // `MenuItem` inherits from `ClickableComponent`. |
| 142 | + assert.strictEqual(eventData.handlers.blur.length, 2, 'the number of blur listeners is two'); |
| 143 | + // Same reason mentioned above. |
| 144 | + assert.strictEqual(eventData.handlers.click.length, 2, 'the number of click listeners is two'); |
| 145 | + |
| 146 | + const blurListenerAddedByMenu = eventData.handlers.blur[1]; |
| 147 | + const clickListenerAddedByMenu = eventData.handlers.click[1]; |
| 148 | + |
| 149 | + assert.strictEqual( |
| 150 | + typeof blurListenerAddedByMenu.calledOnce, |
| 151 | + 'undefined', |
| 152 | + 'previous blur listener wrapped in the spy should be removed' |
| 153 | + ); |
| 154 | + |
| 155 | + assert.strictEqual( |
| 156 | + typeof clickListenerAddedByMenu.calledOnce, |
| 157 | + 'undefined', |
| 158 | + 'previous click listener wrapped in the spy should be removed' |
| 159 | + ); |
| 160 | + |
| 161 | + const blurListenerSpy = eventData.handlers.blur[1] = sinon.spy(blurListenerAddedByMenu); |
| 162 | + const clickListenerSpy = eventData.handlers.click[1] = sinon.spy(clickListenerAddedByMenu); |
| 163 | + |
| 164 | + TestHelpers.triggerDomEvent(menuItem.el(), 'blur'); |
| 165 | + |
| 166 | + assert.ok(blurListenerSpy.calledOnce, 'blur event listener should be called'); |
| 167 | + assert.strictEqual(blurListenerSpy.getCall(0).args[0].target, menuItem.el(), 'event target should be the `menuItem`'); |
| 168 | + assert.ok(menuChildrenSpy.calledOnce, '`watchedMenu`.`children` has been called'); |
| 169 | + |
| 170 | + TestHelpers.triggerDomEvent(menuItem.el(), 'click'); |
| 171 | + |
| 172 | + assert.ok(clickListenerSpy.calledOnce, 'click event listener should be called'); |
| 173 | + assert.strictEqual(clickListenerSpy.getCall(0).args[0].target, menuItem.el(), 'event target should be the `menuItem`'); |
| 174 | + assert.ok(unpressButtonSpy.calledOnce, '`menuButton`.`unpressButtion` has been called'); |
| 175 | + assert.ok(focusSpy.calledOnce, '`menuButton`.`focus` has been called'); |
| 176 | + |
| 177 | + unpressButtonSpy.restore(); |
| 178 | + focusSpy.restore(); |
| 179 | + menuChildrenSpy.restore(); |
| 180 | + } |
| 181 | + |
| 182 | + validateMenuEventListeners(oldMenu); |
| 183 | + |
| 184 | + newMenu.addItem(menuItem); |
| 185 | + validateMenuEventListeners(newMenu); |
| 186 | + |
| 187 | + const focusSpy = sinon.spy(menuButton, 'focus'); |
| 188 | + const captionMenuItem = new CaptionSettingsMenuItem(player, { |
| 189 | + kind: 'subtitles' |
| 190 | + }); |
| 191 | + |
| 192 | + newMenu.addItem(captionMenuItem); |
| 193 | + TestHelpers.triggerDomEvent(captionMenuItem.el(), 'click'); |
| 194 | + assert.ok(!focusSpy.called, '`menuButton`.`focus` should never be called'); |
| 195 | + |
| 196 | + focusSpy.restore(); |
| 197 | +}); |
0 commit comments