|
| 1 | +/* |
| 2 | + * Copyright 2026, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import type { ChildProcessByStdio } from 'node:child_process'; |
| 18 | +import type { Readable, Writable } from 'node:stream'; |
| 19 | +import { TestSession } from '@salesforce/cli-plugins-testkit'; |
| 20 | +import { expect } from 'chai'; |
| 21 | +import { type Browser, type Page } from 'playwright'; |
| 22 | +import { getSession } from '../helpers/sessionUtils.js'; |
| 23 | +import { startLightningDevServer, getPreviewURL, killServerProcess } from '../helpers/devServerUtils.js'; |
| 24 | +import { getPreview } from '../helpers/browserUtils.js'; |
| 25 | + |
| 26 | +const COMPONENT_NAME = 'helloWorld'; |
| 27 | +const INITIAL_GREETING = 'Hello World'; |
| 28 | +const STATIC_CONTENT = 'Static Content'; |
| 29 | + |
| 30 | +describe('lightning preview menu', () => { |
| 31 | + let session: TestSession; |
| 32 | + let childProcess: ChildProcessByStdio<Writable, Readable, Readable> | undefined; |
| 33 | + let browser: Browser; |
| 34 | + let page: Page; |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + session = await getSession(); |
| 38 | + childProcess = startLightningDevServer(session, { AUTO_ENABLE_LOCAL_DEV: 'true' }, COMPONENT_NAME); |
| 39 | + const previewUrl = await getPreviewURL(childProcess.stdout); |
| 40 | + ({ browser, page } = await getPreview(previewUrl, session)); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + if (page) await page.close(); |
| 45 | + if (browser) await browser.close(); |
| 46 | + killServerProcess(childProcess); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should render select link and hamburger menu with helloWorld available and clickable', async () => { |
| 50 | + const greetingLocator = page.getByText(INITIAL_GREETING); |
| 51 | + await greetingLocator.waitFor({ state: 'visible' }); |
| 52 | + |
| 53 | + // When a component is already selected (e.g. --name helloWorld), the canvas shows the component, |
| 54 | + // not the "Select a component..." link. Open the hamburger to verify the panel and helloWorld. |
| 55 | + const menuToggle = page.getByRole('link', { name: 'Toggle menu' }); |
| 56 | + await menuToggle.waitFor({ state: 'visible' }); |
| 57 | + await menuToggle.scrollIntoViewIfNeeded(); |
| 58 | + await menuToggle.click({ force: true }); |
| 59 | + |
| 60 | + // Hamburger opens lwr_dev-component-panel (slide-in panel) |
| 61 | + const componentPanel = page.locator('lwr_dev-component-panel >> .lwr-dev-component-panel__panel--visible'); |
| 62 | + await componentPanel.waitFor({ state: 'visible' }); |
| 63 | + |
| 64 | + const staticItem = page.locator( |
| 65 | + 'lwr_dev-component-panel >> .lwr-dev-component-panel__item[data-specifier="c/static"]', |
| 66 | + ); |
| 67 | + await staticItem.waitFor({ state: 'visible' }); |
| 68 | + await staticItem.click(); |
| 69 | + |
| 70 | + // Wait for the app to load the selected component (URL updates with specifier) |
| 71 | + await page.waitForURL(/specifier=c%2Fstatic|c\/static/, { timeout: 15_000 }); |
| 72 | + |
| 73 | + const staticContentLocator = page.getByText(STATIC_CONTENT); |
| 74 | + await staticContentLocator.waitFor({ state: 'visible', timeout: 15_000 }); |
| 75 | + expect(await staticContentLocator.textContent()).to.include(STATIC_CONTENT); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should render component in performance mode when performance mode button is clicked', async () => { |
| 79 | + const greetingLocator = page.getByText(INITIAL_GREETING); |
| 80 | + await greetingLocator.waitFor({ state: 'visible' }); |
| 81 | + |
| 82 | + const performanceLink = page.locator( |
| 83 | + 'lwr_dev-preview-application >> lwr_dev-preview-header >> .lwr-dev-preview-header__performance-mode-link', |
| 84 | + ); |
| 85 | + await performanceLink.waitFor({ state: 'visible' }); |
| 86 | + await performanceLink.click(); |
| 87 | + |
| 88 | + await page.waitForURL(/mode=performance/); |
| 89 | + expect(page.url()).to.include('mode=performance'); |
| 90 | + |
| 91 | + const header = page.locator( |
| 92 | + 'lwr_dev-preview-application >> lwr_dev-preview-header >> .lwr-dev-preview-header__header', |
| 93 | + ); |
| 94 | + expect(await header.first().isHidden()).to.be.true; |
| 95 | + |
| 96 | + const performanceLinkAfter = page.locator( |
| 97 | + 'lwr_dev-preview-application >> lwr_dev-preview-header >> .lwr-dev-preview-header__performance-mode-link', |
| 98 | + ); |
| 99 | + expect(await performanceLinkAfter.first().isHidden()).to.be.true; |
| 100 | + |
| 101 | + await greetingLocator.waitFor({ state: 'visible' }); |
| 102 | + expect(await greetingLocator.textContent()).to.equal(INITIAL_GREETING); |
| 103 | + }); |
| 104 | +}); |
0 commit comments