analytics/tracker/test/file-downloads.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

53 lines
2.3 KiB
JavaScript

const { test } = require('./support/harness');
const { mockRequest, expectCustomEvent, isMac, mockManyRequests } = require('./support/test-utils');
const { expect } = require('@playwright/test');
const { LOCAL_SERVER_ADDR } = require('./support/server');
test.describe('file-downloads extension', () => {
test('sends event and does not start download when link opens in new tab', async ({ page }, workerInfo) => {
await page.goto('/file-download.html')
const downloadURL = await page.locator('#link').getAttribute('href')
const plausibleRequestMock = mockRequest(page, '/api/event')
const downloadRequestMock = mockRequest(page, downloadURL)
await page.click('#link', { modifiers: [isMac(workerInfo) ? 'Meta' : 'Control'] })
expectCustomEvent(await plausibleRequestMock, 'File Download', { url: downloadURL })
expect(await downloadRequestMock, "should not make download request").toBeNull()
});
test('sends event and starts download when link child is clicked', async ({ page }) => {
await page.goto('/file-download.html')
const downloadURL = await page.locator('#link').getAttribute('href')
const plausibleRequestMock = mockRequest(page, '/api/event')
const downloadRequestMock = mockRequest(page, downloadURL)
await page.click('#link-child')
expectCustomEvent(await plausibleRequestMock, 'File Download', { url: downloadURL })
expect((await downloadRequestMock).url()).toContain(downloadURL)
});
test('sends File Download event with query-stripped url property', async ({ page }) => {
await page.goto('/file-download.html')
const downloadURL = await page.locator('#link-query').getAttribute('href')
const plausibleRequestMock = mockRequest(page, '/api/event')
await page.click('#link-query')
const expectedURL = downloadURL.split("?")[0]
expectCustomEvent(await plausibleRequestMock, 'File Download', { url: expectedURL })
});
test('starts download only once', async ({ page }) => {
await page.goto('/file-download.html')
const downloadURL = LOCAL_SERVER_ADDR + '/' + await page.locator('#local-download').getAttribute('href')
const downloadRequestMockList = mockManyRequests(page, downloadURL, 2)
await page.click('#local-download')
expect((await downloadRequestMockList).length).toBe(1)
});
});