agmission/Development/server/tests/integration/geoutil.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

86 lines
2.5 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 geoutil controller (controllers/geoutil.js)
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockApplicator, mockClient, mockJob, mockReq, mockRes, newId } = require('./mock_data');
let Customer, Client, Job;
beforeAll(async () => {
await connectDB();
Customer = require('../../model/customer');
Client = require('../../model/client');
Job = require('../../model/job');
});
afterAll(async () => {
await disconnectDB();
});
const geoutilCtl = require('../../controllers/geoutil');
describe('geoutil controller generateLines_post', () => {
let applicator, client;
beforeAll(async () => {
applicator = await Customer.create(mockApplicator());
client = await Client.create(mockClient(applicator._id));
});
afterAll(async () => {
await Customer.deleteMany({ _id: { $in: [applicator._id, client._id] } });
await clearCollection(Job);
});
const makeReq = (extra = {}) =>
mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra });
it('calls res.end() when body is null', async () => {
const req = makeReq({ body: {} });
req.body = null;
const res = mockRes();
await geoutilCtl.generateLines_post(req, res);
expect(res.end).toHaveBeenCalled();
});
it('throws when jobId is missing from body', async () => {
const req = makeReq({ body: { swath: 60 } });
const res = mockRes();
await expect(geoutilCtl.generateLines_post(req, res)).rejects.toThrow();
});
it('throws when swath is missing from body', async () => {
const req = makeReq({ body: { jobId: 9999 } });
const res = mockRes();
await expect(geoutilCtl.generateLines_post(req, res)).rejects.toThrow();
});
it('returns empty array when no job matches the given jobId', async () => {
const req = makeReq({ body: { jobId: 9999999, swath: 60 } });
const res = mockRes();
await geoutilCtl.generateLines_post(req, res);
expect(res.json).toHaveBeenCalledWith([]);
});
it('returns empty array when job exists but has no spray areas and generate is false', async () => {
const job = await Job.create({ ...mockJob(applicator._id), client: client._id });
const req = makeReq({ body: { jobId: job._id, swath: 60, generate: false } });
const res = mockRes();
await geoutilCtl.generateLines_post(req, res);
// sprayAreas is empty → $unwind eliminates the doc → no lines found
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
});