Skip to content

Commit

Permalink
test(e2e): support passing agent options as query params
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnderScorer committed Apr 24, 2023
1 parent cc50148 commit 92f0399
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
57 changes: 41 additions & 16 deletions e2e/tests/integration/visitorId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,54 @@ async function checkResponse(page: Page) {
}

test.describe('visitorId', () => {
test('should show correct visitorId using function endpoints', async ({ page, baseURL }) => {
const rootUrl = new URL(baseURL as string)
interface TestCase {
queryParams?: Record<string, string>
}

const { getRequests } = trackRequests(page)
const testCases: TestCase[] = [
{
queryParams: undefined,
},
{
queryParams: {
// Script pattern without version
scriptUrlPattern: '/fpjs/agent?apiKey=<apiKey>&loaderVersion=<loaderVersion>',
},
},
]

await page.goto('/', {
waitUntil: 'networkidle',
})
testCases.forEach((testCase, index) => {
test(`should show correct visitorId using function endpoints #${index + 1}`, async ({ page, baseURL }) => {
if (testCase.queryParams) {
const queryParams = new URLSearchParams(testCase.queryParams)
await page.goto(`/?${queryParams.toString()}`, {
waitUntil: 'networkidle',
})
}

const rootUrl = new URL(baseURL as string)

const { getRequests } = trackRequests(page)

await page.click('#getData')
await page.goto('/', {
waitUntil: 'networkidle',
})

await checkResponse(page)
await page.click('#getData')

const requests = getRequests()
expect(requests).toHaveLength(6)
await checkResponse(page)

const [, , , agentRequest, , apiRequest] = requests
const requests = getRequests()
expect(requests).toHaveLength(6)

const agentRequestUrl = new URL(agentRequest.url())
expect(agentRequestUrl.hostname).toBe(rootUrl.hostname)
const [, , , agentRequest, , apiRequest] = requests

const apiRequestUrl = new URL(apiRequest.url())
expect(apiRequestUrl.hostname).toBe(rootUrl.hostname)
expect(apiRequestUrl.searchParams.get('ci')).toContain(`js/`)
const agentRequestUrl = new URL(agentRequest.url())
expect(agentRequestUrl.hostname).toBe(rootUrl.hostname)

const apiRequestUrl = new URL(apiRequest.url())
expect(apiRequestUrl.hostname).toBe(rootUrl.hostname)
expect(apiRequestUrl.searchParams.get('ci')).toContain(`js/`)
})
})
})
8 changes: 5 additions & 3 deletions example-website/src/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { LoadOptions } from '@fingerprintjs/fingerprintjs-pro'
export type FingerprintOptions = ReturnType<typeof getOptions>

export function getOptions() {
const search = new URLSearchParams(location.search)

return {
apiKey: import.meta.env.VITE_API_KEY as string,
endpoint: import.meta.env.VITE_ENDPOINT as string,
scriptUrlPattern: import.meta.env.VITE_SCRIPT_URL_PATTERN as string,
apiKey: search.get('apiKey') ?? (import.meta.env.VITE_API_KEY as string),
endpoint: search.get('endpoint') ?? (import.meta.env.VITE_ENDPOINT as string),
scriptUrlPattern: search.get('scriptUrlPattern') ?? (import.meta.env.VITE_SCRIPT_URL_PATTERN as string),
} satisfies LoadOptions
}

Expand Down

0 comments on commit 92f0399

Please sign in to comment.