Skip to content

Commit

Permalink
chore: add tests on impact of empty autocapture config
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Sep 15, 2023
1 parent 632530e commit 8795f1d
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/__tests__/autocapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ describe('Autocapture system', () => {
expect(props['attr__data-attr-2']).toBeUndefined()
expect(props['attr__data-attr-3']).toBe('value')
})

it('an empty ignorelist does nothing', () => {
autocapture.config = {
element_attribute_ignorelist: [],
}
div.setAttribute('data-attr', 'value')
div.setAttribute('data-attr-2', 'value')
div.setAttribute('data-attr-3', 'value')
const props = autocapture._getPropertiesFromElement(div)
expect(props['attr__data-attr']).toBe('value')
expect(props['attr__data-attr-2']).toBe('value')
expect(props['attr__data-attr-3']).toBe('value')
})
})

describe('_getAugmentPropertiesFromElement', () => {
Expand Down Expand Up @@ -1249,6 +1262,26 @@ describe('Autocapture system', () => {
expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(false)
})

it('an empty url regex allowlist does not match any url', () => {
var main_el = document.createElement('some-element')
var button = document.createElement('a')
button.innerHTML = 'bla'
main_el.appendChild(button)
const e = {
target: main_el,
composedPath: () => [button, main_el],
type: 'click',
}
const autocapture_config = {
url_allowlist: [],
}

delete window.location
window.location = new URL('https://posthog.com/test/captured')

expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(false)
})

it('only capture event types which match the allowlist', () => {
var main_el = document.createElement('some-element')
var button = document.createElement('button')
Expand All @@ -1270,6 +1303,22 @@ describe('Autocapture system', () => {
expect(shouldCaptureDomEvent(button, e, autocapture_config_change)).toBe(false)
})

it('an empty event type allowlist matches no events', () => {
var main_el = document.createElement('some-element')
var button = document.createElement('button')
button.innerHTML = 'bla'
main_el.appendChild(button)
const e = {
target: main_el,
composedPath: () => [button, main_el],
type: 'click',
}
const autocapture_config = {
dom_event_allowlist: [],
}
expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(false)
})

it('only capture elements which match the allowlist', () => {
var main_el = document.createElement('some-element')
var button = document.createElement('button')
Expand All @@ -1291,6 +1340,22 @@ describe('Autocapture system', () => {
expect(shouldCaptureDomEvent(button, e, autocapture_config_change)).toBe(false)
})

it('an empty event allowlist means we capture no elements', () => {
var main_el = document.createElement('some-element')
var button = document.createElement('button')
button.innerHTML = 'bla'
main_el.appendChild(button)
const e = {
target: main_el,
composedPath: () => [button, main_el],
type: 'click',
}
const autocapture_config = {
element_allowlist: [],
}
expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(false)
})

it('only capture elements which match the css allowlist', () => {
var main_el = document.createElement('some-element')
var button = document.createElement('button')
Expand All @@ -1312,5 +1377,22 @@ describe('Autocapture system', () => {
}
expect(shouldCaptureDomEvent(button, e, autocapture_config_change)).toBe(false)
})

it('an empty css selector list captures no elements', () => {
var main_el = document.createElement('some-element')
var button = document.createElement('button')
button.setAttribute('data-track', 'yes')
button.innerHTML = 'bla'
main_el.appendChild(button)
const e = {
target: main_el,
composedPath: () => [button, main_el],
type: 'click',
}
const autocapture_config = {
css_selector_allowlist: [],
}
expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(false)
})
})
})

0 comments on commit 8795f1d

Please sign in to comment.