diff --git a/CHANGELOG.md b/CHANGELOG.md index 8711323e6f..7275067921 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ CHANGELOG ========= ## HEAD (Unreleased) -_(none)_ +* @llun fixed menus from throwing when focused when empty ([view](https://github.com/videojs/video.js/pull/3218)) -------------------- diff --git a/src/js/menu/menu.js b/src/js/menu/menu.js index 91af1ef14f..688e44a11a 100644 --- a/src/js/menu/menu.js +++ b/src/js/menu/menu.js @@ -118,14 +118,13 @@ class Menu extends Component { */ focus (item = 0) { let children = this.children().slice(); - let haveTitle = children[0].className && + let haveTitle = children.length && children[0].className && /vjs-menu-title/.test(children[0].className); if (haveTitle) { children.shift(); } - if (children.length > 0) { if (item < 0) { item = 0; diff --git a/test/unit/menu.test.js b/test/unit/menu.test.js index 5fbde6afcc..e6880b3867 100644 --- a/test/unit/menu.test.js +++ b/test/unit/menu.test.js @@ -1,11 +1,27 @@ import MenuButton from '../../src/js/menu/menu-button.js'; import TestHelpers from './test-helpers.js'; +import * as Events from '../../src/js/utils/events.js'; q.module('MenuButton'); -test('should place title list item into ul', function() { - var player, menuButton; +q.test('should not throw an error when there is no children', function() { + expect(0); + let player = TestHelpers.makePlayer(); + + let menuButton = new MenuButton(player); + let el = menuButton.el(); + + try { + Events.trigger(el, 'click'); + } catch (error) { + ok(!error, 'click should not throw anything'); + } + player.dispose(); +}); + +q.test('should place title list item into ul', function() { + var player, menuButton; player = TestHelpers.makePlayer(); menuButton = new MenuButton(player, { @@ -18,4 +34,4 @@ test('should place title list item into ul', function() { ok(titleElement.innerHTML === 'TestTitle', 'title element placed in ul'); player.dispose(); -}); \ No newline at end of file +});