diff --git a/packages/happy-dom/src/cookie/CookieContainer.ts b/packages/happy-dom/src/cookie/CookieContainer.ts index 4d36b864f..27063f3f4 100644 --- a/packages/happy-dom/src/cookie/CookieContainer.ts +++ b/packages/happy-dom/src/cookie/CookieContainer.ts @@ -21,9 +21,7 @@ export default class CookieContainer implements ICookieContainer { public addCookies(cookies: ICookie[]): void { const indexMap: { [k: string]: number } = {}; const getKey = (cookie: ICookie): string => - `${cookie.key}-${cookie.originURL.hostname}-${ - cookie.originURL.pathname - }-${typeof cookie.value}`; + `${cookie.key}-${cookie.originURL.hostname}-${cookie.path}-${typeof cookie.value}`; // Creates a map of cookie key, domain, path and value to index. for (let i = 0, max = this.#cookies.length; i < max; i++) { diff --git a/packages/happy-dom/src/nodes/html-anchor-element/HTMLAnchorElement.ts b/packages/happy-dom/src/nodes/html-anchor-element/HTMLAnchorElement.ts index 42146e045..452f71e29 100644 --- a/packages/happy-dom/src/nodes/html-anchor-element/HTMLAnchorElement.ts +++ b/packages/happy-dom/src/nodes/html-anchor-element/HTMLAnchorElement.ts @@ -500,8 +500,7 @@ export default class HTMLAnchorElement extends HTMLElement implements IHTMLHyper if ( event.type === 'click' && event instanceof MouseEvent && - (event.eventPhase === EventPhaseEnum.atTarget || - event.eventPhase === EventPhaseEnum.bubbling) && + event.eventPhase === EventPhaseEnum.none && !event.defaultPrevented ) { const href = this.href; diff --git a/packages/jest-environment/test/react/React.test.tsx b/packages/jest-environment/test/react/React.test.tsx index 9b44f6d7d..f28f1ba8b 100644 --- a/packages/jest-environment/test/react/React.test.tsx +++ b/packages/jest-environment/test/react/React.test.tsx @@ -110,4 +110,14 @@ describe('React', () => { await TESTING_LIBRARY_USER.click(button); expect(document.querySelector('p span').textContent).toBe('test'); }); + + it('Can `preventDefault` to prevent navigation with React click listener on an anchor tag', async () => { + location.href = 'http://localhost/'; + const { getByRole } = ReactTestingLibrary.render( + ev.preventDefault()} /> + ); + expect(document.location.href).toBe('http://localhost/'); + await TESTING_LIBRARY_USER.click(getByRole('link')); + expect(document.location.href).toBe('http://localhost/'); + }); });