feat(tests): Jest backend (38 tests), Jest RTL frontend (28 tests), Playwright E2E spec
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// Shared Prisma mock — individual test files override methods as needed.
|
||||
const prismaMock = {
|
||||
experiment: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
count: jest.fn(),
|
||||
},
|
||||
animal: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
count: jest.fn(),
|
||||
},
|
||||
dailyStatus: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
count: jest.fn(),
|
||||
},
|
||||
auditLog: {
|
||||
findMany: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
count: jest.fn(),
|
||||
},
|
||||
$connect: jest.fn(),
|
||||
$disconnect: jest.fn(),
|
||||
};
|
||||
|
||||
module.exports = prismaMock;
|
||||
@@ -0,0 +1,120 @@
|
||||
const request = require('supertest');
|
||||
|
||||
jest.mock('../src/lib/prisma', () => require('./__mocks__/prisma'));
|
||||
const prisma = require('../src/lib/prisma');
|
||||
const app = require('../src/app');
|
||||
|
||||
const EXP_ID = 'bbbbbbbb-0000-0000-0000-000000000001';
|
||||
const ANIMAL = {
|
||||
id: 'cccccccc-0000-0000-0000-000000000001',
|
||||
experiment_id: EXP_ID,
|
||||
animal_id_string: 'M-001',
|
||||
animal_name: 'Whiskers',
|
||||
_count: { daily_statuses: 0 },
|
||||
};
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
describe('GET /api/animals', () => {
|
||||
it('returns animals filtered by experimentId', async () => {
|
||||
prisma.animal.findMany.mockResolvedValue([ANIMAL]);
|
||||
const res = await request(app).get(`/api/animals?experimentId=${EXP_ID}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveLength(1);
|
||||
expect(res.body[0].animal_id_string).toBe('M-001');
|
||||
});
|
||||
|
||||
it('returns 422 for invalid UUID experimentId', async () => {
|
||||
const res = await request(app).get('/api/animals?experimentId=not-a-uuid');
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/animals/:id', () => {
|
||||
it('returns animal with daily_statuses', async () => {
|
||||
prisma.animal.findUnique.mockResolvedValue({ ...ANIMAL, daily_statuses: [] });
|
||||
const res = await request(app).get(`/api/animals/${ANIMAL.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.animal_name).toBe('Whiskers');
|
||||
});
|
||||
|
||||
it('returns 404 for unknown animal', async () => {
|
||||
prisma.animal.findUnique.mockResolvedValue(null);
|
||||
const res = await request(app).get('/api/animals/00000000-0000-0000-0000-000000000000');
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/animals', () => {
|
||||
it('creates animal with UUID and writes CREATE audit log', async () => {
|
||||
prisma.animal.create.mockResolvedValue(ANIMAL);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app)
|
||||
.post('/api/animals')
|
||||
.send({ experiment_id: EXP_ID, animal_id_string: 'M-001', animal_name: 'Whiskers' });
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
expect(res.body.id).toMatch(/^[0-9a-f-]{36}$/i);
|
||||
expect(prisma.auditLog.create).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns 422 when experiment_id is missing', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/animals')
|
||||
.send({ animal_id_string: 'M-001', animal_name: 'Whiskers' });
|
||||
expect(res.status).toBe(422);
|
||||
expect(res.body.details).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ field: 'experiment_id' })])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 when experiment_id is not a UUID', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/animals')
|
||||
.send({ experiment_id: 'bad-id', animal_id_string: 'M-001', animal_name: 'Whiskers' });
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
|
||||
it('returns 422 when animal_name is missing', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/animals')
|
||||
.send({ experiment_id: EXP_ID, animal_id_string: 'M-001' });
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /api/animals/:id', () => {
|
||||
it('updates animal and writes UPDATE audit log', async () => {
|
||||
const updated = { ...ANIMAL, animal_name: 'Mr Whiskers' };
|
||||
prisma.animal.findUnique
|
||||
.mockResolvedValueOnce(ANIMAL)
|
||||
.mockResolvedValueOnce(updated);
|
||||
prisma.animal.update.mockResolvedValue(updated);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app)
|
||||
.put(`/api/animals/${ANIMAL.id}`)
|
||||
.send({ animal_name: 'Mr Whiskers' });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.animal_name).toBe('Mr Whiskers');
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ action: 'UPDATE' }) })
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/animals/:id', () => {
|
||||
it('deletes animal and writes DELETE audit log', async () => {
|
||||
prisma.animal.findUnique.mockResolvedValue(ANIMAL);
|
||||
prisma.animal.delete.mockResolvedValue(ANIMAL);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app).delete(`/api/animals/${ANIMAL.id}`);
|
||||
expect(res.status).toBe(204);
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ action: 'DELETE' }) })
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
const request = require('supertest');
|
||||
|
||||
jest.mock('../src/lib/prisma', () => require('./__mocks__/prisma'));
|
||||
const prisma = require('../src/lib/prisma');
|
||||
const app = require('../src/app');
|
||||
|
||||
const LOG = {
|
||||
id: 'ffffffff-0000-0000-0000-000000000001',
|
||||
table_name: 'experiments',
|
||||
record_id: 'aaaaaaaa-0000-0000-0000-000000000001',
|
||||
action: 'UPDATE',
|
||||
changes: { before: { title: 'Old' }, after: { title: 'New' } },
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
describe('GET /api/audit-logs', () => {
|
||||
it('returns paginated logs', async () => {
|
||||
prisma.auditLog.findMany.mockResolvedValue([LOG]);
|
||||
prisma.auditLog.count.mockResolvedValue(1);
|
||||
|
||||
const res = await request(app).get('/api/audit-logs');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.logs).toHaveLength(1);
|
||||
expect(res.body.total).toBe(1);
|
||||
expect(res.body.logs[0].action).toBe('UPDATE');
|
||||
});
|
||||
|
||||
it('filters by tableName', async () => {
|
||||
prisma.auditLog.findMany.mockResolvedValue([LOG]);
|
||||
prisma.auditLog.count.mockResolvedValue(1);
|
||||
|
||||
const res = await request(app).get('/api/audit-logs?tableName=experiments');
|
||||
expect(res.status).toBe(200);
|
||||
expect(prisma.auditLog.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ where: { table_name: 'experiments' } })
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 for invalid recordId', async () => {
|
||||
const res = await request(app).get('/api/audit-logs?recordId=not-a-uuid');
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
|
||||
it('respects limit and offset params', async () => {
|
||||
prisma.auditLog.findMany.mockResolvedValue([]);
|
||||
prisma.auditLog.count.mockResolvedValue(0);
|
||||
|
||||
const res = await request(app).get('/api/audit-logs?limit=10&offset=20');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.limit).toBe(10);
|
||||
expect(res.body.offset).toBe(20);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/audit-logs/:id', () => {
|
||||
it('returns a single log entry', async () => {
|
||||
prisma.auditLog.findUnique.mockResolvedValue(LOG);
|
||||
const res = await request(app).get(`/api/audit-logs/${LOG.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.action).toBe('UPDATE');
|
||||
});
|
||||
|
||||
it('returns 404 for unknown log id', async () => {
|
||||
prisma.auditLog.findUnique.mockResolvedValue(null);
|
||||
const res = await request(app).get('/api/audit-logs/00000000-0000-0000-0000-000000000000');
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,133 @@
|
||||
const request = require('supertest');
|
||||
|
||||
jest.mock('../src/lib/prisma', () => require('./__mocks__/prisma'));
|
||||
const prisma = require('../src/lib/prisma');
|
||||
const app = require('../src/app');
|
||||
|
||||
const ANIMAL_ID = 'dddddddd-0000-0000-0000-000000000001';
|
||||
const STATUS = {
|
||||
id: 'eeeeeeee-0000-0000-0000-000000000001',
|
||||
animal_id: ANIMAL_ID,
|
||||
date: new Date('2024-06-01').toISOString(),
|
||||
experiment_description: 'Baseline measurement',
|
||||
vitals: 'HR 72, Temp 37.2',
|
||||
treatment: 'Control',
|
||||
notes: 'Healthy',
|
||||
};
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
describe('GET /api/daily-statuses', () => {
|
||||
it('returns statuses filtered by animalId', async () => {
|
||||
prisma.dailyStatus.findMany.mockResolvedValue([STATUS]);
|
||||
const res = await request(app).get(`/api/daily-statuses?animalId=${ANIMAL_ID}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveLength(1);
|
||||
expect(res.body[0].vitals).toBe('HR 72, Temp 37.2');
|
||||
});
|
||||
|
||||
it('returns 422 for invalid animalId UUID', async () => {
|
||||
const res = await request(app).get('/api/daily-statuses?animalId=invalid');
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/daily-statuses/:id', () => {
|
||||
it('returns a single status entry', async () => {
|
||||
prisma.dailyStatus.findUnique.mockResolvedValue({ ...STATUS, animal: {} });
|
||||
const res = await request(app).get(`/api/daily-statuses/${STATUS.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.id).toBe(STATUS.id);
|
||||
});
|
||||
|
||||
it('returns 404 for unknown id', async () => {
|
||||
prisma.dailyStatus.findUnique.mockResolvedValue(null);
|
||||
const res = await request(app).get('/api/daily-statuses/00000000-0000-0000-0000-000000000000');
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/daily-statuses', () => {
|
||||
it('creates status and returns 201 with UUID', async () => {
|
||||
prisma.dailyStatus.create.mockResolvedValue(STATUS);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app)
|
||||
.post('/api/daily-statuses')
|
||||
.send({ animal_id: ANIMAL_ID, date: '2024-06-01', vitals: 'HR 72' });
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
expect(res.body.id).toMatch(/^[0-9a-f-]{36}$/i);
|
||||
expect(prisma.auditLog.create).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns 422 when animal_id is missing', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/daily-statuses')
|
||||
.send({ date: '2024-06-01' });
|
||||
expect(res.status).toBe(422);
|
||||
expect(res.body.details).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ field: 'animal_id' })])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 when date is missing', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/daily-statuses')
|
||||
.send({ animal_id: ANIMAL_ID });
|
||||
expect(res.status).toBe(422);
|
||||
expect(res.body.details).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ field: 'date' })])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 when date is not a valid ISO date', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/daily-statuses')
|
||||
.send({ animal_id: ANIMAL_ID, date: 'not-a-date' });
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /api/daily-statuses/:id', () => {
|
||||
it('updates status and writes UPDATE audit log', async () => {
|
||||
const updated = { ...STATUS, vitals: 'HR 80' };
|
||||
prisma.dailyStatus.findUnique
|
||||
.mockResolvedValueOnce(STATUS)
|
||||
.mockResolvedValueOnce(updated);
|
||||
prisma.dailyStatus.update.mockResolvedValue(updated);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app)
|
||||
.put(`/api/daily-statuses/${STATUS.id}`)
|
||||
.send({ vitals: 'HR 80' });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.vitals).toBe('HR 80');
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ action: 'UPDATE' }) })
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 when date format is invalid', async () => {
|
||||
prisma.dailyStatus.findUnique.mockResolvedValue(STATUS);
|
||||
const res = await request(app)
|
||||
.put(`/api/daily-statuses/${STATUS.id}`)
|
||||
.send({ date: 'garbage' });
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/daily-statuses/:id', () => {
|
||||
it('deletes status and writes DELETE audit log', async () => {
|
||||
prisma.dailyStatus.findUnique.mockResolvedValue(STATUS);
|
||||
prisma.dailyStatus.delete.mockResolvedValue(STATUS);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app).delete(`/api/daily-statuses/${STATUS.id}`);
|
||||
expect(res.status).toBe(204);
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ action: 'DELETE' }) })
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,138 @@
|
||||
const request = require('supertest');
|
||||
|
||||
// Mock Prisma before importing app so the app uses the mock
|
||||
jest.mock('../src/lib/prisma', () => require('./__mocks__/prisma'));
|
||||
const prisma = require('../src/lib/prisma');
|
||||
const app = require('../src/app');
|
||||
|
||||
const EXPERIMENT = {
|
||||
id: 'aaaaaaaa-0000-0000-0000-000000000001',
|
||||
title: 'Test Study',
|
||||
created_at: new Date('2024-01-01').toISOString(),
|
||||
_count: { animals: 0 },
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('GET /api/experiments', () => {
|
||||
it('returns list of experiments', async () => {
|
||||
prisma.experiment.findMany.mockResolvedValue([EXPERIMENT]);
|
||||
const res = await request(app).get('/api/experiments');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveLength(1);
|
||||
expect(res.body[0].title).toBe('Test Study');
|
||||
});
|
||||
|
||||
it('returns empty array when no experiments', async () => {
|
||||
prisma.experiment.findMany.mockResolvedValue([]);
|
||||
const res = await request(app).get('/api/experiments');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/experiments/:id', () => {
|
||||
it('returns single experiment', async () => {
|
||||
prisma.experiment.findUnique.mockResolvedValue({ ...EXPERIMENT, animals: [] });
|
||||
const res = await request(app).get(`/api/experiments/${EXPERIMENT.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.id).toBe(EXPERIMENT.id);
|
||||
});
|
||||
|
||||
it('returns 404 for unknown id', async () => {
|
||||
prisma.experiment.findUnique.mockResolvedValue(null);
|
||||
const res = await request(app).get('/api/experiments/00000000-0000-0000-0000-000000000000');
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body.error).toMatch(/not found/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/experiments', () => {
|
||||
it('creates experiment and returns 201 with UUID', async () => {
|
||||
prisma.experiment.create.mockResolvedValue(EXPERIMENT);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app)
|
||||
.post('/api/experiments')
|
||||
.send({ title: 'Test Study' });
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
expect(res.body.id).toBe(EXPERIMENT.id);
|
||||
expect(res.body.title).toBe('Test Study');
|
||||
// UUID format assertion
|
||||
expect(res.body.id).toMatch(
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
);
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ action: 'CREATE' }) })
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 when title is missing', async () => {
|
||||
const res = await request(app).post('/api/experiments').send({});
|
||||
expect(res.status).toBe(422);
|
||||
expect(res.body.details).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ field: 'title' })])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 when title exceeds 255 chars', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/experiments')
|
||||
.send({ title: 'x'.repeat(256) });
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /api/experiments/:id', () => {
|
||||
it('updates experiment and writes audit log', async () => {
|
||||
const updated = { ...EXPERIMENT, title: 'Renamed Study' };
|
||||
prisma.experiment.findUnique
|
||||
.mockResolvedValueOnce(EXPERIMENT) // audit middleware fetches before-state
|
||||
.mockResolvedValueOnce(updated); // audit middleware fetches after-state
|
||||
prisma.experiment.update.mockResolvedValue(updated);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app)
|
||||
.put(`/api/experiments/${EXPERIMENT.id}`)
|
||||
.send({ title: 'Renamed Study' });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.title).toBe('Renamed Study');
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ action: 'UPDATE' }) })
|
||||
);
|
||||
});
|
||||
|
||||
it('returns 422 with empty title', async () => {
|
||||
prisma.experiment.findUnique.mockResolvedValue(EXPERIMENT);
|
||||
const res = await request(app)
|
||||
.put(`/api/experiments/${EXPERIMENT.id}`)
|
||||
.send({ title: '' });
|
||||
expect(res.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/experiments/:id', () => {
|
||||
it('deletes experiment and writes DELETE audit log', async () => {
|
||||
prisma.experiment.findUnique.mockResolvedValue(EXPERIMENT);
|
||||
prisma.experiment.delete.mockResolvedValue(EXPERIMENT);
|
||||
prisma.auditLog.create.mockResolvedValue({});
|
||||
|
||||
const res = await request(app).delete(`/api/experiments/${EXPERIMENT.id}`);
|
||||
expect(res.status).toBe(204);
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ data: expect.objectContaining({ action: 'DELETE' }) })
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /health', () => {
|
||||
it('returns ok status', async () => {
|
||||
const res = await request(app).get('/health');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.status).toBe('ok');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
// Global setup — runs once before all test suites.
|
||||
// When DATABASE_URL points to a real test DB, Prisma migrations are applied here.
|
||||
// In CI/local runs without a live DB, tests use mocked Prisma via jest.mock.
|
||||
module.exports = async function () {
|
||||
process.env.NODE_ENV = 'test';
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = async function () {
|
||||
// Graceful shutdown of any open handles
|
||||
};
|
||||
Reference in New Issue
Block a user