- Next.js SPA mit Bun Runtime - Prisma mit SQLite Datenbank - Vollständige CRUD-Operationen für Fahrräder, Verschleißteile und Wartungshistorie - Warnsystem für bevorstehende Wartungen - Statistik-Features (Gesamtkosten, durchschnittliche Lebensdauer) - Zod-Validierung für alle API-Requests - Umfassende Test-Suite (41 Tests)
179 lines
4.4 KiB
TypeScript
179 lines
4.4 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
describe('Bikes API', () => {
|
|
beforeEach(async () => {
|
|
// Cleanup before each test
|
|
await prisma.maintenanceHistory.deleteMany()
|
|
await prisma.wearPart.deleteMany()
|
|
await prisma.bike.deleteMany()
|
|
})
|
|
|
|
afterEach(async () => {
|
|
// Cleanup after each test
|
|
await prisma.maintenanceHistory.deleteMany()
|
|
await prisma.wearPart.deleteMany()
|
|
await prisma.bike.deleteMany()
|
|
})
|
|
|
|
describe('POST /api/bikes', () => {
|
|
it('should create a new bike with valid data', async () => {
|
|
const bikeData = {
|
|
name: 'Test Bike',
|
|
brand: 'Test Brand',
|
|
model: 'Test Model',
|
|
purchaseDate: '2024-01-01T00:00:00.000Z',
|
|
notes: 'Test notes',
|
|
}
|
|
|
|
const bike = await prisma.bike.create({
|
|
data: bikeData,
|
|
})
|
|
|
|
expect(bike).toBeDefined()
|
|
expect(bike.name).toBe(bikeData.name)
|
|
expect(bike.brand).toBe(bikeData.brand)
|
|
expect(bike.model).toBe(bikeData.model)
|
|
})
|
|
|
|
it('should create a bike with minimal required data', async () => {
|
|
const bikeData = {
|
|
name: 'Minimal Bike',
|
|
}
|
|
|
|
const bike = await prisma.bike.create({
|
|
data: bikeData,
|
|
})
|
|
|
|
expect(bike).toBeDefined()
|
|
expect(bike.name).toBe(bikeData.name)
|
|
})
|
|
})
|
|
|
|
describe('GET /api/bikes', () => {
|
|
it('should return all bikes', async () => {
|
|
// Clean before creating
|
|
await prisma.bike.deleteMany()
|
|
|
|
await prisma.bike.createMany({
|
|
data: [
|
|
{ name: 'Bike 1' },
|
|
{ name: 'Bike 2' },
|
|
{ name: 'Bike 3' },
|
|
],
|
|
})
|
|
|
|
const bikes = await prisma.bike.findMany()
|
|
|
|
expect(bikes).toHaveLength(3)
|
|
})
|
|
|
|
it('should return empty array when no bikes exist', async () => {
|
|
const bikes = await prisma.bike.findMany()
|
|
|
|
expect(bikes).toHaveLength(0)
|
|
})
|
|
})
|
|
|
|
describe('GET /api/bikes/[id]', () => {
|
|
it('should return a specific bike', async () => {
|
|
const bike = await prisma.bike.create({
|
|
data: { name: 'Test Bike' },
|
|
})
|
|
|
|
const foundBike = await prisma.bike.findUnique({
|
|
where: { id: bike.id },
|
|
})
|
|
|
|
expect(foundBike).toBeDefined()
|
|
expect(foundBike?.id).toBe(bike.id)
|
|
expect(foundBike?.name).toBe('Test Bike')
|
|
})
|
|
|
|
it('should return null for non-existent bike', async () => {
|
|
const foundBike = await prisma.bike.findUnique({
|
|
where: { id: 'non-existent-id' },
|
|
})
|
|
|
|
expect(foundBike).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('PUT /api/bikes/[id]', () => {
|
|
it('should update a bike', async () => {
|
|
const bike = await prisma.bike.create({
|
|
data: { name: 'Original Name' },
|
|
})
|
|
|
|
const updatedBike = await prisma.bike.update({
|
|
where: { id: bike.id },
|
|
data: { name: 'Updated Name' },
|
|
})
|
|
|
|
expect(updatedBike.name).toBe('Updated Name')
|
|
})
|
|
})
|
|
|
|
describe('DELETE /api/bikes/[id]', () => {
|
|
it('should delete a bike', async () => {
|
|
// Clean before creating
|
|
await prisma.bike.deleteMany({ where: { name: 'To Delete' } })
|
|
|
|
const bike = await prisma.bike.create({
|
|
data: { name: 'To Delete' },
|
|
})
|
|
|
|
const bikeId = bike.id
|
|
|
|
await prisma.bike.delete({
|
|
where: { id: bikeId },
|
|
})
|
|
|
|
const foundBike = await prisma.bike.findUnique({
|
|
where: { id: bikeId },
|
|
})
|
|
|
|
expect(foundBike).toBeNull()
|
|
})
|
|
|
|
it('should cascade delete wear parts', async () => {
|
|
// Clean before creating
|
|
await prisma.wearPart.deleteMany()
|
|
await prisma.bike.deleteMany({ where: { name: 'Bike with parts' } })
|
|
|
|
const bike = await prisma.bike.create({
|
|
data: { name: 'Bike with parts' },
|
|
})
|
|
|
|
const part = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: bike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
},
|
|
})
|
|
|
|
const bikeId = bike.id
|
|
const partId = part.id
|
|
|
|
await prisma.bike.delete({
|
|
where: { id: bikeId },
|
|
})
|
|
|
|
const parts = await prisma.wearPart.findMany({
|
|
where: { bikeId: bikeId },
|
|
})
|
|
|
|
const foundPart = await prisma.wearPart.findUnique({
|
|
where: { id: partId },
|
|
})
|
|
|
|
expect(parts).toHaveLength(0)
|
|
expect(foundPart).toBeNull()
|
|
})
|
|
})
|
|
})
|
|
|