This is an automated email from the ASF dual-hosted git repository. unclegedd pushed a commit to branch path-tests in repository https://gitbox.apache.org/repos/asf/incubator-flagon-useralejs.git
commit 6dfae2c7a8743fb300d2733ef0ab69ebab8bcedc Author: unclegedd <[email protected]> AuthorDate: Mon Jan 2 11:04:48 2023 -0600 WIP: refactors path tests --- src/packageLogs.js | 16 +++++----------- test/packageLogs_spec.js | 23 +++++------------------ 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/src/packageLogs.js b/src/packageLogs.js index 527206e..59e0b85 100644 --- a/src/packageLogs.js +++ b/src/packageLogs.js @@ -304,18 +304,12 @@ export function getSelector(ele) { * @return {HTMLElement[]} Array of elements, starting at the event target, ending at the root element. */ export function buildPath(e) { - let path = []; - if (typeof e.composedPath == "function" && e.composedPath().length > 0) { - path = e.composedPath(); - } else { - let ele = e.target - while(ele) { - path.push(ele); - ele = ele.parentElement; + // todo: window.event? + if (e instanceof window.Event) { + const path = e.composedPath(); + return selectorizePath(path); } - } - - return selectorizePath(path); + return null; } /** diff --git a/test/packageLogs_spec.js b/test/packageLogs_spec.js index e505969..a859e9c 100644 --- a/test/packageLogs_spec.js +++ b/test/packageLogs_spec.js @@ -313,28 +313,15 @@ describe('packageLogs', () => { describe('buildPath', () => { it('builds a path', () => { new JSDOM(``) + let actualPath const document = window.document; const ele = document.createElement('div'); - const evt = document.createEvent('CustomEvent'); - evt.initEvent('testEvent', true, true); + const evt = new window.Event('CustomEvent', {bubbles: true, cancelable: true}) document.body.appendChild(ele); + ele.addEventListener('CustomEvent', e => actualPath = buildPath(e)) ele.dispatchEvent(evt); - expect(buildPath(evt)).to.deep.equal(['div', 'body', 'html']); - }); - - it('defaults to path if available', () => { - new JSDOM(``) - const document = window.document; - const ele = document.createElement('div'); - const evt = document.createEvent('CustomEvent'); - document.body.appendChild(ele); - evt.initEvent('testEvent', true, true); - ele.dispatchEvent(evt); - evt.composedPath = function() { - let ele = evt.target; - return [ele, ele.parentElement, ele.parentElement.parentElement]; - }; - expect(buildPath(evt)).to.deep.equal(['div', 'body', 'html']); + const expectedPath = ['div', 'body', 'html', "#document", "Window"] + expect(actualPath).to.deep.equal(expectedPath); }); }); });
