From 2ea406b2c8a795f563fcbd826e12d1d3db396f0d Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Sun, 19 Nov 2023 21:00:06 +0000 Subject: [PATCH] fix --- src/__tests__/autocapture-utils.test.ts | 52 ++++--------------------- src/__tests__/autocapture.test.ts | 12 +++--- 2 files changed, 14 insertions(+), 50 deletions(-) diff --git a/src/__tests__/autocapture-utils.test.ts b/src/__tests__/autocapture-utils.test.ts index 35aa20a3d..162c7dbf2 100644 --- a/src/__tests__/autocapture-utils.test.ts +++ b/src/__tests__/autocapture-utils.test.ts @@ -13,6 +13,7 @@ import { getDirectAndNestedSpanText, } from '../autocapture-utils' import { document } from '../utils/globals' +import { makeMouseEvent } from './autocapture.test' describe(`Autocapture utility functions`, () => { afterEach(() => { @@ -126,11 +127,7 @@ describe(`Autocapture utility functions`, () => { // [`div`, `sPan`, `A`, `strong`, `table`] ;['a'].forEach((tagName) => { it(`should capture "click" events on <` + tagName.toLowerCase() + `> elements`, () => { - expect( - shouldCaptureDomEvent(document!.createElement(tagName), { - type: `click`, - } as unknown as Event) - ).toBe(true) + expect(shouldCaptureDomEvent(document!.createElement(tagName), makeMouseEvent({}))).toBe(true) }) }) @@ -141,55 +138,22 @@ describe(`Autocapture utility functions`, () => { const button3 = document!.createElement(`input`) button3.setAttribute(`type`, `submit`) ;[button1, button2, button3].forEach((button) => { - expect( - shouldCaptureDomEvent(button, { - type: `click`, - } as unknown as Event) - ).toBe(true) + expect(shouldCaptureDomEvent(button, makeMouseEvent({}))).toBe(true) }) }) it(`should protect against bad inputs`, () => { - expect( - shouldCaptureDomEvent( - null as unknown as Element, - { - type: `click`, - } as unknown as Event - ) - ).toBe(false) - expect( - shouldCaptureDomEvent( - undefined as unknown as Element, - { - type: `click`, - } as unknown as Event - ) - ).toBe(false) - expect( - shouldCaptureDomEvent( - `div` as unknown as Element, - { - type: `click`, - } as unknown as Event - ) - ).toBe(false) + expect(shouldCaptureDomEvent(null as unknown as Element, makeMouseEvent({}))).toBe(false) + expect(shouldCaptureDomEvent(undefined as unknown as Element, makeMouseEvent({}))).toBe(false) + expect(shouldCaptureDomEvent(`div` as unknown as Element, makeMouseEvent({}))).toBe(false) }) it(`should NOT capture "click" events on
elements`, () => { - expect( - shouldCaptureDomEvent(document!.createElement(`form`), { - type: `click`, - } as unknown as Event) - ).toBe(false) + expect(shouldCaptureDomEvent(document!.createElement(`form`), makeMouseEvent({}))).toBe(false) }) ;[`html`].forEach((tagName) => { it(`should NOT capture "click" events on <` + tagName.toLowerCase() + `> elements`, () => { - expect( - shouldCaptureDomEvent(document!.createElement(tagName), { - type: `click`, - } as unknown as Event) - ).toBe(false) + expect(shouldCaptureDomEvent(document!.createElement(tagName), makeMouseEvent({}))).toBe(false) }) }) }) diff --git a/src/__tests__/autocapture.test.ts b/src/__tests__/autocapture.test.ts index cc45553ce..07b21e818 100644 --- a/src/__tests__/autocapture.test.ts +++ b/src/__tests__/autocapture.test.ts @@ -32,8 +32,8 @@ function makePostHog(ph: Partial): PostHog { } as unknown as PostHog } -function makeMouseEvent(me: Partial) { - return { type: 'click', ...me } as unknown as MouseEvent +export function makeMouseEvent(partialEvent: Partial) { + return { type: 'click', ...partialEvent } as unknown as MouseEvent } describe('Autocapture system', () => { @@ -1005,19 +1005,19 @@ describe('Autocapture system', () => { const a = document.createElement('a') const span = document.createElement('span') a.appendChild(span) - autocapture._captureEvent(makeMouseEvent({ target: a, type: 'click' }), lib) + autocapture._captureEvent(makeMouseEvent({ target: a }), lib) expect((lib.capture as sinon.SinonSpy).calledOnce).toBe(true) ;(lib.capture as sinon.SinonSpy).resetHistory() - autocapture._captureEvent(makeMouseEvent({ target: span, type: 'click' }), lib) + autocapture._captureEvent(makeMouseEvent({ target: span }), lib) expect((lib.capture as sinon.SinonSpy).calledOnce).toBe(true) ;(lib.capture as sinon.SinonSpy).resetHistory() a.className = 'test1 ph-no-capture test2' - autocapture._captureEvent(makeMouseEvent({ target: a, type: 'click' }), lib) + autocapture._captureEvent(makeMouseEvent({ target: a }), lib) expect((lib.capture as sinon.SinonSpy).callCount).toBe(0) - autocapture._captureEvent(makeMouseEvent({ target: span, type: 'click' }), lib) + autocapture._captureEvent(makeMouseEvent({ target: span }), lib) expect((lib.capture as sinon.SinonSpy).callCount).toBe(0) })