From 4535b09c46b0de464dc7bca435e18bd463791112 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Fri, 11 Dec 2020 00:36:41 +0530 Subject: [PATCH] Tooltip - check if tip and its parent node exist in dispose method (#32420) --- js/src/tooltip.js | 2 +- js/tests/unit/tooltip.spec.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 4eb4fde0bf..103524b8b4 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -222,7 +222,7 @@ class Tooltip extends BaseComponent { EventHandler.off(this._element, this.constructor.EVENT_KEY) EventHandler.off(this._element.closest(`.${CLASS_NAME_MODAL}`), 'hide.bs.modal', this._hideModalHandler) - if (this.tip) { + if (this.tip && this.tip.parentNode) { this.tip.parentNode.removeChild(this.tip) } diff --git a/js/tests/unit/tooltip.spec.js b/js/tests/unit/tooltip.spec.js index 9fc306dfe1..2b20d7e117 100644 --- a/js/tests/unit/tooltip.spec.js +++ b/js/tests/unit/tooltip.spec.js @@ -286,6 +286,24 @@ describe('Tooltip', () => { expect(Tooltip.getInstance(tooltipEl)).toEqual(null) }) + it('should destroy a tooltip after it is shown and hidden', () => { + fixtureEl.innerHTML = '' + + const tooltipEl = fixtureEl.querySelector('a') + const tooltip = new Tooltip(tooltipEl) + + tooltipEl.addEventListener('shown.bs.tooltip', () => { + tooltip.hide() + }) + tooltipEl.addEventListener('hidden.bs.tooltip', () => { + tooltip.dispose() + expect(tooltip.tip).toEqual(null) + expect(Tooltip.getInstance(tooltipEl)).toEqual(null) + }) + + tooltip.show() + }) + it('should destroy a tooltip and remove it from the dom', done => { fixtureEl.innerHTML = ''