feat: geizhals scraper with cheerio + tests
This commit is contained in:
3757
tests/fixtures/geizhals-gpu.html
vendored
Normal file
3757
tests/fixtures/geizhals-gpu.html
vendored
Normal file
File diff suppressed because one or more lines are too long
32
tests/scrapers/geizhals.test.ts
Normal file
32
tests/scrapers/geizhals.test.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { geizhalsScraper } from '@/lib/scrapers/geizhals'
|
||||
|
||||
const fixture = readFileSync(join(__dirname, '../fixtures/geizhals-gpu.html'), 'utf-8')
|
||||
|
||||
beforeEach(() => {
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
text: async () => fixture,
|
||||
}) as unknown as typeof fetch
|
||||
})
|
||||
|
||||
describe('geizhalsScraper', () => {
|
||||
it('extracts price and name', async () => {
|
||||
const r = await geizhalsScraper.scrape('https://geizhals.de/test')
|
||||
expect(r.price).toBeGreaterThan(0)
|
||||
expect(r.currency).toBe('EUR')
|
||||
expect(r.name).toBeTruthy()
|
||||
})
|
||||
|
||||
it('returns error on HTTP failure', async () => {
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: false, status: 503, text: async () => '',
|
||||
}) as unknown as typeof fetch
|
||||
const r = await geizhalsScraper.scrape('https://geizhals.de/test')
|
||||
expect(r.price).toBeNull()
|
||||
expect(r.error).toMatch(/HTTP 503/)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user