'use strict'; /** * Integration tests – log_payment controller (controllers/log_payment.js) * * createLogPayment_post and createLogPayments_post are skipped because they * use MongoDB transactions which require a replica-set. Only the read path * (getLogPayments_get) is tested here. */ const { connectDB, disconnectDB, clearCollection } = require('./jest.setup'); const { mockApplicator, mockClient, mockReq, mockRes, newId } = require('./mock_data'); let LogPayment, Invoice, Customer, Client; beforeAll(async () => { await connectDB(); Customer = require('../../model/customer'); Client = require('../../model/client'); LogPayment = require('../../model/log_payment'); ({ Invoice } = require('../../model')); }); afterAll(async () => { await disconnectDB(); }); const logPaymentCtl = require('../../controllers/log_payment'); describe('log_payment controller – data methods', () => { let applicator, client, invoice; function makeInvoiceDoc(puid, clientId) { return { code: `INV-TEST-${Date.now()}`, currency: 'USD', dueDate: new Date(), openDate: new Date(), byPuid: puid, clients: [{ billTo: clientId, split: '100', subTotal: '0.00' }], jobs: [{ totalAmount: '0.00' }], }; } function makeLogPaymentDoc(puid, clientId, invoiceId) { return { byPuid: puid, invoice: invoiceId, client: clientId, amount: '100.00', paymentDate: new Date(), paymentMethod: 'cash', }; } beforeAll(async () => { await clearCollection(LogPayment); applicator = await Customer.create(mockApplicator()); client = await Client.create(mockClient(applicator._id)); invoice = await Invoice.create(makeInvoiceDoc(applicator._id, client._id)); await LogPayment.create(makeLogPaymentDoc(applicator._id, client._id, invoice._id)); }); afterAll(async () => { await clearCollection(LogPayment); await Invoice.deleteMany({ _id: invoice._id }); 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('getLogPayments_get', () => { it('returns log payments for the applicator', async () => { const req = makeReq({ query: { invoiceId: String(invoice._id) } }); const res = mockRes(); await logPaymentCtl.getLogPayments_get(req, res); expect(res.json).toHaveBeenCalled(); expect(Array.isArray(res._data)).toBe(true); expect(res._data.length).toBeGreaterThan(0); }); it('returns empty array when invoice has no log payments', async () => { const emptyInvoice = await Invoice.create(makeInvoiceDoc(applicator._id, client._id)); const req = makeReq({ query: { invoiceId: String(emptyInvoice._id) } }); const res = mockRes(); await logPaymentCtl.getLogPayments_get(req, res); expect(res.json).toHaveBeenCalled(); expect(res._data.length).toBe(0); await Invoice.deleteMany({ _id: emptyInvoice._id }); }); }); // ------------------------------------------------------------------------- describe('createLogPayment_post (transaction – skipped)', () => { it.skip('skipped: MongoDB transactions require a replica-set', () => {}); }); describe('createLogPayments_post (transaction – skipped)', () => { it.skip('skipped: MongoDB transactions require a replica-set', () => {}); }); });