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

68 lines
1.7 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 common controller (controllers/common.js)
*
* Covers: getCountries_get
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockReq, mockRes } = require('./mock_data');
let Country;
beforeAll(async () => {
await connectDB();
Country = require('../../model/country');
});
afterAll(async () => {
await disconnectDB();
});
const commonCtl = require('../../controllers/common');
describe('common controller data methods', () => {
beforeAll(async () => {
await clearCollection(Country);
await Country.create([
{ code: 'US', name: 'United States' },
{ code: 'CA', name: 'Canada' },
]);
});
afterAll(async () => {
await clearCollection(Country);
});
describe('getCountries_get', () => {
it('returns an array of countries with code and name', async () => {
const req = mockReq();
const res = mockRes();
await commonCtl.getCountries_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
expect(res._data.length).toBeGreaterThanOrEqual(2);
const us = res._data.find(c => c.code === 'US');
expect(us).toBeDefined();
expect(us.name).toBe('United States');
// _id should be excluded per the controller projection
expect(us._id).toBeUndefined();
});
it('returns an empty array when no countries exist', async () => {
await clearCollection(Country);
const req = mockReq();
const res = mockRes();
await commonCtl.getCountries_get(req, res);
expect(res.json).toHaveBeenCalled();
expect(res._data).toEqual([]);
});
});
});