agmission/Development/server/tests/integration/costing_items.integration.test.js
2026-04-29 09:40:51 -04:00

144 lines
4.3 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 costing_items controller (controllers/costing_items.js)
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockApplicator, mockCostingItem, mockReq, mockRes, newId } = require('./mock_data');
let CostingItem, Customer;
beforeAll(async () => {
await connectDB();
CostingItem = require('../../model/costing_items');
Customer = require('../../model/customer');
});
afterAll(async () => {
await disconnectDB();
});
const costingCtl = require('../../controllers/costing_items');
describe('costing_items controller data methods', () => {
let applicator;
beforeAll(async () => {
await clearCollection(CostingItem);
applicator = await Customer.create(mockApplicator());
});
afterAll(async () => {
await clearCollection(CostingItem);
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 costing item and returns it', async () => {
const req = makeReq({ body: mockCostingItem(applicator._id, applicator._id) });
const res = mockRes();
await costingCtl.createCostingItem_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data._id).toBeDefined();
expect(res._data.name).toMatch(/CostItem-/);
});
it('throws when body is missing', async () => {
const req = makeReq({ body: null });
const res = mockRes();
await expect(costingCtl.createCostingItem_post(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('list_get', () => {
it('returns costing items for the applicator', async () => {
const req = makeReq({ query: {} });
const res = mockRes();
await costingCtl.getCostingItems_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
expect(res._data.length).toBeGreaterThan(0);
});
});
// -------------------------------------------------------------------------
describe('getById_get', () => {
let itemId;
beforeAll(async () => {
const doc = await CostingItem.create(mockCostingItem(applicator._id, applicator._id));
itemId = String(doc._id);
});
it('returns costing item by id', async () => {
const req = makeReq({ params: { costingItemId: itemId } });
const res = mockRes();
await costingCtl.getCostingItemDetail_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(String(res._data._id)).toBe(itemId);
});
it('throws when id is invalid', async () => {
const req = makeReq({ params: { costingItemId: 'bad-id' } });
const res = mockRes();
await expect(costingCtl.getCostingItemDetail_get(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('update_put', () => {
let itemId;
beforeAll(async () => {
const doc = await CostingItem.create(mockCostingItem(applicator._id, applicator._id));
itemId = String(doc._id);
});
it('updates a costing item', async () => {
const req = makeReq({
params: { costingItemId: itemId },
body: { name: 'Updated Cost Item', price: 20.0 },
});
const res = mockRes();
await costingCtl.updateCostingItem_put(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data.name).toBe('Updated Cost Item');
});
});
// -------------------------------------------------------------------------
describe('delete_delete', () => {
let itemId;
beforeAll(async () => {
const doc = await CostingItem.create(mockCostingItem(applicator._id, applicator._id));
itemId = String(doc._id);
});
it('deletes a costing item', async () => {
const req = makeReq({ params: { costingItemId: itemId } });
const res = mockRes();
await costingCtl.deleteCostingItem(req, res);
expect(res.json).toHaveBeenCalled();
const found = await CostingItem.findById(itemId);
expect(found).toBeNull();
});
});
});