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

126 lines
3.9 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 customer controller (controllers/customer.js)
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockApplicator, mockClient, mockReq, mockRes, newId } = require('./mock_data');
let Customer, Client;
beforeAll(async () => {
await connectDB();
Customer = require('../../model/customer');
Client = require('../../model/client');
});
afterAll(async () => {
await disconnectDB();
});
const customerCtl = require('../../controllers/customer');
describe('customer controller data methods', () => {
let applicator, client;
beforeAll(async () => {
await clearCollection(Client);
applicator = await Customer.create(mockApplicator());
client = await Client.create(mockClient(applicator._id));
});
afterAll(async () => {
await clearCollection(Client);
await Customer.deleteMany({ _id: applicator._id });
});
const makeReq = (extra = {}) =>
mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra });
// -------------------------------------------------------------------------
describe('getCustomers_get', () => {
it('returns aggregated customer list for applicator', async () => {
const req = makeReq({ query: {} });
const res = mockRes();
await customerCtl.getCustomers_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
it('filters by name when provided', async () => {
const filters = JSON.stringify({ name: { value: 'Test Applicator', valueOperator: 'contains' } });
const req = makeReq({ query: { filters } });
const res = mockRes();
await customerCtl.getCustomers_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data.length).toBeGreaterThan(0);
});
it('returns empty when name does not match', async () => {
const filters = JSON.stringify({ name: { value: 'NONEXISTENT_CLIENT_XYZ', valueOperator: 'contains' } });
const req = makeReq({ query: { filters } });
const res = mockRes();
await customerCtl.getCustomers_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data.length).toBe(0);
});
});
// -------------------------------------------------------------------------
describe('getCustomer_get', () => {
it('returns a customer by id', async () => {
const req = makeReq({ params: { customer_id: String(applicator._id) } });
const res = mockRes();
await customerCtl.getCustomer_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(String(res._data._id)).toBe(String(applicator._id));
});
it('returns null when customer not found', async () => {
const req = makeReq({ params: { customer_id: String(newId()) } });
const res = mockRes();
await customerCtl.getCustomer_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data).toBeNull();
});
it('throws when id is not a valid ObjectId', async () => {
const req = makeReq({ params: { customer_id: 'bad-id' } });
const res = mockRes();
await expect(customerCtl.getCustomer_get(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('updateCustomer_put', () => {
it('updates a customer field', async () => {
const req = makeReq({
body: { _id: String(applicator._id), name: 'Updated Company Name' },
});
const res = mockRes();
await customerCtl.updateCustomer_put(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data.name).toBe('Updated Company Name');
});
it('throws when _id is missing from body', async () => {
const req = makeReq({ body: { name: 'No ID' } });
const res = mockRes();
await expect(customerCtl.updateCustomer_put(req, res)).rejects.toThrow();
});
});
});