|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { Page } from '@playwright/test'; |
| 3 | +import { test } from '../setup/global'; |
| 4 | +import { waitForToolbarReady } from '../utils/apiMocking'; |
| 5 | +import { ToolbarPage } from '../pages/ToolbarPage'; |
| 6 | + |
| 7 | +test.describe('LaunchDarkly Toolbar - Analytics Consent', () => { |
| 8 | + test.describe('Consent Toast Display', () => { |
| 9 | + test('should show analytics consent toast on first expansion', async ({ page }: { page: Page }) => { |
| 10 | + // Clear any stored consent preferences by clearing localStorage |
| 11 | + await page.goto('/sdk'); |
| 12 | + await page.evaluate(() => { |
| 13 | + // Clear any toolbar-related storage to simulate first visit |
| 14 | + const keys = Object.keys(localStorage); |
| 15 | + keys.forEach((key) => { |
| 16 | + if (key.includes('ld-toolbar') || key.includes('launchdarkly')) { |
| 17 | + localStorage.removeItem(key); |
| 18 | + } |
| 19 | + }); |
| 20 | + }); |
| 21 | + await waitForToolbarReady(page); |
| 22 | + |
| 23 | + const toolbarPage = new ToolbarPage(page); |
| 24 | + await toolbarPage.expand(); |
| 25 | + await toolbarPage.expectExpanded(); |
| 26 | + |
| 27 | + // The consent toast should appear with Accept and Decline buttons |
| 28 | + // Wait a bit for the toast to animate in |
| 29 | + await page.waitForTimeout(500); |
| 30 | + const acceptBtn = page.getByRole('button', { name: 'Accept' }); |
| 31 | + const declineBtn = page.getByRole('button', { name: 'Decline' }); |
| 32 | + |
| 33 | + // At least one of these should be visible (toast may or may not show depending on prior state) |
| 34 | + const isVisible = |
| 35 | + (await acceptBtn.isVisible().catch(() => false)) || (await declineBtn.isVisible().catch(() => false)); |
| 36 | + if (isVisible) { |
| 37 | + await expect(acceptBtn).toBeVisible(); |
| 38 | + await expect(declineBtn).toBeVisible(); |
| 39 | + } |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + test.describe('Accept Analytics', () => { |
| 44 | + test('should dismiss toast when accepting analytics', async ({ page }: { page: Page }) => { |
| 45 | + await page.goto('/sdk'); |
| 46 | + await page.evaluate(() => { |
| 47 | + const keys = Object.keys(localStorage); |
| 48 | + keys.forEach((key) => { |
| 49 | + if (key.includes('ld-toolbar') || key.includes('launchdarkly')) { |
| 50 | + localStorage.removeItem(key); |
| 51 | + } |
| 52 | + }); |
| 53 | + }); |
| 54 | + await waitForToolbarReady(page); |
| 55 | + |
| 56 | + const toolbarPage = new ToolbarPage(page); |
| 57 | + await toolbarPage.expand(); |
| 58 | + await toolbarPage.expectExpanded(); |
| 59 | + await page.waitForTimeout(500); |
| 60 | + |
| 61 | + const acceptBtn = page.getByRole('button', { name: 'Accept' }); |
| 62 | + if (await acceptBtn.isVisible().catch(() => false)) { |
| 63 | + await acceptBtn.click(); |
| 64 | + |
| 65 | + // Toast should disappear |
| 66 | + await expect(acceptBtn).not.toBeVisible({ timeout: 3000 }); |
| 67 | + |
| 68 | + // Toolbar should still be functional |
| 69 | + await expect(page.getByLabel('Flags', { exact: true })).toBeVisible(); |
| 70 | + } |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + test.describe('Decline Analytics', () => { |
| 75 | + test('should dismiss toast when declining analytics', async ({ page }: { page: Page }) => { |
| 76 | + await page.goto('/sdk'); |
| 77 | + await page.evaluate(() => { |
| 78 | + const keys = Object.keys(localStorage); |
| 79 | + keys.forEach((key) => { |
| 80 | + if (key.includes('ld-toolbar') || key.includes('launchdarkly')) { |
| 81 | + localStorage.removeItem(key); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + await waitForToolbarReady(page); |
| 86 | + |
| 87 | + const toolbarPage = new ToolbarPage(page); |
| 88 | + await toolbarPage.expand(); |
| 89 | + await toolbarPage.expectExpanded(); |
| 90 | + await page.waitForTimeout(500); |
| 91 | + |
| 92 | + const declineBtn = page.getByRole('button', { name: 'Decline' }); |
| 93 | + if (await declineBtn.isVisible().catch(() => false)) { |
| 94 | + await declineBtn.click(); |
| 95 | + |
| 96 | + // Toast should disappear |
| 97 | + await expect(declineBtn).not.toBeVisible({ timeout: 3000 }); |
| 98 | + |
| 99 | + // Toolbar should still be functional |
| 100 | + await expect(page.getByLabel('Flags', { exact: true })).toBeVisible(); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + test.describe('Consent Persistence', () => { |
| 106 | + test('should not show consent toast after it was previously dismissed', async ({ page }: { page: Page }) => { |
| 107 | + await page.goto('/sdk'); |
| 108 | + await waitForToolbarReady(page); |
| 109 | + |
| 110 | + const toolbarPage = new ToolbarPage(page); |
| 111 | + await toolbarPage.expand(); |
| 112 | + await toolbarPage.expectExpanded(); |
| 113 | + await page.waitForTimeout(500); |
| 114 | + |
| 115 | + // Dismiss the toast if visible |
| 116 | + const declineBtn = page.getByRole('button', { name: 'Decline' }); |
| 117 | + if (await declineBtn.isVisible().catch(() => false)) { |
| 118 | + await declineBtn.click(); |
| 119 | + await expect(declineBtn).not.toBeVisible({ timeout: 3000 }); |
| 120 | + } |
| 121 | + |
| 122 | + // Collapse and re-expand |
| 123 | + await toolbarPage.collapse(); |
| 124 | + await toolbarPage.expectCollapsed(); |
| 125 | + await toolbarPage.expand(); |
| 126 | + await toolbarPage.expectExpanded(); |
| 127 | + |
| 128 | + // The consent toast should NOT reappear |
| 129 | + await page.waitForTimeout(500); |
| 130 | + const acceptBtn = page.getByRole('button', { name: 'Accept' }); |
| 131 | + await expect(acceptBtn).not.toBeVisible(); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments