agmission/Development/server/tests/integration/pilot.integration.test.js
2026-04-29 09:40:51 -04:00

114 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* Integration tests pilot controller (controllers/pilot.js)
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockApplicator, mockPilot, mockReq, mockRes, newId } = require('./mock_data');
let Pilot, Customer;
beforeAll(async () => {
await connectDB();
Customer = require('../../model/customer');
Pilot = require('../../model/pilot');
});
afterAll(async () => {
await disconnectDB();
});
const pilotCtl = require('../../controllers/pilot');
describe('pilot controller data methods', () => {
let applicator;
beforeAll(async () => {
await clearCollection(Pilot);
applicator = await Customer.create(mockApplicator());
});
afterAll(async () => {
await clearCollection(Pilot);
await Customer.deleteMany({ _id: applicator._id });
});
const makeReq = (extra = {}) =>
mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra });
// -------------------------------------------------------------------------
describe('create_post', () => {
it('creates a pilot and returns it', async () => {
const req = makeReq({ body: mockPilot(applicator._id) });
const res = mockRes();
await pilotCtl.createPilot_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data._id).toBeDefined();
expect(res._data.name).toBe('Test Pilot');
});
it('throws when body is missing', async () => {
const req = makeReq({ body: null });
const res = mockRes();
await expect(pilotCtl.createPilot_post(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('getById_get', () => {
let pilotId;
beforeAll(async () => {
const doc = await Pilot.create(mockPilot(applicator._id));
pilotId = String(doc._id);
});
it('returns pilot by id', async () => {
const req = makeReq({ params: { id: pilotId } });
const res = mockRes();
await pilotCtl.getPilot_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(String(res._data._id)).toBe(pilotId);
});
});
// -------------------------------------------------------------------------
describe('search_post', () => {
it('returns matching pilots', async () => {
const req = makeReq({ body: { byUserId: applicator._id } });
const res = mockRes();
await pilotCtl.search_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
});
// -------------------------------------------------------------------------
describe('delete_delete', () => {
let pilotId;
beforeAll(async () => {
const doc = await Pilot.create(mockPilot(applicator._id));
pilotId = String(doc._id);
});
it.skip('soft-deletes a pilot (skipped: requires MongoDB replica-set for transactions)', async () => {
const req = makeReq({ params: { id: pilotId } });
const res = mockRes();
await pilotCtl.deletePilot(req, res);
expect(res.json).toHaveBeenCalled();
const found = await Pilot.findById(pilotId);
expect(!found || found.markedDelete).toBeTruthy();
});
});
});