|
| 1 | +import { expect, setupTest, test } from './utils' |
| 2 | + |
| 3 | +test.describe('Base URL configuration tests', () => { |
| 4 | + test('assets should load from root path on nested routes', async ({ page }) => { |
| 5 | + await setupTest(page) |
| 6 | + |
| 7 | + // Navigate to a nested route (e.g., /profile/:id) |
| 8 | + // We'll use a direct navigation to simulate the issue scenario |
| 9 | + await page.goto('/profile/8235701') |
| 10 | + |
| 11 | + // Wait for the page to load |
| 12 | + await page.waitForLoadState('networkidle') |
| 13 | + |
| 14 | + // Check that JavaScript assets are loaded from the correct path |
| 15 | + const scriptTags = await page.locator('script[src]').all() |
| 16 | + const scriptSources = await Promise.all(scriptTags.map((tag) => tag.getAttribute('src'))) |
| 17 | + |
| 18 | + // Filter for internal scripts with absolute paths |
| 19 | + const assetScripts = scriptSources.filter( |
| 20 | + (src) => src && src.startsWith('/') && !src.startsWith('http'), |
| 21 | + ) |
| 22 | + |
| 23 | + // Verify that we found asset scripts to test |
| 24 | + expect(assetScripts.length).toBeGreaterThan(0) |
| 25 | + |
| 26 | + // Verify that asset paths start with / (absolute) and not from current path |
| 27 | + const currentPath = new URL(page.url()).pathname |
| 28 | + for (const src of assetScripts) { |
| 29 | + if (src) { |
| 30 | + expect(src.startsWith('/')).toBeTruthy() |
| 31 | + expect(src.startsWith(currentPath + '/')).toBeFalsy() |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Also verify that internal script resources loaded successfully (no 404s) |
| 36 | + const failedRequests: string[] = [] |
| 37 | + page.on('requestfailed', (request) => { |
| 38 | + if ( |
| 39 | + request.resourceType() === 'script' && |
| 40 | + request.url().startsWith('http://localhost:3000/') |
| 41 | + ) { |
| 42 | + failedRequests.push(request.url()) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + // Reload the page to trigger all resource loads again |
| 47 | + await page.reload() |
| 48 | + await page.waitForLoadState('networkidle') |
| 49 | + |
| 50 | + // Check that no internal script requests failed |
| 51 | + expect(failedRequests.length).toBe(0) |
| 52 | + }) |
| 53 | + |
| 54 | + test('assets should load correctly on dashboard page', async ({ page }) => { |
| 55 | + await setupTest(page) |
| 56 | + |
| 57 | + // Navigate to dashboard (a top-level route) |
| 58 | + await page.goto('/dashboard') |
| 59 | + await page.waitForLoadState('networkidle') |
| 60 | + |
| 61 | + // Check that JavaScript assets are loaded from the correct path |
| 62 | + const scriptTags = await page.locator('script[src]').all() |
| 63 | + const scriptSources = await Promise.all(scriptTags.map((tag) => tag.getAttribute('src'))) |
| 64 | + |
| 65 | + // Filter for internal scripts with absolute paths |
| 66 | + const assetScripts = scriptSources.filter( |
| 67 | + (src) => src && src.startsWith('/') && !src.startsWith('http'), |
| 68 | + ) |
| 69 | + |
| 70 | + // Verify that we found asset scripts to test |
| 71 | + expect(assetScripts.length).toBeGreaterThan(0) |
| 72 | + |
| 73 | + // Verify that asset paths start with / (absolute) |
| 74 | + for (const src of assetScripts) { |
| 75 | + if (src) { |
| 76 | + expect(src.startsWith('/')).toBeTruthy() |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + test('CSS assets should load from root path on nested routes', async ({ page }) => { |
| 82 | + await setupTest(page) |
| 83 | + |
| 84 | + // Navigate to a nested route |
| 85 | + await page.goto('/profile/8235701') |
| 86 | + await page.waitForLoadState('networkidle') |
| 87 | + |
| 88 | + // Check that CSS assets are loaded from the correct path |
| 89 | + const linkTags = await page.locator('link[rel="stylesheet"]').all() |
| 90 | + const linkHrefs = await Promise.all(linkTags.map((tag) => tag.getAttribute('href'))) |
| 91 | + |
| 92 | + // Filter for internal links with absolute paths |
| 93 | + const assetLinks = linkHrefs.filter( |
| 94 | + (href) => href && href.startsWith('/') && !href.startsWith('http'), |
| 95 | + ) |
| 96 | + |
| 97 | + // If there are asset links, verify their paths |
| 98 | + if (assetLinks.length > 0) { |
| 99 | + // Verify that asset paths start with / (absolute) and not from current path |
| 100 | + const currentPath = new URL(page.url()).pathname |
| 101 | + for (const href of assetLinks) { |
| 102 | + if (href) { |
| 103 | + expect(href.startsWith('/')).toBeTruthy() |
| 104 | + expect(href.startsWith(currentPath + '/')).toBeFalsy() |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | +}) |
0 commit comments