outbound-links.spec.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const { mockRequest, expectCustomEvent, metaKey } = require('./support/test-utils')
  2. const { expect, test } = require('@playwright/test');
  3. test.describe('outbound-links extension', () => {
  4. test('sends event and does not navigate when link opens in new tab', async ({ page }) => {
  5. await page.goto('/outbound-link.html')
  6. const outboundURL = await page.locator('#link').getAttribute('href')
  7. const plausibleRequestMock = mockRequest(page, '/api/event')
  8. const navigationRequestMock = mockRequest(page, outboundURL)
  9. await page.click('#link', { modifiers: [metaKey()] })
  10. expectCustomEvent(await plausibleRequestMock, 'Outbound Link: Click', { url: outboundURL })
  11. expect(await navigationRequestMock, "should not have made navigation request").toBeNull()
  12. });
  13. test('sends event and navigates to target when link child is clicked', async ({ page }) => {
  14. await page.goto('/outbound-link.html')
  15. const outboundURL = await page.locator('#link').getAttribute('href')
  16. const plausibleRequestMock = mockRequest(page, '/api/event')
  17. const navigationRequestMock = mockRequest(page, outboundURL)
  18. await page.click('#link-child')
  19. const navigationRequest = await navigationRequestMock
  20. expectCustomEvent(await plausibleRequestMock, 'Outbound Link: Click', { url: outboundURL })
  21. expect(navigationRequest.url()).toContain(outboundURL)
  22. });
  23. test('sends event and does not navigate if default externally prevented', async ({ page }) => {
  24. await page.goto('/outbound-link.html')
  25. const outboundURL = await page.locator('#link-default-prevented').getAttribute('href')
  26. const plausibleRequestMock = mockRequest(page, '/api/event')
  27. const navigationRequestMock = mockRequest(page, outboundURL)
  28. await page.click('#link-default-prevented')
  29. expectCustomEvent(await plausibleRequestMock, 'Outbound Link: Click', { url: outboundURL })
  30. expect(await navigationRequestMock, "should not have made navigation request").toBeNull()
  31. });
  32. });