Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Nov 19, 2023
1 parent 38d7e46 commit 2ea406b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 50 deletions.
52 changes: 8 additions & 44 deletions src/__tests__/autocapture-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getDirectAndNestedSpanText,
} from '../autocapture-utils'
import { document } from '../utils/globals'
import { makeMouseEvent } from './autocapture.test'

describe(`Autocapture utility functions`, () => {
afterEach(() => {
Expand Down Expand Up @@ -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)
})
})

Expand All @@ -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 <form> 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)
})
})
})
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/autocapture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function makePostHog(ph: Partial<PostHog>): PostHog {
} as unknown as PostHog
}

function makeMouseEvent(me: Partial<MouseEvent>) {
return { type: 'click', ...me } as unknown as MouseEvent
export function makeMouseEvent(partialEvent: Partial<MouseEvent>) {
return { type: 'click', ...partialEvent } as unknown as MouseEvent
}

describe('Autocapture system', () => {
Expand Down Expand Up @@ -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)
})

Expand Down

0 comments on commit 2ea406b

Please sign in to comment.