agmission/Development/server/tests/integration/pilot.integration.test.js
Devin Major 6ff07c9d74
All checks were successful
Server Tests / Mocha – Unit & Utility Tests (push) Successful in 1m13s
Server Tests / Jest – Integration Tests (push) Successful in 1m43s
additional integration tests
2026-05-08 14:51:04 -04:00

145 lines
4.1 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();
});
});
// -------------------------------------------------------------------------
describe('updatePilot_put', () => {
let pilotId;
beforeAll(async () => {
const req = makeReq({ body: mockPilot(applicator._id) });
const res = mockRes();
await pilotCtl.createPilot_post(req, res);
pilotId = String(res._data._id);
});
it('updates a pilot field', async () => {
const req = makeReq({
params: { id: pilotId },
body: { name: 'Updated Pilot Name', kind: '5' },
});
const res = mockRes();
await pilotCtl.updatePilot_put(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data.name).toBe('Updated Pilot Name');
});
it('throws when id is invalid', async () => {
const req = makeReq({ params: {}, body: { name: 'X', kind: '5' } });
const res = mockRes();
await expect(pilotCtl.updatePilot_put(req, res)).rejects.toThrow();
});
});
});