|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { |
| 3 | + getPort, |
| 4 | + killProcess, |
| 5 | + runBuildCommand, |
| 6 | + runPreviewCommand, |
| 7 | +} from '../../utils/runCommands'; |
| 8 | + |
| 9 | +test.describe('plugin twoslash test', async () => { |
| 10 | + let appPort: number; |
| 11 | + let app: unknown; |
| 12 | + test.beforeAll(async () => { |
| 13 | + const appDir = __dirname; |
| 14 | + appPort = await getPort(); |
| 15 | + await runBuildCommand(appDir); |
| 16 | + app = await runPreviewCommand(appDir, appPort); |
| 17 | + }); |
| 18 | + |
| 19 | + test.afterAll(async () => { |
| 20 | + if (app) { |
| 21 | + await killProcess(app); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + test('should render twoslash popup elements', async ({ page }) => { |
| 26 | + await page.goto(`http://localhost:${appPort}`, { |
| 27 | + waitUntil: 'networkidle', |
| 28 | + }); |
| 29 | + |
| 30 | + const triggers = await page.$$('twoslash-popup-trigger'); |
| 31 | + expect(triggers.length).toBe(10); |
| 32 | + |
| 33 | + for (const trigger of triggers) { |
| 34 | + const container = await trigger.$('twoslash-popup-container'); |
| 35 | + expect(container).not.toBeNull(); |
| 36 | + |
| 37 | + const initialized = await container?.getAttribute('data-initialized'); |
| 38 | + expect(initialized).toBeNull(); |
| 39 | + |
| 40 | + const inner = await container.$('.twoslash-popup-inner'); |
| 41 | + expect(inner).not.toBeNull(); |
| 42 | + const arrow = await container.$('.twoslash-popup-arrow'); |
| 43 | + expect(arrow).not.toBeNull(); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + test('should clone twoslash popup elements', async ({ page }) => { |
| 48 | + await page.goto(`http://localhost:${appPort}`, { |
| 49 | + waitUntil: 'networkidle', |
| 50 | + }); |
| 51 | + |
| 52 | + const portal = await page.$('twoslash-popup-portal'); |
| 53 | + expect(portal).not.toBeNull(); |
| 54 | + |
| 55 | + const containers = await portal.$$('twoslash-popup-container'); |
| 56 | + expect(containers.length).toBe(10); |
| 57 | + |
| 58 | + const extractTypeAlways = await containers[1].getAttribute('data-always'); |
| 59 | + expect(extractTypeAlways).toBe('true'); |
| 60 | + |
| 61 | + const completionsAlways = await containers[4].getAttribute('data-always'); |
| 62 | + expect(completionsAlways).toBe('true'); |
| 63 | + |
| 64 | + for (const container of containers) { |
| 65 | + const initialized = await container.getAttribute('data-initialized'); |
| 66 | + expect(initialized).not.toBeNull(); |
| 67 | + |
| 68 | + const inner = await container.$('.twoslash-popup-inner'); |
| 69 | + expect(inner).not.toBeNull(); |
| 70 | + const arrow = await container.$('.twoslash-popup-arrow'); |
| 71 | + expect(arrow).not.toBeNull(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + test('should highlight code blocks with twoslash', async ({ page }) => { |
| 76 | + await page.goto(`http://localhost:${appPort}`, { |
| 77 | + waitUntil: 'networkidle', |
| 78 | + }); |
| 79 | + |
| 80 | + const codeBlock = await page.$( |
| 81 | + 'h2:has-text("Highlighting") + .language-ts', |
| 82 | + ); |
| 83 | + expect(codeBlock).not.toBeNull(); |
| 84 | + |
| 85 | + const highlighted = await codeBlock?.$('.twoslash-highlighted'); |
| 86 | + expect(highlighted).not.toBeNull(); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should show errors in code blocks with twoslash', async ({ page }) => { |
| 90 | + await page.goto(`http://localhost:${appPort}`, { |
| 91 | + waitUntil: 'networkidle', |
| 92 | + }); |
| 93 | + |
| 94 | + const codeBlock = await page.$('h2:has-text("Error") + .language-ts'); |
| 95 | + expect(codeBlock).not.toBeNull(); |
| 96 | + |
| 97 | + const error = await codeBlock?.$('.twoslash-error'); |
| 98 | + expect(error).not.toBeNull(); |
| 99 | + |
| 100 | + const errorLine = await codeBlock?.$('.twoslash-error-line'); |
| 101 | + expect(errorLine).not.toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + test('should not apply twoslash to code blocks without twoslash', async ({ |
| 105 | + page, |
| 106 | + }) => { |
| 107 | + await page.goto(`http://localhost:${appPort}`, { |
| 108 | + waitUntil: 'networkidle', |
| 109 | + }); |
| 110 | + |
| 111 | + const codeBlock = await page.$( |
| 112 | + 'h2:has-text("Disable twoslash") + .language-ts', |
| 113 | + ); |
| 114 | + expect(codeBlock).not.toBeNull(); |
| 115 | + |
| 116 | + const triggers = await codeBlock?.$$('twoslash-popup-trigger'); |
| 117 | + expect(triggers.length).toBe(0); |
| 118 | + }); |
| 119 | +}); |
0 commit comments