2023-03-02 13:04:01 +03:00
|
|
|
const { mockRequest, expectCustomEvent, mockManyRequests, metaKey } = require('./support/test-utils');
|
|
|
|
const { expect, test } = require('@playwright/test');
|
2022-09-29 14:12:35 +03:00
|
|
|
const { LOCAL_SERVER_ADDR } = require('./support/server');
|
|
|
|
|
2022-08-04 11:50:09 +03:00
|
|
|
|
|
|
|
test.describe('file-downloads extension', () => {
|
2023-03-02 13:04:01 +03:00
|
|
|
test('sends event and does not start download when link opens in new tab', async ({ page }) => {
|
2022-08-04 11:50:09 +03:00
|
|
|
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)
|
2023-03-02 13:04:01 +03:00
|
|
|
await page.click('#link', { modifiers: [metaKey()] })
|
2022-08-04 11:50:09 +03:00
|
|
|
|
|
|
|
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 })
|
|
|
|
});
|
2022-09-29 14:12:35 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
});
|
2022-08-04 11:50:09 +03:00
|
|
|
});
|