From 114b2f41ec0fe46a300e13250583350abc290046 Mon Sep 17 00:00:00 2001 From: mister-ben <1676039+mister-ben@users.noreply.github.com> Date: Wed, 14 Aug 2024 21:58:01 +0200 Subject: [PATCH] fix: ensure spatial navigation starts without error without an ErrorDisplay component --- src/js/spatial-navigation.js | 30 +++++++++++++++------------- test/unit/spatial-navigation.test.js | 8 ++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/js/spatial-navigation.js b/src/js/spatial-navigation.js index dacb5e75a1..6cbde34a55 100644 --- a/src/js/spatial-navigation.js +++ b/src/js/spatial-navigation.js @@ -59,25 +59,27 @@ class SpatialNavigation extends EventTarget { this.player_.on('focusin', this.handlePlayerFocus_.bind(this)); this.player_.on('focusout', this.handlePlayerBlur_.bind(this)); this.isListening_ = true; - this.player_.errorDisplay.on('aftermodalfill', () => { - this.updateFocusableComponents(); + if (this.player_.errorDisplay) { + this.player_.errorDisplay.on('aftermodalfill', () => { + this.updateFocusableComponents(); - if (this.focusableComponents.length) { - // The modal has focusable components: + if (this.focusableComponents.length) { + // The modal has focusable components: - if (this.focusableComponents.length > 1) { - // The modal has close button + some additional buttons. - // Focusing first additional button: + if (this.focusableComponents.length > 1) { + // The modal has close button + some additional buttons. + // Focusing first additional button: - this.focusableComponents[1].focus(); - } else { - // The modal has only close button, - // Focusing it: + this.focusableComponents[1].focus(); + } else { + // The modal has only close button, + // Focusing it: - this.focusableComponents[0].focus(); + this.focusableComponents[0].focus(); + } } - } - }); + }); + } } /** diff --git a/test/unit/spatial-navigation.test.js b/test/unit/spatial-navigation.test.js index 6c7d290bc6..1c1ccb518b 100644 --- a/test/unit/spatial-navigation.test.js +++ b/test/unit/spatial-navigation.test.js @@ -612,3 +612,11 @@ QUnit.test('If component passes the required functions it should be added to foc assert.strictEqual(this.spatialNav.focusableComponents.length, 1, 'focusableComponents array should have 1 component'); assert.strictEqual(this.spatialNav.focusableComponents[0].name_, 'firstComponent', 'the name of the component in focusableComponents array should be "firstComponent"'); }); + +QUnit.test('Doesn\'t error if no ErrorDisplay component is present', function(assert) { + this.player.errorDisplay.dispose(); + delete this.player.errorDisplay; + + this.spatialNav.start(); + assert.ok(true, 'started without throwing when errorDisplay not present'); +});