144 lines
4.6 KiB
JavaScript
144 lines
4.6 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* Integration tests – invoice_settings controller (controllers/invoice_settings.js)
|
||
*/
|
||
|
||
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
|
||
const { mockApplicator, mockClient, mockInvoiceSetting, mockReq, mockRes, newId } = require('./mock_data');
|
||
|
||
let InvoiceSetting, Customer, Client;
|
||
|
||
beforeAll(async () => {
|
||
await connectDB();
|
||
InvoiceSetting = require('../../model/invoice_settings');
|
||
Customer = require('../../model/customer');
|
||
Client = require('../../model/client');
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await disconnectDB();
|
||
});
|
||
|
||
const invSettingsCtl = require('../../controllers/invoice_settings');
|
||
|
||
describe('invoice_settings controller – data methods', () => {
|
||
let applicator, client;
|
||
|
||
beforeAll(async () => {
|
||
await clearCollection(InvoiceSetting);
|
||
applicator = await Customer.create(mockApplicator());
|
||
client = await Client.create(mockClient(applicator._id));
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await clearCollection(InvoiceSetting);
|
||
await Client.deleteMany({ _id: client._id });
|
||
await Customer.deleteMany({ _id: applicator._id });
|
||
});
|
||
|
||
const makeReq = (extra = {}) =>
|
||
mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra });
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('createInvoiceSetting_post', () => {
|
||
it('creates an invoice setting and returns it', async () => {
|
||
const req = makeReq({ body: mockInvoiceSetting(applicator._id, client._id) });
|
||
const res = mockRes();
|
||
|
||
await invSettingsCtl.createInvoiceSetting_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data._id).toBeDefined();
|
||
expect(res._data.companyName).toBe('Test Applicator Co.');
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('getInvoiceSettings_get', () => {
|
||
it('returns invoice settings for the applicator', async () => {
|
||
const req = makeReq({ query: {} });
|
||
const res = mockRes();
|
||
|
||
await invSettingsCtl.getInvoiceSettings_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(Array.isArray(res._data)).toBe(true);
|
||
expect(res._data.length).toBeGreaterThan(0);
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('getInvoiceSettingDetail_get', () => {
|
||
let settingId;
|
||
|
||
beforeAll(async () => {
|
||
await clearCollection(InvoiceSetting);
|
||
const doc = await InvoiceSetting.create(mockInvoiceSetting(applicator._id, client._id));
|
||
settingId = String(doc._id);
|
||
});
|
||
|
||
it('returns invoice setting by id', async () => {
|
||
const req = makeReq({ params: { invoiceSettingId: settingId } });
|
||
const res = mockRes();
|
||
|
||
await invSettingsCtl.getInvoiceSettingDetail_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(String(res._data._id)).toBe(settingId);
|
||
});
|
||
|
||
it('throws when id is invalid', async () => {
|
||
const req = makeReq({ params: { invoiceSettingId: 'bad-id' } });
|
||
const res = mockRes();
|
||
await expect(invSettingsCtl.getInvoiceSettingDetail_get(req, res)).rejects.toThrow();
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('updateInvoiceSetting_put', () => {
|
||
let settingId;
|
||
|
||
beforeAll(async () => {
|
||
await clearCollection(InvoiceSetting);
|
||
const doc = await InvoiceSetting.create(mockInvoiceSetting(applicator._id, client._id));
|
||
settingId = String(doc._id);
|
||
});
|
||
|
||
it('updates an invoice setting', async () => {
|
||
const req = makeReq({
|
||
params: { invoiceSettingId: settingId },
|
||
body: { companyName: 'Updated Co.', paymentTerm: 45 },
|
||
});
|
||
const res = mockRes();
|
||
|
||
await invSettingsCtl.updateInvoiceSetting_put(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data.companyName).toBe('Updated Co.');
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('deleteInvoiceSetting', () => {
|
||
let settingId;
|
||
|
||
beforeAll(async () => {
|
||
await clearCollection(InvoiceSetting);
|
||
const doc = await InvoiceSetting.create(mockInvoiceSetting(applicator._id, client._id));
|
||
settingId = String(doc._id);
|
||
});
|
||
|
||
it('deletes an invoice setting', async () => {
|
||
const req = makeReq({ params: { invoiceSettingId: settingId } });
|
||
const res = mockRes();
|
||
|
||
await invSettingsCtl.deleteInvoiceSetting(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
const found = await InvoiceSetting.findById(settingId);
|
||
expect(found).toBeNull();
|
||
});
|
||
});
|
||
});
|