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

164 lines
4.8 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 client controller (controllers/client.js)
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockApplicator, mockClient, mockReq, mockRes, newId } = require('./mock_data');
let Client, Customer;
beforeAll(async () => {
await connectDB();
Customer = require('../../model/customer');
Client = require('../../model/client');
});
afterAll(async () => {
await disconnectDB();
});
const clientCtl = require('../../controllers/client');
describe('client controller data methods', () => {
let applicator;
beforeAll(async () => {
await clearCollection(Client);
applicator = await Customer.create(mockApplicator());
});
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('create_post', () => {
it('creates a client and returns it', async () => {
const req = makeReq({ body: mockClient(applicator._id) });
const res = mockRes();
await clientCtl.createClient_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data._id).toBeDefined();
expect(res._data.name).toBe('Test Client');
});
it('throws when body is missing', async () => {
const req = makeReq({ body: null });
const res = mockRes();
await expect(clientCtl.createClient_post(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('getById_get', () => {
let clientId;
beforeAll(async () => {
const doc = await Client.create(mockClient(applicator._id));
clientId = String(doc._id);
});
it('returns client by id', async () => {
const req = makeReq({ params: { id: clientId } });
const res = mockRes();
await clientCtl.getClient_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(String(res._data._id)).toBe(clientId);
});
});
// -------------------------------------------------------------------------
describe('search_post', () => {
it('returns matching clients', async () => {
const req = makeReq({ body: { byPuid: applicator._id } });
const res = mockRes();
await clientCtl.search_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
});
// -------------------------------------------------------------------------
describe('delete_delete', () => {
let clientId;
beforeAll(async () => {
const doc = await Client.create(mockClient(applicator._id));
clientId = String(doc._id);
});
it.skip('soft-deletes a client (skipped: requires MongoDB replica-set for transactions)', async () => {
const req = makeReq({ params: { id: clientId } });
const res = mockRes();
await clientCtl.deleteClient(req, res);
expect(res.json).toHaveBeenCalled();
const found = await Client.findById(clientId);
expect(!found || found.markedDelete).toBeTruthy();
});
});
// -------------------------------------------------------------------------
describe('updateClient_put', () => {
let clientId;
beforeAll(async () => {
const req = makeReq({ body: mockClient(applicator._id) });
const res = mockRes();
await clientCtl.createClient_post(req, res);
clientId = String(res._data._id);
});
it('updates a client field', async () => {
const req = makeReq({
params: { id: clientId },
body: { name: 'Updated Client Name', kind: '3' },
});
const res = mockRes();
await clientCtl.updateClient_put(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data.name).toBe('Updated Client Name');
});
it('throws when id is missing', async () => {
const req = makeReq({ params: {}, body: { name: 'X', kind: '3' } });
const res = mockRes();
await expect(clientCtl.updateClient_put(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('searchWithSetting_post', () => {
it('returns clients with invoice settings for the applicator', async () => {
const req = makeReq({ body: { byPuid: String(applicator._id) } });
const res = mockRes();
await clientCtl.searchWithSetting_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
it('throws when byPuid is missing', async () => {
const req = makeReq({ body: {} });
const res = mockRes();
await expect(clientCtl.searchWithSetting_post(req, res)).rejects.toThrow();
});
});
});