126 lines
3.9 KiB
JavaScript
126 lines
3.9 KiB
JavaScript
'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();
|
||
});
|
||
});
|
||
});
|