Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove complex get_config #812

Merged
merged 8 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 51 additions & 70 deletions src/__tests__/autocapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,10 @@ describe('Autocapture system', () => {
return 'distinctid'
},
capture: sandbox.spy(),
get_config: sandbox.spy(function (key) {
switch (key) {
case 'mask_all_element_attributes':
return false
case 'rageclick':
return true
}
}),
config: {
mask_all_element_attributes: false,
rageclick: true,
},
}
})

Expand All @@ -506,18 +502,12 @@ describe('Autocapture system', () => {
it('should add the custom property when an element matching any of the event selectors is clicked', () => {
lib = {
_prepare_callback: sandbox.spy((callback) => callback),
get_config: sandbox.spy(function (key) {
switch (key) {
case 'api_host':
return 'https://test.com'
case 'token':
return 'testtoken'
case 'mask_all_element_attributes':
return false
case 'autocapture':
return true
}
}),
config: {
api_host: 'https://test.com',
token: 'testtoken',
mask_all_element_attributes: false,
autocapture: true,
},
token: 'testtoken',
capture: sandbox.spy(),
get_distinct_id() {
Expand Down Expand Up @@ -614,21 +604,15 @@ describe('Autocapture system', () => {
expect(captureProperties).toHaveProperty('parent-augment', 'the parent')
})

it('should not capture events when get_config returns false, when an element matching any of the event selectors is clicked', () => {
it('should not capture events when config returns false, when an element matching any of the event selectors is clicked', () => {
lib = {
_prepare_callback: sandbox.spy((callback) => callback),
get_config: sandbox.spy(function (key) {
switch (key) {
case 'api_host':
return 'https://test.com'
case 'token':
return 'testtoken'
case 'mask_all_element_attributes':
return false
case 'autocapture':
return false
}
}),
config: {
api_host: 'https://test.com',
token: 'testtoken',
mask_all_element_attributes: false,
autocapture: false,
},
token: 'testtoken',
capture: sandbox.spy(),
get_distinct_id() {
Expand Down Expand Up @@ -666,21 +650,15 @@ describe('Autocapture system', () => {
lib.capture.resetHistory()
})

it('should not capture events when get_config returns true but server setting is disabled', () => {
it('should not capture events when config returns true but server setting is disabled', () => {
lib = {
_prepare_callback: sandbox.spy((callback) => callback),
get_config: sandbox.spy(function (key) {
switch (key) {
case 'api_host':
return 'https://test.com'
case 'token':
return 'testtoken'
case 'mask_all_element_attributes':
return false
case 'autocapture':
return true
}
}),
config: {
api_host: 'https://test.com',
token: 'testtoken',
mask_all_element_attributes: false,
autocapture: true,
},
token: 'testtoken',
capture: sandbox.spy(),
get_distinct_id() {
Expand Down Expand Up @@ -1035,7 +1013,13 @@ describe('Autocapture system', () => {
</button>
`

const newLib = { ...lib, get_config: jest.fn(() => true) }
const newLib = {
...lib,
config: {
...lib.config,
mask_all_element_attributes: true,
},
}

document.body.innerHTML = dom
const button1 = document.getElementById('button1')
Expand All @@ -1057,7 +1041,13 @@ describe('Autocapture system', () => {
</a>
`

const newLib = { ...lib, get_config: jest.fn(() => true) }
const newLib = {
...lib,
config: {
...lib.config,
mask_all_text: true,
},
}

document.body.innerHTML = dom
const a = document.getElementById('a1')
Expand All @@ -1075,19 +1065,14 @@ describe('Autocapture system', () => {
})

describe('_addDomEventHandlers', () => {
const sandbox = sinon.createSandbox()

const lib = {
capture: sinon.spy(),
get_distinct_id() {
return 'distinctid'
},
get_config: sandbox.spy(function (key) {
switch (key) {
case 'mask_all_element_attributes':
return false
}
}),
config: {
mask_all_element_attributes: false,
},
}

let navigateSpy
Expand Down Expand Up @@ -1125,7 +1110,11 @@ describe('Autocapture system', () => {
given('persistence', () => ({ props: {}, register: jest.fn() }))

given('posthog', () => ({
get_config: jest.fn().mockImplementation((key) => given.config[key]),
config: {
api_host: 'https://test.com',
token: 'testtoken',
autocapture: true,
},
token: 'testtoken',
capture: jest.fn(),
get_distinct_id: () => 'distinctid',
Expand All @@ -1134,14 +1123,6 @@ describe('Autocapture system', () => {
persistence: given.persistence,
}))

given('clientSideEnabled', () => true)

given('config', () => ({
api_host: 'https://test.com',
token: 'testtoken',
autocapture: given.clientSideEnabled,
}))

given('decideResponse', () => ({ config: { enable_collect_everything: true } }))

beforeEach(() => {
Expand All @@ -1166,7 +1147,7 @@ describe('Autocapture system', () => {
})

it('should be disabled before the decide response if client side opted out', () => {
given('clientSideEnabled', () => false)
given.posthog.config.autocapture = false

// _setIsAutocaptureEnabled is called during init
autocapture._setIsAutocaptureEnabled(given.posthog)
Expand All @@ -1183,7 +1164,7 @@ describe('Autocapture system', () => {
])(
'when client side config is %p and remote opt out is %p - autocapture enabled should be %p',
(clientSideOptIn, serverSideOptOut, expected) => {
given('clientSideEnabled', () => clientSideOptIn)
given.posthog.config.autocapture = clientSideOptIn
given('decideResponse', () => ({
config: { enable_collect_everything: true },
autocapture_opt_out: serverSideOptOut,
Expand All @@ -1201,11 +1182,11 @@ describe('Autocapture system', () => {
})

it('should not call _addDomEventHandlders if autocapture is disabled', () => {
given('config', () => ({
given.posthog.config = {
api_host: 'https://test.com',
token: 'testtoken',
autocapture: false,
}))
}
given('$autocapture_disabled_server_side', () => true)
given.subject()
expect(autocapture._addDomEventHandlers).not.toHaveBeenCalled()
Expand Down Expand Up @@ -1233,7 +1214,7 @@ describe('Autocapture system', () => {
autocapture.afterDecideResponse(given.decideResponse, given.posthog)
expect(autocapture._addDomEventHandlers).toHaveBeenCalledTimes(1)

given('config', () => ({ api_host: 'https://test.com', token: 'anotherproject', autocapture: true }))
given.posthog.config = { api_host: 'https://test.com', token: 'anotherproject', autocapture: true }
autocapture.afterDecideResponse(given.decideResponse, given.posthog)
expect(autocapture._addDomEventHandlers).toHaveBeenCalledTimes(2)
})
Expand Down
12 changes: 4 additions & 8 deletions src/__tests__/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,10 @@ describe('Payload Compression', () => {
throw new Error('Should not get here')
}
}),
get_config: sandbox.spy(function (key) {
switch (key) {
case 'api_host':
return 'https://test.com'
case 'token':
return 'testtoken'
}
}),
config: {
api_host: 'https://test.com',
token: 'testtoken',
},
token: 'testtoken',
get_distinct_id() {
return 'distinctid'
Expand Down
Loading
Loading