'use strict'; /** * Integration tests – customer controller (controllers/customer.js) */ const { connectDB, disconnectDB, clearCollection } = require('./jest.setup'); const { mockApplicator, mockClient, mockReq, mockRes, newId } = require('./mock_data'); let Customer, Client; beforeAll(async () => { await connectDB(); Customer = require('../../model/customer'); Client = require('../../model/client'); }); afterAll(async () => { await disconnectDB(); }); const customerCtl = require('../../controllers/customer'); describe('customer controller – data methods', () => { let applicator, client; beforeAll(async () => { await clearCollection(Client); applicator = await Customer.create(mockApplicator()); client = await Client.create(mockClient(applicator._id)); }); 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('getCustomers_get', () => { it('returns aggregated customer list for applicator', async () => { const req = makeReq({ query: {} }); const res = mockRes(); await customerCtl.getCustomers_get(req, res); expect(res.json).toHaveBeenCalled(); expect(Array.isArray(res._data)).toBe(true); }); it('filters by name when provided', async () => { const filters = JSON.stringify({ name: { value: 'Test Applicator', valueOperator: 'contains' } }); const req = makeReq({ query: { filters } }); const res = mockRes(); await customerCtl.getCustomers_get(req, res); expect(res.json).toHaveBeenCalled(); expect(res._data.length).toBeGreaterThan(0); }); it('returns empty when name does not match', async () => { const filters = JSON.stringify({ name: { value: 'NONEXISTENT_CLIENT_XYZ', valueOperator: 'contains' } }); const req = makeReq({ query: { filters } }); const res = mockRes(); await customerCtl.getCustomers_get(req, res); expect(res.json).toHaveBeenCalled(); expect(res._data.length).toBe(0); }); }); });