analytics/tracker/test/outbound-links.spec.js
RobertJoonas f75d5106f0
Rework outbound links and file downloads (#2208)
* moved custom event code to the bottom + fix indentation

* add handlebars helper fn + extract getLinkEl fn

* extract isOutboundLink function

* extract shouldFollowLink function

* remove middle and click variables

* use only one click handler for outbounds and downloads

* extract sendLinkClickEvent function

* add error handling when script compilation fails

* use callback instead of fixed timeout

* do not prevent default if externally prevented + test

* add more tests

* generate tracker files in priv/tracker/js

* update changelog

* requested changes after review

* regenerate tracker files

* use return instead of else if

* move middleMouseButton outside the function
2022-09-29 14:12:35 +03:00

47 lines
2.0 KiB
JavaScript

const { test } = require('./support/harness')
const { mockRequest, isMac, expectCustomEvent } = require('./support/test-utils')
const { expect } = require('@playwright/test');
test.describe('outbound-links extension', () => {
test('sends event and does not navigate when link opens in new tab', async ({ page }, workerInfo) => {
await page.goto('/outbound-link.html')
const outboundURL = await page.locator('#link').getAttribute('href')
const plausibleRequestMock = mockRequest(page, '/api/event')
const navigationRequestMock = mockRequest(page, outboundURL)
await page.click('#link', { modifiers: [isMac(workerInfo) ? 'Meta' : 'Control'] })
expectCustomEvent(await plausibleRequestMock, 'Outbound Link: Click', { url: outboundURL })
expect(await navigationRequestMock, "should not have made navigation request").toBeNull()
});
test('sends event and navigates to target when link child is clicked', async ({ page }) => {
await page.goto('/outbound-link.html')
const outboundURL = await page.locator('#link').getAttribute('href')
const plausibleRequestMock = mockRequest(page, '/api/event')
const navigationRequestMock = mockRequest(page, outboundURL)
await page.click('#link-child')
const navigationRequest = await navigationRequestMock
expectCustomEvent(await plausibleRequestMock, 'Outbound Link: Click', { url: outboundURL })
expect(navigationRequest.url()).toContain(outboundURL)
});
test('sends event and does not navigate if default externally prevented', async ({ page }) => {
await page.goto('/outbound-link.html')
const outboundURL = await page.locator('#link-default-prevented').getAttribute('href')
const plausibleRequestMock = mockRequest(page, '/api/event')
const navigationRequestMock = mockRequest(page, outboundURL)
await page.click('#link-default-prevented')
expectCustomEvent(await plausibleRequestMock, 'Outbound Link: Click', { url: outboundURL })
expect(await navigationRequestMock, "should not have made navigation request").toBeNull()
});
});