68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
'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([]);
|
||
});
|
||
});
|
||
});
|