|
| 1 | +import { expect, test } from '../utils/posthog-playwright-test-base' |
| 2 | +import { start, gotoPage } from '../utils/setup' |
| 3 | +import { |
| 4 | + createTour, |
| 5 | + startOptionsWithProductTours, |
| 6 | + startWithTours, |
| 7 | + tourTooltip, |
| 8 | + tourContainer, |
| 9 | + LAST_SEEN_TOUR_DATE_KEY_PREFIX, |
| 10 | +} from './utils' |
| 11 | + |
| 12 | +test.describe('product tours - wait period', () => { |
| 13 | + test('shows tour when no seenTourWaitPeriod is configured', async ({ page, context }) => { |
| 14 | + const tour = createTour({ id: 'no-wait-period', tour_type: 'tour' }) |
| 15 | + await startWithTours(page, context, [tour]) |
| 16 | + |
| 17 | + await expect(tourTooltip(page, 'no-wait-period')).toBeVisible({ timeout: 5000 }) |
| 18 | + }) |
| 19 | + |
| 20 | + test('blocks tour when within wait period for matching type', async ({ page, context }) => { |
| 21 | + // Navigate first to establish localStorage |
| 22 | + await gotoPage(page, './playground/cypress/index.html') |
| 23 | + |
| 24 | + // Seed a recent seen date for 'tour' type (seen today) |
| 25 | + await page.evaluate(({ key, value }: { key: string; value: string }) => localStorage.setItem(key, value), { |
| 26 | + key: `${LAST_SEEN_TOUR_DATE_KEY_PREFIX}tour`, |
| 27 | + value: JSON.stringify(new Date().toISOString()), |
| 28 | + }) |
| 29 | + |
| 30 | + const tour = createTour({ |
| 31 | + id: 'wait-blocked', |
| 32 | + tour_type: 'tour', |
| 33 | + conditions: { |
| 34 | + seenTourWaitPeriod: { days: 7, types: ['tour'] }, |
| 35 | + }, |
| 36 | + }) |
| 37 | + |
| 38 | + await startWithTours(page, context, [tour], { |
| 39 | + startOptions: { ...startOptionsWithProductTours, type: 'reload' }, |
| 40 | + }) |
| 41 | + |
| 42 | + await page.waitForTimeout(2000) |
| 43 | + await expect(tourTooltip(page, 'wait-blocked')).not.toBeVisible() |
| 44 | + }) |
| 45 | + |
| 46 | + test('shows tour when wait period has passed', async ({ page, context }) => { |
| 47 | + // Navigate first to establish localStorage |
| 48 | + await gotoPage(page, './playground/cypress/index.html') |
| 49 | + |
| 50 | + // Seed a seen date from 10 days ago (past the 7-day wait period) |
| 51 | + await page.evaluate(({ key, value }: { key: string; value: string }) => localStorage.setItem(key, value), { |
| 52 | + key: `${LAST_SEEN_TOUR_DATE_KEY_PREFIX}tour`, |
| 53 | + value: JSON.stringify(new Date(Date.now() - 10 * 24 * 3600 * 1000).toISOString()), |
| 54 | + }) |
| 55 | + |
| 56 | + const tour = createTour({ |
| 57 | + id: 'wait-passed', |
| 58 | + tour_type: 'tour', |
| 59 | + conditions: { |
| 60 | + seenTourWaitPeriod: { days: 7, types: ['tour'] }, |
| 61 | + }, |
| 62 | + }) |
| 63 | + |
| 64 | + await startWithTours(page, context, [tour], { |
| 65 | + startOptions: { ...startOptionsWithProductTours, type: 'reload' }, |
| 66 | + }) |
| 67 | + |
| 68 | + await expect(tourTooltip(page, 'wait-passed')).toBeVisible({ timeout: 5000 }) |
| 69 | + }) |
| 70 | + |
| 71 | + test('shows tour when wait period type does not match stored type', async ({ page, context }) => { |
| 72 | + // Navigate first to establish localStorage |
| 73 | + await gotoPage(page, './playground/cypress/index.html') |
| 74 | + |
| 75 | + // Seed a recent seen date for 'announcement' type |
| 76 | + await page.evaluate(({ key, value }: { key: string; value: string }) => localStorage.setItem(key, value), { |
| 77 | + key: `${LAST_SEEN_TOUR_DATE_KEY_PREFIX}announcement`, |
| 78 | + value: JSON.stringify(new Date().toISOString()), |
| 79 | + }) |
| 80 | + |
| 81 | + // Tour with wait period only checking 'tour' type — should NOT be blocked by 'announcement' |
| 82 | + const tour = createTour({ |
| 83 | + id: 'wait-type-mismatch', |
| 84 | + tour_type: 'tour', |
| 85 | + conditions: { |
| 86 | + seenTourWaitPeriod: { days: 7, types: ['tour'] }, |
| 87 | + }, |
| 88 | + }) |
| 89 | + |
| 90 | + await startWithTours(page, context, [tour], { |
| 91 | + startOptions: { ...startOptionsWithProductTours, type: 'reload' }, |
| 92 | + }) |
| 93 | + |
| 94 | + await expect(tourTooltip(page, 'wait-type-mismatch')).toBeVisible({ timeout: 5000 }) |
| 95 | + }) |
| 96 | + |
| 97 | + test('blocks tour when any of the configured types was seen recently', async ({ page, context }) => { |
| 98 | + // Navigate first to establish localStorage |
| 99 | + await gotoPage(page, './playground/cypress/index.html') |
| 100 | + |
| 101 | + // 'tour' was seen long ago, but 'announcement' was seen today |
| 102 | + await page.evaluate( |
| 103 | + ({ prefix }: { prefix: string }) => { |
| 104 | + const oldDate = new Date(Date.now() - 10 * 24 * 3600 * 1000).toISOString() |
| 105 | + const recentDate = new Date().toISOString() |
| 106 | + localStorage.setItem(`${prefix}tour`, JSON.stringify(oldDate)) |
| 107 | + localStorage.setItem(`${prefix}announcement`, JSON.stringify(recentDate)) |
| 108 | + }, |
| 109 | + { prefix: LAST_SEEN_TOUR_DATE_KEY_PREFIX } |
| 110 | + ) |
| 111 | + |
| 112 | + const tour = createTour({ |
| 113 | + id: 'wait-multi-type', |
| 114 | + tour_type: 'tour', |
| 115 | + conditions: { |
| 116 | + seenTourWaitPeriod: { days: 7, types: ['tour', 'announcement'] }, |
| 117 | + }, |
| 118 | + }) |
| 119 | + |
| 120 | + await startWithTours(page, context, [tour], { |
| 121 | + startOptions: { ...startOptionsWithProductTours, type: 'reload' }, |
| 122 | + }) |
| 123 | + |
| 124 | + await page.waitForTimeout(2000) |
| 125 | + await expect(tourTooltip(page, 'wait-multi-type')).not.toBeVisible() |
| 126 | + }) |
| 127 | + |
| 128 | + test('showing a tour stores the last seen date for its tour_type', async ({ page, context }) => { |
| 129 | + const tour = createTour({ id: 'stores-date', tour_type: 'announcement' }) |
| 130 | + await startWithTours(page, context, [tour]) |
| 131 | + |
| 132 | + await expect(tourTooltip(page, 'stores-date')).toBeVisible({ timeout: 5000 }) |
| 133 | + |
| 134 | + const storedValue = await page.evaluate( |
| 135 | + (key: string) => localStorage.getItem(key), |
| 136 | + `${LAST_SEEN_TOUR_DATE_KEY_PREFIX}announcement` |
| 137 | + ) |
| 138 | + |
| 139 | + expect(storedValue).toBeTruthy() |
| 140 | + const parsed = new Date(JSON.parse(storedValue!)) |
| 141 | + expect(parsed.getTime()).toBeGreaterThan(Date.now() - 60_000) // within last minute |
| 142 | + }) |
| 143 | + |
| 144 | + test('second tour blocked by wait period after first tour shown', async ({ page, context }) => { |
| 145 | + // Show first tour (type 'tour'), which sets the last seen date |
| 146 | + const firstTour = createTour({ id: 'first-tour', tour_type: 'tour' }) |
| 147 | + await startWithTours(page, context, [firstTour]) |
| 148 | + |
| 149 | + const firstTooltip = tourTooltip(page, 'first-tour') |
| 150 | + await expect(firstTooltip).toBeVisible({ timeout: 5000 }) |
| 151 | + |
| 152 | + // Dismiss the first tour |
| 153 | + await tourContainer(page, 'first-tour').locator('.ph-tour-dismiss').click() |
| 154 | + await expect(firstTooltip).not.toBeVisible() |
| 155 | + |
| 156 | + // Now reload with a second tour that has a wait period checking 'tour' type |
| 157 | + const secondTour = createTour({ |
| 158 | + id: 'second-tour', |
| 159 | + tour_type: 'tour', |
| 160 | + conditions: { |
| 161 | + seenTourWaitPeriod: { days: 7, types: ['tour'] }, |
| 162 | + }, |
| 163 | + }) |
| 164 | + |
| 165 | + await page.route('**/api/product_tours/**', async (route) => { |
| 166 | + await route.fulfill({ json: { product_tours: [secondTour] } }) |
| 167 | + }) |
| 168 | + |
| 169 | + await page.reload() |
| 170 | + await start({ ...startOptionsWithProductTours, type: 'reload' }, page, context) |
| 171 | + |
| 172 | + await page.waitForTimeout(2000) |
| 173 | + await expect(tourTooltip(page, 'second-tour')).not.toBeVisible() |
| 174 | + }) |
| 175 | +}) |
0 commit comments