From 978dbfa87bda10c685d47cb7ea177a504a4384ac Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 8 Jan 2025 23:41:55 +0100 Subject: [PATCH] chore: [#1661] Adds unit test for testing bubbling of events (#1679) --- .../happy-dom/test/nodes/node/Node.test.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/happy-dom/test/nodes/node/Node.test.ts b/packages/happy-dom/test/nodes/node/Node.test.ts index faf0fc51..4b9d82c4 100644 --- a/packages/happy-dom/test/nodes/node/Node.test.ts +++ b/packages/happy-dom/test/nodes/node/Node.test.ts @@ -1135,6 +1135,48 @@ describe('Node', () => { true ); }); + + it('Handles bubbling events correctly for issue #1661', () => { + window.document.body.innerHTML = '
'; + + const div = window.document.querySelector('div'); + const button = window.document.querySelector('button'); + const outputs: string[] = []; + + for (const node of [div, button]) { + node.addEventListener('click', () => { + outputs.push('click:' + node.nodeName); + }); + + node.addEventListener('a', () => { + outputs.push('a:' + node.nodeName); + }); + + node.addEventListener('b', () => { + outputs.push('b:' + node.nodeName); + }); + + node.addEventListener('c', () => { + outputs.push('c:' + node.nodeName); + }); + } + + button.click(); + button.dispatchEvent(new Event('a', { bubbles: true })); + button.dispatchEvent(new Event('b', { bubbles: true })); + button.dispatchEvent(new Event('c', { bubbles: true })); + + expect(outputs).toEqual([ + 'click:BUTTON', + 'click:DIV', + 'a:BUTTON', + 'a:DIV', + 'b:BUTTON', + 'b:DIV', + 'c:BUTTON', + 'c:DIV' + ]); + }); }); describe('compareDocumentPosition()', () => {