|
| 1 | +/* |
| 2 | + * Copyright 2025 Collate. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import { expect, Page, test } from '@playwright/test'; |
| 15 | +import { redirectToHomePage } from '../../utils/common'; |
| 16 | +import { waitForAllLoadersToDisappear } from '../../utils/entity'; |
| 17 | + |
| 18 | +test.use({ storageState: 'playwright/.auth/admin.json' }); |
| 19 | + |
| 20 | +/** |
| 21 | + * Navigates to MySQL service creation step 3 (configure connection), |
| 22 | + * where the ServiceDocPanel is visible with code blocks and sections. |
| 23 | + */ |
| 24 | +const goToMysqlConnectionStep = async (page: Page, serviceName: string) => { |
| 25 | + await page.goto('/databaseServices/add-service', { |
| 26 | + waitUntil: 'domcontentloaded', |
| 27 | + }); |
| 28 | + await waitForAllLoadersToDisappear(page); |
| 29 | + await page.getByTestId('Mysql').click(); |
| 30 | + await page.getByTestId('next-button').click(); |
| 31 | + await page.getByTestId('service-name').fill(serviceName); |
| 32 | + await page.getByTestId('next-button').click(); |
| 33 | + await page.getByTestId('service-requirements').waitFor({ state: 'visible' }); |
| 34 | +}; |
| 35 | + |
| 36 | +test.describe('ServiceDocPanel', () => { |
| 37 | + test.beforeEach(async ({ page }) => { |
| 38 | + await redirectToHomePage(page); |
| 39 | + }); |
| 40 | + |
| 41 | + test.describe('Content rendering', () => { |
| 42 | + test('should render headings not raw markdown', async ({ page }) => { |
| 43 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-headings'); |
| 44 | + |
| 45 | + const docPanel = page.getByTestId('service-requirements'); |
| 46 | + |
| 47 | + // Requirements h2 heading should render as an element, not raw "## Requirements" |
| 48 | + await expect(docPanel.locator('h2').first()).toBeVisible(); |
| 49 | + await expect(docPanel).not.toContainText('## Requirements'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should render admonition blocks with correct class', async ({ |
| 53 | + page, |
| 54 | + }) => { |
| 55 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-admonition'); |
| 56 | + |
| 57 | + const docPanel = page.getByTestId('service-requirements'); |
| 58 | + |
| 59 | + // Mysql.md has $$note blocks — should render as .admonition.admonition-note |
| 60 | + const admonition = docPanel.locator('.admonition-note').first(); |
| 61 | + |
| 62 | + await expect(admonition).toBeVisible(); |
| 63 | + // Should contain actual note content, not raw "$$note" syntax |
| 64 | + await expect(docPanel).not.toContainText('$$note'); |
| 65 | + }); |
| 66 | + |
| 67 | + test('should render code blocks inside pre > code, not as raw text', async ({ |
| 68 | + page, |
| 69 | + }) => { |
| 70 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-codeblock'); |
| 71 | + |
| 72 | + const docPanel = page.getByTestId('service-requirements'); |
| 73 | + |
| 74 | + await expect(docPanel.locator('pre code').first()).toBeVisible(); |
| 75 | + // Raw fence markers should not appear |
| 76 | + await expect(docPanel).not.toContainText('```'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should render links that open in a new tab', async ({ page }) => { |
| 80 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-links'); |
| 81 | + |
| 82 | + const docPanel = page.getByTestId('service-requirements'); |
| 83 | + const externalLink = docPanel.locator('a[target="_blank"]').first(); |
| 84 | + |
| 85 | + await expect(externalLink).toBeVisible(); |
| 86 | + await expect(externalLink).toHaveAttribute('href', /^https?:\/\//); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should render image in Mssql doc panel', async ({ page }) => { |
| 90 | + await page.goto('/databaseServices/add-service', { |
| 91 | + waitUntil: 'domcontentloaded', |
| 92 | + }); |
| 93 | + await waitForAllLoadersToDisappear(page); |
| 94 | + await page.getByTestId('Mssql').click(); |
| 95 | + await page.getByTestId('next-button').click(); |
| 96 | + await page.getByTestId('service-name').fill('pw-doc-panel-mssql-img'); |
| 97 | + await page.getByTestId('next-button').click(); |
| 98 | + await page.getByTestId('service-requirements').waitFor({ |
| 99 | + state: 'visible', |
| 100 | + }); |
| 101 | + |
| 102 | + const docPanel = page.getByTestId('service-requirements'); |
| 103 | + const image = docPanel.locator('img').first(); |
| 104 | + |
| 105 | + await expect(image).toBeVisible(); |
| 106 | + // Verify the image loaded successfully (no broken image) |
| 107 | + const naturalWidth = await image.evaluate( |
| 108 | + (img: HTMLImageElement) => img.naturalWidth |
| 109 | + ); |
| 110 | + |
| 111 | + expect(naturalWidth).toBeGreaterThan(0); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + test.describe('Section highlighting', () => { |
| 116 | + test('should highlight section when the corresponding form field is focused', async ({ |
| 117 | + page, |
| 118 | + }) => { |
| 119 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-highlight'); |
| 120 | + |
| 121 | + const docPanel = page.getByTestId('service-requirements'); |
| 122 | + |
| 123 | + // No section should be highlighted initially |
| 124 | + await expect( |
| 125 | + docPanel.locator('section[data-highlighted="true"]') |
| 126 | + ).toHaveCount(0); |
| 127 | + |
| 128 | + // Focus the username field — activeField becomes "username" |
| 129 | + await page.locator(String.raw`#root\/username`).focus(); |
| 130 | + |
| 131 | + // The username section should now be highlighted |
| 132 | + const usernameSection = docPanel.locator( |
| 133 | + 'section[data-id="username"][data-highlighted="true"]' |
| 134 | + ); |
| 135 | + |
| 136 | + await expect(usernameSection).toBeVisible(); |
| 137 | + }); |
| 138 | + |
| 139 | + test('should remove highlight from previous section when a new field is focused', async ({ |
| 140 | + page, |
| 141 | + }) => { |
| 142 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-highlight-switch'); |
| 143 | + |
| 144 | + const docPanel = page.getByTestId('service-requirements'); |
| 145 | + |
| 146 | + // Focus username first |
| 147 | + await page.locator(String.raw`#root\/username`).focus(); |
| 148 | + |
| 149 | + await expect( |
| 150 | + docPanel.locator('section[data-id="username"][data-highlighted="true"]') |
| 151 | + ).toBeVisible(); |
| 152 | + |
| 153 | + // Focus hostPort — username section should lose highlight |
| 154 | + await page.locator(String.raw`#root\/hostPort`).focus(); |
| 155 | + |
| 156 | + await expect( |
| 157 | + docPanel.locator('section[data-id="username"][data-highlighted="true"]') |
| 158 | + ).toHaveCount(0); |
| 159 | + |
| 160 | + // hostPort section should now be highlighted |
| 161 | + await expect( |
| 162 | + docPanel.locator('section[data-id="hostPort"][data-highlighted="true"]') |
| 163 | + ).toBeVisible(); |
| 164 | + }); |
| 165 | + |
| 166 | + test('should only ever have one section highlighted at a time', async ({ |
| 167 | + page, |
| 168 | + }) => { |
| 169 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-single-highlight'); |
| 170 | + |
| 171 | + const docPanel = page.getByTestId('service-requirements'); |
| 172 | + |
| 173 | + await page.locator(String.raw`#root\/username`).focus(); |
| 174 | + await page.locator(String.raw`#root\/hostPort`).focus(); |
| 175 | + |
| 176 | + await expect( |
| 177 | + docPanel.locator('section[data-highlighted="true"]') |
| 178 | + ).toHaveCount(1); |
| 179 | + }); |
| 180 | + |
| 181 | + test('should load the correct doc file for the selected service type', async ({ |
| 182 | + page, |
| 183 | + }) => { |
| 184 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-correct-doc'); |
| 185 | + |
| 186 | + const docPanel = page.getByTestId('service-requirements'); |
| 187 | + |
| 188 | + // MySQL doc starts with "# MySQL" |
| 189 | + await expect(docPanel.locator('h1').first()).toContainText('MySQL'); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + test.describe('Code block copy button', () => { |
| 194 | + test('should copy code block content to clipboard and show copied tooltip', async ({ |
| 195 | + page, |
| 196 | + context, |
| 197 | + }) => { |
| 198 | + await context.grantPermissions(['clipboard-read', 'clipboard-write']); |
| 199 | + await goToMysqlConnectionStep(page, 'pw-doc-panel-copy'); |
| 200 | + |
| 201 | + const docPanel = page.getByTestId('service-requirements'); |
| 202 | + const codeBlock = docPanel.locator('pre').first(); |
| 203 | + const copyButtonWrapper = docPanel.locator('.code-copy-button').first(); |
| 204 | + const copyButton = docPanel.getByTestId('code-block-copy-icon').first(); |
| 205 | + |
| 206 | + // Hover code block to reveal the button |
| 207 | + await codeBlock.hover(); |
| 208 | + await expect(copyButton).toBeVisible(); |
| 209 | + |
| 210 | + // Verify initial state |
| 211 | + await expect(copyButtonWrapper).toHaveAttribute('data-copied', 'false'); |
| 212 | + |
| 213 | + // Click and verify copied state + tooltip |
| 214 | + await copyButton.click(); |
| 215 | + |
| 216 | + await expect(copyButtonWrapper).toHaveAttribute('data-copied', 'true'); |
| 217 | + await expect(page.getByRole('tooltip')).toBeVisible(); |
| 218 | + |
| 219 | + // Verify clipboard is non-empty |
| 220 | + const clipboardText = await page.evaluate(() => |
| 221 | + navigator.clipboard.readText() |
| 222 | + ); |
| 223 | + |
| 224 | + expect(clipboardText.length).toBeGreaterThan(0); |
| 225 | + |
| 226 | + // Verify state resets after 2s timer |
| 227 | + await expect(copyButtonWrapper).toHaveAttribute('data-copied', 'false'); |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments