Skip to content

Commit 155b91e

Browse files
committed
test: add an e2e test
1 parent ceedfef commit 155b91e

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

modules/runtime/server/cache.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,24 @@ function getMockForUrl(url: string): MockResult | null {
240240
return null
241241
}
242242

243+
// Algolia npm-search API - return mock popular packages
244+
if (host.endsWith('-dsn.algolia.net') && pathname.endsWith('/query')) {
245+
return {
246+
data: {
247+
hits: [
248+
{ name: 'nuxt', downloadsLast30Days: 500_000, modified: new Date().toISOString() },
249+
{ name: 'pnpm', downloadsLast30Days: 800_000, modified: new Date().toISOString() },
250+
{ name: 'express', downloadsLast30Days: 1_000_000, modified: new Date().toISOString() },
251+
{ name: 'minimatch', downloadsLast30Days: 600_000, modified: new Date().toISOString() },
252+
{ name: 'next', downloadsLast30Days: 700_000, modified: new Date().toISOString() },
253+
{ name: 'axios', downloadsLast30Days: 900_000, modified: new Date().toISOString() },
254+
{ name: 'remix', downloadsLast30Days: 400_000, modified: new Date().toISOString() },
255+
{ name: 'webpack', downloadsLast30Days: 750_000, modified: new Date().toISOString() },
256+
],
257+
},
258+
}
259+
}
260+
243261
// esm.sh is handled specially via $fetch.raw override, not here
244262
// Return null to indicate no mock available at the cachedFetch level
245263

test/e2e/homepage-picks.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect, test } from './test-utils'
2+
3+
test.describe('Homepage Picks', () => {
4+
test('displays 4 picks with highlighted letters', async ({ page, goto }) => {
5+
await goto('/', { waitUntil: 'hydration' })
6+
7+
// Use aria-label to target the picks nav specifically (not the header nav)
8+
const picksNav = page.getByRole('navigation', { name: /npmx picks/i })
9+
await expect(picksNav).toBeVisible({ timeout: 15000 })
10+
11+
const pickItems = picksNav.locator('ul > li')
12+
await expect(pickItems).toHaveCount(4)
13+
14+
// Each pick should contain a bold highlighted letter
15+
for (let i = 0; i < 4; i++) {
16+
const highlightedLetter = pickItems.nth(i).locator('span.font-bold')
17+
await expect(highlightedLetter).toBeVisible()
18+
}
19+
20+
// Each pick should be a link to a package page
21+
for (let i = 0; i < 4; i++) {
22+
const link = pickItems.nth(i).locator('a')
23+
const href = await link.getAttribute('href')
24+
expect(href).toMatch(/^\/package\//)
25+
}
26+
})
27+
28+
test('pick links navigate to package pages', async ({ page, goto }) => {
29+
await goto('/', { waitUntil: 'hydration' })
30+
31+
const picksNav = page.getByRole('navigation', { name: /npmx picks/i })
32+
await expect(picksNav).toBeVisible({ timeout: 15000 })
33+
34+
const firstLink = picksNav.locator('ul > li a').first()
35+
await firstLink.click()
36+
await expect(page).toHaveURL(/\/package\//)
37+
})
38+
})

test/fixtures/mock-routes.cjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,25 @@ function matchGitHubApi(urlString) {
520520
return null
521521
}
522522

523+
/**
524+
* @param {string} _urlString
525+
* @returns {MockResponse | null}
526+
*/
527+
function matchAlgoliaApi(_urlString) {
528+
return json({
529+
hits: [
530+
{ name: 'nuxt', downloadsLast30Days: 500000, modified: new Date().toISOString() },
531+
{ name: 'pnpm', downloadsLast30Days: 800000, modified: new Date().toISOString() },
532+
{ name: 'express', downloadsLast30Days: 1000000, modified: new Date().toISOString() },
533+
{ name: 'minimatch', downloadsLast30Days: 600000, modified: new Date().toISOString() },
534+
{ name: 'next', downloadsLast30Days: 700000, modified: new Date().toISOString() },
535+
{ name: 'axios', downloadsLast30Days: 900000, modified: new Date().toISOString() },
536+
{ name: 'remix', downloadsLast30Days: 400000, modified: new Date().toISOString() },
537+
{ name: 'webpack', downloadsLast30Days: 750000, modified: new Date().toISOString() },
538+
],
539+
})
540+
}
541+
523542
/**
524543
* Route definitions mapping URL patterns to their matchers.
525544
* Each entry has a pattern (for Playwright's page.route) and a match function
@@ -549,6 +568,7 @@ const routes = [
549568
pattern: 'https://constellation.microcosm.blue/**',
550569
match: matchConstellationApi,
551570
},
571+
{ name: 'Algolia API', pattern: 'https://*-dsn.algolia.net/**', match: matchAlgoliaApi },
552572
]
553573

554574
/**

0 commit comments

Comments
 (0)