|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | +import { createReadStream } from 'node:fs' |
| 3 | +import { stat } from 'node:fs/promises' |
| 4 | +import { createServer, type Server } from 'node:http' |
| 5 | +import path from 'node:path' |
| 6 | +import { fileURLToPath } from 'node:url' |
| 7 | + |
| 8 | +const storybookStaticDir = path.resolve( |
| 9 | + path.dirname(fileURLToPath(import.meta.url)), |
| 10 | + '../storybook-static' |
| 11 | +) |
| 12 | + |
| 13 | +function getContentType(filePath: string): string { |
| 14 | + const ext = path.extname(filePath).toLowerCase() |
| 15 | + switch (ext) { |
| 16 | + case '.html': |
| 17 | + return 'text/html; charset=utf-8' |
| 18 | + case '.js': |
| 19 | + return 'text/javascript; charset=utf-8' |
| 20 | + case '.css': |
| 21 | + return 'text/css; charset=utf-8' |
| 22 | + case '.json': |
| 23 | + return 'application/json; charset=utf-8' |
| 24 | + case '.svg': |
| 25 | + return 'image/svg+xml' |
| 26 | + case '.png': |
| 27 | + return 'image/png' |
| 28 | + case '.jpg': |
| 29 | + case '.jpeg': |
| 30 | + return 'image/jpeg' |
| 31 | + case '.webp': |
| 32 | + return 'image/webp' |
| 33 | + case '.ico': |
| 34 | + return 'image/x-icon' |
| 35 | + case '.woff': |
| 36 | + return 'font/woff' |
| 37 | + case '.woff2': |
| 38 | + return 'font/woff2' |
| 39 | + case '.ttf': |
| 40 | + return 'font/ttf' |
| 41 | + case '.map': |
| 42 | + return 'application/json; charset=utf-8' |
| 43 | + default: |
| 44 | + return 'application/octet-stream' |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function startStorybookServer(): Promise<{ server: Server; baseUrl: string }> { |
| 49 | + await stat(path.join(storybookStaticDir, 'index.html')) |
| 50 | + |
| 51 | + const server = createServer(async (req, res) => { |
| 52 | + const url = new URL(req.url ?? '/', 'http://localhost') |
| 53 | + const rawPath = decodeURIComponent(url.pathname) |
| 54 | + const relativePath = rawPath === '/' ? '/index.html' : rawPath |
| 55 | + const filePath = path.normalize(path.join(storybookStaticDir, relativePath)) |
| 56 | + |
| 57 | + if (!filePath.startsWith(storybookStaticDir)) { |
| 58 | + res.statusCode = 403 |
| 59 | + res.end('Forbidden') |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + const stats = await stat(filePath) |
| 65 | + if (!stats.isFile()) { |
| 66 | + res.statusCode = 404 |
| 67 | + res.end('Not found') |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + res.statusCode = 200 |
| 72 | + res.setHeader('Content-Type', getContentType(filePath)) |
| 73 | + createReadStream(filePath).pipe(res) |
| 74 | + } catch { |
| 75 | + res.statusCode = 404 |
| 76 | + res.end('Not found') |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', () => resolve())) |
| 81 | + const address = server.address() |
| 82 | + if (!address || typeof address === 'string') throw new Error('Unexpected server address') |
| 83 | + |
| 84 | + return { server, baseUrl: `http://127.0.0.1:${address.port}` } |
| 85 | +} |
| 86 | + |
| 87 | +function storyUrl(baseUrl: string, storyId: string): string { |
| 88 | + return `${baseUrl}/?path=/story/${storyId}` |
| 89 | +} |
| 90 | + |
| 91 | +test.describe('PendingTxList Storybook', () => { |
| 92 | + let server: Server |
| 93 | + let baseUrl: string |
| 94 | + |
| 95 | + test.beforeAll(async () => { |
| 96 | + const started = await startStorybookServer() |
| 97 | + server = started.server |
| 98 | + baseUrl = started.baseUrl |
| 99 | + }) |
| 100 | + |
| 101 | + test.afterAll(async () => { |
| 102 | + await new Promise<void>((resolve, reject) => |
| 103 | + server.close((err) => (err ? reject(err) : resolve())) |
| 104 | + ) |
| 105 | + }) |
| 106 | + |
| 107 | + test('Default renders multiple pending transactions', async ({ page }) => { |
| 108 | + const frame = page.frameLocator('#storybook-preview-iframe') |
| 109 | + |
| 110 | + await page.goto(storyUrl(baseUrl, 'transaction-pendingtxlist--default')) |
| 111 | + |
| 112 | + // Wait for the pending tx list to be visible |
| 113 | + await expect(frame.getByText('待处理交易')).toBeVisible({ timeout: 10000 }) |
| 114 | + |
| 115 | + // Check that transactions are visible |
| 116 | + await expect(frame.getByText('100.5 BFM')).toBeVisible() |
| 117 | + await expect(frame.getByText('50 CPCC')).toBeVisible() |
| 118 | + await expect(frame.getByText('200 BFM')).toBeVisible() |
| 119 | + |
| 120 | + // Take screenshot |
| 121 | + await expect(frame.locator('#storybook-root')).toHaveScreenshot('pending-tx-list-default.png') |
| 122 | + }) |
| 123 | + |
| 124 | + test('Broadcasting state shows spinner animation', async ({ page }) => { |
| 125 | + const frame = page.frameLocator('#storybook-preview-iframe') |
| 126 | + |
| 127 | + await page.goto(storyUrl(baseUrl, 'transaction-pendingtxlist--broadcasting')) |
| 128 | + |
| 129 | + // Wait for the pending tx list to be visible |
| 130 | + await expect(frame.getByText('待处理交易')).toBeVisible({ timeout: 10000 }) |
| 131 | + |
| 132 | + // Check broadcasting state |
| 133 | + await expect(frame.getByText('广播中')).toBeVisible() |
| 134 | + |
| 135 | + // Take screenshot |
| 136 | + await expect(frame.locator('#storybook-root')).toHaveScreenshot('pending-tx-broadcasting.png') |
| 137 | + }) |
| 138 | + |
| 139 | + test('Broadcasted state shows waiting confirmation', async ({ page }) => { |
| 140 | + const frame = page.frameLocator('#storybook-preview-iframe') |
| 141 | + |
| 142 | + await page.goto(storyUrl(baseUrl, 'transaction-pendingtxlist--broadcasted')) |
| 143 | + |
| 144 | + // Wait for the pending tx list to be visible |
| 145 | + await expect(frame.getByText('待处理交易')).toBeVisible({ timeout: 10000 }) |
| 146 | + |
| 147 | + // Check broadcasted state |
| 148 | + await expect(frame.getByText('等待上链')).toBeVisible() |
| 149 | + |
| 150 | + // Take screenshot |
| 151 | + await expect(frame.locator('#storybook-root')).toHaveScreenshot('pending-tx-broadcasted.png') |
| 152 | + }) |
| 153 | + |
| 154 | + test('Failed state shows error message and retry button', async ({ page }) => { |
| 155 | + const frame = page.frameLocator('#storybook-preview-iframe') |
| 156 | + |
| 157 | + await page.goto(storyUrl(baseUrl, 'transaction-pendingtxlist--failed')) |
| 158 | + |
| 159 | + // Wait for the pending tx list to be visible |
| 160 | + await expect(frame.getByText('待处理交易')).toBeVisible({ timeout: 10000 }) |
| 161 | + |
| 162 | + // Check failed state |
| 163 | + await expect(frame.getByText('广播失败')).toBeVisible() |
| 164 | + await expect(frame.getByText('资产余额不足')).toBeVisible() |
| 165 | + |
| 166 | + // Take screenshot |
| 167 | + await expect(frame.locator('#storybook-root')).toHaveScreenshot('pending-tx-failed.png') |
| 168 | + }) |
| 169 | + |
| 170 | + test('MultipleStates shows all status types', async ({ page }) => { |
| 171 | + const frame = page.frameLocator('#storybook-preview-iframe') |
| 172 | + |
| 173 | + await page.goto(storyUrl(baseUrl, 'transaction-pendingtxlist--multiple-states')) |
| 174 | + |
| 175 | + // Wait for the pending tx list to be visible |
| 176 | + await expect(frame.getByText('待处理交易')).toBeVisible({ timeout: 10000 }) |
| 177 | + |
| 178 | + // Check all states are visible |
| 179 | + await expect(frame.getByText('广播中')).toBeVisible() |
| 180 | + await expect(frame.getByText('等待上链')).toBeVisible() |
| 181 | + await expect(frame.getByText('广播失败').first()).toBeVisible() |
| 182 | + |
| 183 | + // Take screenshot |
| 184 | + await expect(frame.locator('#storybook-root')).toHaveScreenshot('pending-tx-multiple-states.png') |
| 185 | + }) |
| 186 | +}) |
0 commit comments