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

157 lines
4.4 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 product controller (controllers/product.js)
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockApplicator, mockProduct, mockReq, mockRes, newId } = require('./mock_data');
let Product, Customer;
beforeAll(async () => {
await connectDB();
Product = require('../../model/product');
Customer = require('../../model/customer');
});
afterAll(async () => {
await disconnectDB();
});
const productCtl = require('../../controllers/product');
describe('product controller data methods', () => {
let applicator;
beforeAll(async () => {
await clearCollection(Product);
applicator = await Customer.create(mockApplicator());
});
afterAll(async () => {
await clearCollection(Product);
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 product and returns it', async () => {
const req = makeReq({ body: mockProduct(applicator._id) });
const res = mockRes();
await productCtl.createProduct_post(req, res);
expect(res.json).toHaveBeenCalled();
const p = res._data;
expect(p._id).toBeDefined();
expect(p.name).toMatch(/Product-/);
});
it('throws when body is missing', async () => {
const req = makeReq({ body: null });
const res = mockRes();
await expect(productCtl.createProduct_post(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('getAll_get', () => {
it('returns products for the applicator', async () => {
const req = makeReq({ query: {} });
const res = mockRes();
await productCtl.getProducts_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
});
// -------------------------------------------------------------------------
describe('getById_get', () => {
let productId;
beforeAll(async () => {
const doc = await Product.create(mockProduct(applicator._id));
productId = String(doc._id);
});
it('returns a product by id', async () => {
const req = makeReq({ params: { product_id: productId } });
const res = mockRes();
await productCtl.getProduct_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(String(res._data._id)).toBe(productId);
});
it('throws when id is invalid', async () => {
const req = makeReq({ params: { product_id: 'bad-id' } });
const res = mockRes();
await expect(productCtl.getProduct_get(req, res)).rejects.toThrow();
});
});
// -------------------------------------------------------------------------
describe('update_put', () => {
let productId;
beforeAll(async () => {
const doc = await Product.create(mockProduct(applicator._id));
productId = String(doc._id);
});
it('updates a product', async () => {
const req = makeReq({
params: { product_id: productId },
body: { name: 'Updated Product' },
});
const res = mockRes();
await productCtl.updateProduct_put(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data.name).toBe('Updated Product');
});
});
// -------------------------------------------------------------------------
describe('search_post', () => {
it('returns matching products', async () => {
const req = makeReq({ body: { byUserId: applicator._id } });
const res = mockRes();
await productCtl.search_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
});
// -------------------------------------------------------------------------
describe('delete_delete', () => {
let productId;
beforeAll(async () => {
const doc = await Product.create(mockProduct(applicator._id));
productId = String(doc._id);
});
it('deletes a product', async () => {
const req = makeReq({ params: { product_id: productId } });
const res = mockRes();
await productCtl.deleteProduct(req, res);
expect(res.json).toHaveBeenCalled();
const found = await Product.findById(productId);
expect(found).toBeNull();
});
});
});