test(products): add unit tests for GET /api/products

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-04-25 18:31:35 +02:00
parent ead91cef5f
commit eafdef4d5a

View file

@ -31,17 +31,73 @@ function makeRequest(): Request {
describe('GET /api/products', () => {
beforeEach(() => {
vi.clearAllMocks()
mockAuth.mockResolvedValue({ userId: 'user-1', isDemo: false })
})
// TC-P-04
it.todo('returns products owned by the authenticated user')
it('returns products owned by the authenticated user', async () => {
mockPrisma.product.findMany.mockResolvedValue([
{ id: 'prod-1', name: 'Alpha', repo_url: null },
{ id: 'prod-2', name: 'Beta', repo_url: 'https://github.com/example/beta' },
])
const res = await getProducts(makeRequest())
const data = await res.json()
expect(res.status).toBe(200)
expect(data).toHaveLength(2)
expect(data[0]).toMatchObject({ id: 'prod-1', name: 'Alpha' })
expect(data[1]).toMatchObject({ id: 'prod-2', name: 'Beta' })
})
// TC-P-05
it.todo('returns products where user is a team member')
it('includes products where the user is a team member via productAccessFilter', async () => {
mockPrisma.product.findMany.mockResolvedValue([
{ id: 'prod-shared', name: 'Shared Product', repo_url: null },
])
const res = await getProducts(makeRequest())
const data = await res.json()
expect(res.status).toBe(200)
expect(data).toHaveLength(1)
expect(mockPrisma.product.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
archived: false,
OR: expect.arrayContaining([
{ user_id: 'user-1' },
{ members: { some: { user_id: 'user-1' } } },
]),
}),
})
)
})
// TC-P-06
it.todo('returns empty array when user has no products')
it('returns empty array when user has no products', async () => {
mockPrisma.product.findMany.mockResolvedValue([])
const res = await getProducts(makeRequest())
const data = await res.json()
expect(res.status).toBe(200)
expect(data).toEqual([])
})
// TC-P-07
it.todo('excludes archived products')
it('excludes archived products by querying with archived: false', async () => {
mockPrisma.product.findMany.mockResolvedValue([
{ id: 'prod-active', name: 'Active', repo_url: null },
])
const res = await getProducts(makeRequest())
expect(res.status).toBe(200)
expect(mockPrisma.product.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({ archived: false }),
})
)
})
})