Files
gruperly/apps/backend/test/students.test.ts
Jose Selesan 4b1f356fab refactor: migrate backend to pnpm monorepo with Hono module architecture
- Replace Bun with pnpm 9 + Turborepo + tsx; apps/api renamed to apps/backend
- Split backend into http/, modules/, lib/ layers mirroring tai-specguard
- Add use-case + Result pattern, Problem Details RFC 7807, basePath /api/v1
- Mount Better Auth at /api/v1/auth, keep session-auth whitelist
- Split Prisma schema into prisma/models/*, generate into generated/
- Rework packages/shared into lib/ + schemas/ with pagination DTOs
- Implement health, groups, students, payments, waitlist modules
- Add vitest suite with prisma mocks (21 tests), biome lint
- Point web client to /api/v1/auth and /api/v1/groups/from-organization
2026-09-14 09:18:40 -03:00

83 lines
2.2 KiB
TypeScript

import { Hono } from 'hono';
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('@/lib/prisma', () => ({
default: {
student: {
findMany: vi.fn(),
count: vi.fn(),
},
},
getPrismaClient: vi.fn(),
UnitOfWork: class {},
}));
import prisma from '@/lib/prisma';
import { studentsRoutes } from '@/modules/students';
const app = new Hono();
app.route('/students', studentsRoutes);
const student = {
id: 'student-1',
groupId: 'group-1',
fullName: 'Ana Pérez',
email: 'ana@example.com',
phone: null,
guardianName: 'Luis Pérez',
guardianPhone: null,
notes: null,
createdAt: new Date('2026-08-01T10:00:00.000Z'),
updatedAt: new Date('2026-08-01T10:00:00.000Z'),
};
describe('students routes', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('lists students with pagination', async () => {
vi.mocked(prisma.student.findMany).mockResolvedValue([student]);
vi.mocked(prisma.student.count).mockResolvedValue(21);
const res = await app.request('/students?page=2&pageSize=10');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
data: [
{
...student,
createdAt: student.createdAt.toISOString(),
updatedAt: student.updatedAt.toISOString(),
},
],
pagination: { page: 2, pageSize: 10, total: 21, totalPages: 3 },
});
expect(prisma.student.findMany).toHaveBeenCalledWith({
skip: 10,
take: 10,
orderBy: { createdAt: 'desc' },
});
expect(prisma.student.count).toHaveBeenCalledWith();
});
it('uses default pagination', async () => {
vi.mocked(prisma.student.findMany).mockResolvedValue([]);
vi.mocked(prisma.student.count).mockResolvedValue(0);
const res = await app.request('/students');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({
data: [],
pagination: { page: 1, pageSize: 10, total: 0, totalPages: 0 },
});
});
it('rejects invalid pagination query parameters', async () => {
const res = await app.request('/students?page=0&pageSize=25');
expect(res.status).toBe(400);
expect(await res.json()).toMatchObject({ status: 400, title: 'Bad Request' });
});
});