156 lines
4.3 KiB
JavaScript
156 lines
4.3 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* Integration tests – crop controller (controllers/crop.js)
|
||
*/
|
||
|
||
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
|
||
const { mockApplicator, mockCrop, mockReq, mockRes, newId } = require('./mock_data');
|
||
|
||
let Crop, Customer;
|
||
|
||
beforeAll(async () => {
|
||
await connectDB();
|
||
Crop = require('../../model/crop');
|
||
Customer = require('../../model/customer');
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await disconnectDB();
|
||
});
|
||
|
||
const cropCtl = require('../../controllers/crop');
|
||
|
||
describe('crop controller – data methods', () => {
|
||
let applicator;
|
||
|
||
beforeAll(async () => {
|
||
await clearCollection(Crop);
|
||
applicator = await Customer.create(mockApplicator());
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await clearCollection(Crop);
|
||
await Customer.deleteMany({ _id: applicator._id });
|
||
});
|
||
|
||
const makeReq = (extra = {}) =>
|
||
mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra });
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('create_post', () => {
|
||
it('creates a crop and returns it', async () => {
|
||
const req = makeReq({ body: mockCrop(applicator._id) });
|
||
const res = mockRes();
|
||
|
||
await cropCtl.createCrop_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data._id).toBeDefined();
|
||
expect(res._data.name).toMatch(/Crop-/);
|
||
});
|
||
|
||
it('throws when body is missing', async () => {
|
||
const req = makeReq({ body: null });
|
||
const res = mockRes();
|
||
await expect(cropCtl.createCrop_post(req, res)).rejects.toThrow();
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('getAll_get', () => {
|
||
it('returns crops for the applicator', async () => {
|
||
const req = makeReq({ query: {} });
|
||
const res = mockRes();
|
||
|
||
await cropCtl.getCrops_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(Array.isArray(res._data)).toBe(true);
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('getById_get', () => {
|
||
let cropId;
|
||
|
||
beforeAll(async () => {
|
||
const doc = await Crop.create(mockCrop(applicator._id));
|
||
cropId = String(doc._id);
|
||
});
|
||
|
||
it('returns a crop by id', async () => {
|
||
const req = makeReq({ params: { crop_id: cropId } });
|
||
const res = mockRes();
|
||
|
||
await cropCtl.getCrop_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(String(res._data._id)).toBe(cropId);
|
||
});
|
||
|
||
it('throws when id is invalid', async () => {
|
||
const req = makeReq({ params: { crop_id: 'bad-id' } });
|
||
const res = mockRes();
|
||
await expect(cropCtl.getCrop_get(req, res)).rejects.toThrow();
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('update_put', () => {
|
||
let cropId;
|
||
|
||
beforeAll(async () => {
|
||
const doc = await Crop.create(mockCrop(applicator._id));
|
||
cropId = String(doc._id);
|
||
});
|
||
|
||
it('updates a crop', async () => {
|
||
const req = makeReq({
|
||
params: { crop_id: cropId },
|
||
body: { name: 'Updated Crop' },
|
||
});
|
||
const res = mockRes();
|
||
|
||
await cropCtl.updateCrop_put(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data.name).toBe('Updated Crop');
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('search_post', () => {
|
||
it('returns matching crops', async () => {
|
||
const req = makeReq({ body: { byUserId: applicator._id } });
|
||
const res = mockRes();
|
||
|
||
await cropCtl.search_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(Array.isArray(res._data)).toBe(true);
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('delete_delete', () => {
|
||
let cropId;
|
||
|
||
beforeAll(async () => {
|
||
const doc = await Crop.create(mockCrop(applicator._id));
|
||
cropId = String(doc._id);
|
||
});
|
||
|
||
it('deletes a crop', async () => {
|
||
const req = makeReq({ params: { crop_id: cropId } });
|
||
const res = mockRes();
|
||
|
||
await cropCtl.deleteCrop(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
const found = await Crop.findById(cropId);
|
||
expect(found).toBeNull();
|
||
});
|
||
});
|
||
});
|