file-downloads.spec.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const { mockRequest, expectCustomEvent, mockManyRequests, metaKey } = require('./support/test-utils');
  2. const { expect, test } = require('@playwright/test');
  3. const { LOCAL_SERVER_ADDR } = require('./support/server');
  4. test.describe('file-downloads extension', () => {
  5. test('sends event and does not start download when link opens in new tab', async ({ page }) => {
  6. await page.goto('/file-download.html')
  7. const downloadURL = await page.locator('#link').getAttribute('href')
  8. const plausibleRequestMock = mockRequest(page, '/api/event')
  9. const downloadRequestMock = mockRequest(page, downloadURL)
  10. await page.click('#link', { modifiers: [metaKey()] })
  11. expectCustomEvent(await plausibleRequestMock, 'File Download', { url: downloadURL })
  12. expect(await downloadRequestMock, "should not make download request").toBeNull()
  13. });
  14. test('sends event and starts download when link child is clicked', async ({ page }) => {
  15. await page.goto('/file-download.html')
  16. const downloadURL = await page.locator('#link').getAttribute('href')
  17. const plausibleRequestMock = mockRequest(page, '/api/event')
  18. const downloadRequestMock = mockRequest(page, downloadURL)
  19. await page.click('#link-child')
  20. expectCustomEvent(await plausibleRequestMock, 'File Download', { url: downloadURL })
  21. expect((await downloadRequestMock).url()).toContain(downloadURL)
  22. });
  23. test('sends File Download event with query-stripped url property', async ({ page }) => {
  24. await page.goto('/file-download.html')
  25. const downloadURL = await page.locator('#link-query').getAttribute('href')
  26. const plausibleRequestMock = mockRequest(page, '/api/event')
  27. await page.click('#link-query')
  28. const expectedURL = downloadURL.split("?")[0]
  29. expectCustomEvent(await plausibleRequestMock, 'File Download', { url: expectedURL })
  30. });
  31. test('starts download only once', async ({ page }) => {
  32. await page.goto('/file-download.html')
  33. const downloadURL = LOCAL_SERVER_ADDR + '/' + await page.locator('#local-download').getAttribute('href')
  34. const downloadRequestMockList = mockManyRequests(page, downloadURL, 2)
  35. await page.click('#local-download')
  36. expect((await downloadRequestMockList).length).toBe(1)
  37. });
  38. });