117 lines
3.2 KiB
JavaScript
117 lines
3.2 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* Integration tests – location controller (controllers/location.js)
|
||
*
|
||
* helpers/job_queue is mocked to prevent RabbitMQ connection attempts when the
|
||
* controller publishes location updates to the queue.
|
||
*/
|
||
|
||
// Mock job_queue singleton before the controller is required so the controller
|
||
// picks up the stub on first load.
|
||
const publishLocTaskMock = jest.fn();
|
||
jest.doMock(require.resolve('../../helpers/job_queue'), () => ({
|
||
getInstance: jest.fn(() => ({
|
||
publishLocTask: publishLocTaskMock,
|
||
})),
|
||
}));
|
||
|
||
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
|
||
const { mockApplicator, mockReq, mockRes, newId } = require('./mock_data');
|
||
|
||
let Customer, LocCache;
|
||
|
||
beforeAll(async () => {
|
||
await connectDB();
|
||
Customer = require('../../model/customer');
|
||
LocCache = require('../../model/location_cache');
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await disconnectDB();
|
||
});
|
||
|
||
// Require the controller after mocks are in place.
|
||
const locationCtl = require('../../controllers/location');
|
||
|
||
// ---------------------------------------------------------------------------
|
||
describe('location controller – addpos_post', () => {
|
||
let applicator;
|
||
|
||
beforeAll(async () => {
|
||
applicator = await Customer.create(mockApplicator());
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await Customer.deleteMany({ _id: applicator._id });
|
||
await clearCollection(LocCache);
|
||
});
|
||
|
||
it('throws AppParamError when coors is missing from body', async () => {
|
||
const req = mockReq({ uid: applicator._id, ut: '1', body: {} });
|
||
const res = mockRes();
|
||
|
||
await expect(locationCtl.addpos_post(req, res)).rejects.toThrow();
|
||
});
|
||
|
||
it('throws when user does not exist', async () => {
|
||
const req = mockReq({ uid: newId(), ut: '1', body: { coors: [34.05, -118.24] } });
|
||
const res = mockRes();
|
||
|
||
await expect(locationCtl.addpos_post(req, res)).rejects.toThrow();
|
||
});
|
||
|
||
it('returns the user document when valid user and coords are provided', async () => {
|
||
const req = mockReq({
|
||
uid: applicator._id,
|
||
ut: '1',
|
||
body: { coors: [34.0522, -118.2437] },
|
||
});
|
||
const res = mockRes();
|
||
|
||
await locationCtl.addpos_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data).toHaveProperty('_id');
|
||
});
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
describe('location controller – nearMe_post', () => {
|
||
let applicator;
|
||
|
||
beforeAll(async () => {
|
||
applicator = await Customer.create(mockApplicator());
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await Customer.deleteMany({ _id: applicator._id });
|
||
await clearCollection(LocCache);
|
||
});
|
||
|
||
it('throws when user does not exist', async () => {
|
||
const req = mockReq({
|
||
uid: newId(),
|
||
ut: '1',
|
||
body: { coors: [34.05, -118.24] },
|
||
});
|
||
const res = mockRes();
|
||
|
||
await expect(locationCtl.nearMe_post(req, res)).rejects.toThrow();
|
||
});
|
||
|
||
it('returns an array (possibly empty) when user exists', async () => {
|
||
const req = mockReq({
|
||
uid: applicator._id,
|
||
ut: '1',
|
||
body: { coors: [34.0522, -118.2437] },
|
||
});
|
||
const res = mockRes();
|
||
|
||
await locationCtl.nearMe_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(Array.isArray(res._data)).toBe(true);
|
||
});
|
||
});
|