test(reorder): add unit tests for PATCH /api/stories/:id/tasks/reorder

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-04-25 18:31:45 +02:00
parent 536456c1cd
commit 89f74f3dca

View file

@ -6,7 +6,6 @@ vi.mock('@/lib/prisma', () => ({
findFirst: vi.fn(),
},
task: {
findMany: vi.fn(),
update: vi.fn(),
},
$transaction: vi.fn(),
@ -23,11 +22,19 @@ import { PATCH as patchReorder } from '@/app/api/stories/[id]/tasks/reorder/rout
const mockPrisma = prisma as unknown as {
story: { findFirst: ReturnType<typeof vi.fn> }
task: { findMany: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> }
task: { update: ReturnType<typeof vi.fn> }
$transaction: ReturnType<typeof vi.fn>
}
const mockAuth = authenticateApiRequest as ReturnType<typeof vi.fn>
function makeStory(taskIds: string[]) {
return {
id: 'story-1',
product_id: 'prod-1',
tasks: taskIds.map(id => ({ id })),
}
}
function makeRequest(body: unknown, storyId = 'story-1'): [Request, { params: Promise<{ id: string }> }] {
return [
new Request(`http://localhost/api/stories/${storyId}/tasks/reorder`, {
@ -42,29 +49,63 @@ function makeRequest(body: unknown, storyId = 'story-1'): [Request, { params: Pr
describe('PATCH /api/stories/:id/tasks/reorder', () => {
beforeEach(() => {
vi.clearAllMocks()
mockAuth.mockResolvedValue({ userId: 'user-1', isDemo: false })
mockPrisma.$transaction.mockResolvedValue([])
mockPrisma.task.update.mockResolvedValue({ id: 'task-1', sort_order: 1 })
})
// TC-RO-01
it.todo('returns 401 when no token provided')
// TC-RO-03
it.todo('returns 403 for demo users')
// TC-RO-04
it.todo('returns 404 when story is not found')
// TC-RO-05
it.todo('returns 404 for another user\'s story')
// TC-RO-06
it.todo('returns 400 when task_ids is an empty array')
// TC-RO-06 — body validation fires before story lookup
it('returns 400 when task_ids is an empty array', async () => {
const res = await patchReorder(...makeRequest({ task_ids: [] }))
expect(res.status).toBe(400)
expect(mockPrisma.story.findFirst).not.toHaveBeenCalled()
})
// TC-RO-07
it.todo('returns 400 when task_ids is not an array')
it('returns 400 when task_ids is not an array', async () => {
const res = await patchReorder(...makeRequest({ task_ids: 'task-1' }))
expect(res.status).toBe(400)
expect(mockPrisma.story.findFirst).not.toHaveBeenCalled()
})
it('returns 400 when task_ids is missing entirely', async () => {
const res = await patchReorder(...makeRequest({}))
expect(res.status).toBe(400)
})
// TC-RO-08
it.todo('returns 400 when task_ids contains IDs from a different story')
it('returns 400 when task_ids contains an ID not belonging to the story', async () => {
mockPrisma.story.findFirst.mockResolvedValue(makeStory(['task-1', 'task-2']))
const res = await patchReorder(...makeRequest({ task_ids: ['task-1', 'task-from-other-story'] }))
const data = await res.json()
expect(res.status).toBe(400)
expect(data.error).toContain('task-from-other-story')
})
// TC-RO-09
it.todo('reorders tasks and returns 200')
it('reorders tasks and returns 200 with success: true', async () => {
mockPrisma.story.findFirst.mockResolvedValue(makeStory(['task-1', 'task-2', 'task-3']))
const res = await patchReorder(...makeRequest({ task_ids: ['task-3', 'task-1', 'task-2'] }))
const data = await res.json()
expect(res.status).toBe(200)
expect(data).toEqual({ success: true })
expect(mockPrisma.$transaction).toHaveBeenCalled()
})
it('updates each task with its new sort_order index', async () => {
mockPrisma.story.findFirst.mockResolvedValue(makeStory(['task-1', 'task-2']))
await patchReorder(...makeRequest({ task_ids: ['task-2', 'task-1'] }))
expect(mockPrisma.task.update).toHaveBeenCalledWith(
expect.objectContaining({ where: { id: 'task-2' }, data: { sort_order: 1 } })
)
expect(mockPrisma.task.update).toHaveBeenCalledWith(
expect.objectContaining({ where: { id: 'task-1' }, data: { sort_order: 2 } })
)
})
})