From 5bb4a9444474174b8c7d57cf7ac0f3fb4e4870fe Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Wed, 14 Jun 2023 15:50:04 +0200 Subject: [PATCH] Added signup form attribution tests no issue Added some tests for the signup form attribution when on an external site or on the Ghost site. --- .../signup-form/test/e2e/attribution.test.ts | 46 ++++++++++++++++++- ghost/signup-form/test/utils/e2e.ts | 4 +- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/ghost/signup-form/test/e2e/attribution.test.ts b/ghost/signup-form/test/e2e/attribution.test.ts index ec1d364728..9fdac68469 100644 --- a/ghost/signup-form/test/e2e/attribution.test.ts +++ b/ghost/signup-form/test/e2e/attribution.test.ts @@ -2,8 +2,12 @@ import {expect} from '@playwright/test'; import {initialize} from '../utils/e2e'; import {test} from '@playwright/test'; -async function testHistory({page, path, referrer, urlHistory}: {page: any, path: string, urlHistory: any[]}) { - const {frame, lastApiRequest} = await initialize({page, title: 'Sign up', path}); +async function testHistory({page, embeddedOnUrl, path, urlHistory, localStorageHistory}: {page: any, embeddedOnUrl?: string, path: string, urlHistory: any[], localStorageHistory?: any[]}) { + const {frame, lastApiRequest} = await initialize({page, title: 'Sign up', embeddedOnUrl, path}); + + await page.evaluate((history) => { + localStorage.setItem('ghost-history', JSON.stringify(history)); + }, localStorageHistory); // Fill out the form const emailInput = frame.getByTestId('input'); @@ -54,5 +58,43 @@ test.describe('Attribution', async () => { ]} ); }); + + test('uses current localStorage history', async ({page}) => { + // Save history as localstorage item 'ghost-history' + const history = [ + {path: '/p/573d2c92-183a-435f-b0e7-34595e1ceae7/', time: 1686667443580, referrerSource: null, referrerMedium: null, referrerUrl: 'http://admin.ghost/'}, + {path: '/', time: 1686748392078, referrerSource: null, referrerMedium: null, referrerUrl: null} + ]; + + await testHistory({ + page, + path: '/my-custom-path/123?ref=ghost', + urlHistory: history, + localStorageHistory: history + }); + }); + + test('does not use current localStorage history if on external site', async ({page}) => { + // Save history as localstorage item 'ghost-history' + const history = [ + {path: '/p/573d2c92-183a-435f-b0e7-34595e1ceae7/', time: 1686667443580, referrerSource: null, referrerMedium: null, referrerUrl: 'http://admin.ghost/'}, + {path: '/', time: 1686748392078, referrerSource: null, referrerMedium: null, referrerUrl: null} + ]; + + await testHistory({ + page, + embeddedOnUrl: 'https://example.com', + path: '/my-custom-path/123?ref=ghost', + urlHistory: [ + { + referrerMedium: 'Embed', + referrerSource: 'example.com', + referrerUrl: 'https://example.com/my-custom-path/123', + time: expect.any(Number) + } + ], + localStorageHistory: history + }); + }); }); diff --git a/ghost/signup-form/test/utils/e2e.ts b/ghost/signup-form/test/utils/e2e.ts index 06cc9b9fb6..34400c72e5 100644 --- a/ghost/signup-form/test/utils/e2e.ts +++ b/ghost/signup-form/test/utils/e2e.ts @@ -7,8 +7,8 @@ type LastApiRequest = { body: null | any }; -export async function initialize({page, path, apiStatus, ...options}: {page: Page, path?: string; title?: string, description?: string, icon?: string, backgroundColor?: string, buttonColor?: string, site?: string, 'label-1'?: string, 'label-2'?: string, apiStatus?: number}) { - const sitePath = `${MOCKED_SITE_URL}${path ?? ''}`; +export async function initialize({page, path, apiStatus, embeddedOnUrl, ...options}: {page: Page, embeddedOnUrl?: string, path?: string; title?: string, description?: string, icon?: string, backgroundColor?: string, buttonColor?: string, site?: string, 'label-1'?: string, 'label-2'?: string, apiStatus?: number}) { + const sitePath = `${embeddedOnUrl ?? MOCKED_SITE_URL}${path ?? ''}`; await page.route(sitePath, async (route) => { await route.fulfill({ status: 200,