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