test(actions): add defaultPrevented coverage for clickOutside (#195)
The action already checks event.defaultPrevented before dispatching clickoutside, but that branch had no test. Add the missing case and add a one-line comment explaining why capture phase is used. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -51,6 +51,18 @@ describe('clickOutside action', () => {
|
|||||||
expect(fired).toBe(false);
|
expect(fired).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not dispatch clickoutside when event.defaultPrevented is true', () => {
|
||||||
|
const node = makeNode();
|
||||||
|
const outside = makeNode();
|
||||||
|
let fired = false;
|
||||||
|
node.addEventListener('clickoutside', () => (fired = true));
|
||||||
|
clickOutside(node);
|
||||||
|
const event = new MouseEvent('click', { bubbles: true, cancelable: true });
|
||||||
|
event.preventDefault();
|
||||||
|
outside.dispatchEvent(event);
|
||||||
|
expect(fired).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it('removes the listener on destroy', () => {
|
it('removes the listener on destroy', () => {
|
||||||
const node = makeNode();
|
const node = makeNode();
|
||||||
const outside = makeNode();
|
const outside = makeNode();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export function clickOutside(node: HTMLElement): { destroy: () => void } {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capture phase (true) ensures this fires before any child stopPropagation() calls.
|
||||||
document.addEventListener('click', handleClick, true);
|
document.addEventListener('click', handleClick, true);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user