- 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)
186 lines
4.5 KiB
TypeScript
186 lines
4.5 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
describe('WearParts API', () => {
|
|
let testBike: any
|
|
|
|
beforeEach(async () => {
|
|
// Clean in correct order to respect foreign keys
|
|
await prisma.maintenanceHistory.deleteMany()
|
|
await prisma.wearPart.deleteMany()
|
|
await prisma.bike.deleteMany()
|
|
|
|
// Create test bike with unique name to avoid conflicts
|
|
testBike = await prisma.bike.create({
|
|
data: { name: `Test Bike ${Date.now()}` },
|
|
})
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await prisma.maintenanceHistory.deleteMany()
|
|
await prisma.wearPart.deleteMany()
|
|
await prisma.bike.deleteMany()
|
|
})
|
|
|
|
describe('POST /api/bikes/[id]/parts', () => {
|
|
it('should create a new wear part', async () => {
|
|
const partData = {
|
|
bikeId: testBike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
status: 'ACTIVE',
|
|
}
|
|
|
|
const part = await prisma.wearPart.create({
|
|
data: partData,
|
|
})
|
|
|
|
expect(part).toBeDefined()
|
|
expect(part.type).toBe('CHAIN')
|
|
expect(part.bikeId).toBe(testBike.id)
|
|
expect(part.serviceInterval).toBe(1000)
|
|
})
|
|
|
|
it('should create maintenance history entry on install', async () => {
|
|
const part = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: testBike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
},
|
|
})
|
|
|
|
await prisma.maintenanceHistory.create({
|
|
data: {
|
|
wearPartId: part.id,
|
|
date: new Date(),
|
|
mileage: 0,
|
|
action: 'INSTALL',
|
|
},
|
|
})
|
|
|
|
const history = await prisma.maintenanceHistory.findMany({
|
|
where: { wearPartId: part.id },
|
|
})
|
|
|
|
expect(history).toHaveLength(1)
|
|
expect(history[0].action).toBe('INSTALL')
|
|
})
|
|
})
|
|
|
|
describe('GET /api/parts/[id]', () => {
|
|
it('should return a specific wear part', async () => {
|
|
const part = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: testBike.id,
|
|
type: 'BRAKE_PADS',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 500,
|
|
},
|
|
})
|
|
|
|
const foundPart = await prisma.wearPart.findUnique({
|
|
where: { id: part.id },
|
|
})
|
|
|
|
expect(foundPart).toBeDefined()
|
|
expect(foundPart?.type).toBe('BRAKE_PADS')
|
|
})
|
|
})
|
|
|
|
describe('PUT /api/parts/[id]', () => {
|
|
it('should update a wear part', async () => {
|
|
const part = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: testBike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
},
|
|
})
|
|
|
|
const updatedPart = await prisma.wearPart.update({
|
|
where: { id: part.id },
|
|
data: { serviceInterval: 2000 },
|
|
})
|
|
|
|
expect(updatedPart.serviceInterval).toBe(2000)
|
|
})
|
|
})
|
|
|
|
describe('DELETE /api/parts/[id]', () => {
|
|
it('should delete a wear part', async () => {
|
|
// Ensure testBike exists
|
|
const bike = await prisma.bike.findUnique({
|
|
where: { id: testBike.id },
|
|
})
|
|
|
|
if (!bike) {
|
|
testBike = await prisma.bike.create({
|
|
data: { name: 'Test Bike' },
|
|
})
|
|
}
|
|
|
|
const part = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: testBike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
},
|
|
})
|
|
|
|
const partId = part.id
|
|
|
|
await prisma.wearPart.delete({
|
|
where: { id: partId },
|
|
})
|
|
|
|
const foundPart = await prisma.wearPart.findUnique({
|
|
where: { id: partId },
|
|
})
|
|
|
|
expect(foundPart).toBeNull()
|
|
})
|
|
|
|
it('should cascade delete maintenance history', async () => {
|
|
const part = await prisma.wearPart.create({
|
|
data: {
|
|
bikeId: testBike.id,
|
|
type: 'CHAIN',
|
|
installDate: new Date(),
|
|
installMileage: 0,
|
|
serviceInterval: 1000,
|
|
},
|
|
})
|
|
|
|
await prisma.maintenanceHistory.create({
|
|
data: {
|
|
wearPartId: part.id,
|
|
date: new Date(),
|
|
mileage: 0,
|
|
action: 'INSTALL',
|
|
},
|
|
})
|
|
|
|
await prisma.wearPart.delete({
|
|
where: { id: part.id },
|
|
})
|
|
|
|
const history = await prisma.maintenanceHistory.findMany({
|
|
where: { wearPartId: part.id },
|
|
})
|
|
|
|
expect(history).toHaveLength(0)
|
|
})
|
|
})
|
|
})
|
|
|