From 45aef26ba48cd7136746a5860aac7ec8b5f6f826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Mon, 14 Jan 2019 17:24:13 +0100 Subject: [PATCH 1/3] Add FocustTracker.destroy() method. --- src/focustracker.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/focustracker.js b/src/focustracker.js index dcc48a5..748b179 100644 --- a/src/focustracker.js +++ b/src/focustracker.js @@ -94,6 +94,16 @@ export default class FocusTracker { } } + /** + * Destroys the focus tracker by: + * - Disabling all event listeners attached to tracked elements. + * - Removing all tracked elements that were previously added. + */ + destroy() { + this.stopListening(); + this._elements.clear(); + } + /** * Stores currently focused element and set {#isFocused} as `true`. * From ab2fc69e71a4f47dcc7d0f9988c8a17a93b1b4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Wed, 16 Jan 2019 15:27:07 +0100 Subject: [PATCH 2/3] Remove redundant collection.clear() calls. --- src/focustracker.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/focustracker.js b/src/focustracker.js index 748b179..3aee7d9 100644 --- a/src/focustracker.js +++ b/src/focustracker.js @@ -101,7 +101,6 @@ export default class FocusTracker { */ destroy() { this.stopListening(); - this._elements.clear(); } /** From 25529a0b2fd4e29ad3dec39eaf3b0edb003424b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Wed, 16 Jan 2019 16:14:27 +0100 Subject: [PATCH 3/3] Add FocusTracker.destroy() test. --- tests/focustracker.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/focustracker.js b/tests/focustracker.js index 0e555b3..075bcc2 100644 --- a/tests/focustracker.js +++ b/tests/focustracker.js @@ -170,4 +170,14 @@ describe( 'FocusTracker', () => { expect( focusTracker.isFocused ).to.false; } ); } ); + + describe( 'destroy()', () => { + it( 'should stop listening', () => { + const stopListeningSpy = sinon.spy( focusTracker, 'stopListening' ); + + focusTracker.destroy(); + + sinon.assert.calledOnce( stopListeningSpy ); + } ); + } ); } );