203 lines
6.2 KiB
JavaScript
203 lines
6.2 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* Integration tests – dealer controller (controllers/dealer.js)
|
||
*/
|
||
|
||
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
|
||
const { mockReq, mockRes } = require('./mock_data');
|
||
|
||
let Dealer;
|
||
|
||
beforeAll(async () => {
|
||
await connectDB();
|
||
Dealer = require('../../model/dealer');
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await disconnectDB();
|
||
});
|
||
|
||
const dealerCtl = require('../../controllers/dealer');
|
||
|
||
function mockDealer(overrides = {}) {
|
||
return {
|
||
companyName: `Dealer-${Date.now()}`,
|
||
country: 'US',
|
||
contactName: 'John Smith',
|
||
address: '789 Dealer Rd',
|
||
phone: '+15550003000',
|
||
email: `dealer_${Date.now()}@test.com`,
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
describe('dealer controller – data methods', () => {
|
||
beforeAll(async () => {
|
||
await clearCollection(Dealer);
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await clearCollection(Dealer);
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('getDealers_get', () => {
|
||
it('returns an empty array when no dealers exist', async () => {
|
||
const req = mockReq();
|
||
const res = mockRes();
|
||
|
||
await dealerCtl.getDealers_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(Array.isArray(res._data)).toBe(true);
|
||
expect(res._data.length).toBe(0);
|
||
});
|
||
|
||
it('returns dealers sorted by country then companyName', async () => {
|
||
await Dealer.create([
|
||
mockDealer({ companyName: 'Zulu Ag', country: 'CA' }),
|
||
mockDealer({ companyName: 'Alpha Ag', country: 'US' }),
|
||
mockDealer({ companyName: 'Beta Ag', country: 'CA' }),
|
||
]);
|
||
|
||
const req = mockReq();
|
||
const res = mockRes();
|
||
|
||
await dealerCtl.getDealers_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data.length).toBe(3);
|
||
// First two entries should be CA (sorted by country first)
|
||
expect(res._data[0].country).toBe('CA');
|
||
expect(res._data[1].country).toBe('CA');
|
||
// Within CA, sorted by companyName: Beta < Zulu
|
||
expect(res._data[0].companyName).toBe('Beta Ag');
|
||
expect(res._data[1].companyName).toBe('Zulu Ag');
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('createDealer_post', () => {
|
||
it('creates a dealer and returns the saved document', async () => {
|
||
const req = mockReq({ body: mockDealer() });
|
||
const res = mockRes();
|
||
|
||
await dealerCtl.createDealer_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data._id).toBeDefined();
|
||
expect(res._data.country).toBe('US');
|
||
});
|
||
|
||
it('strips the _id from input so Mongo assigns its own', async () => {
|
||
const mongoose = require('mongoose');
|
||
const injectedId = new mongoose.Types.ObjectId();
|
||
const req = mockReq({ body: mockDealer({ _id: injectedId }) });
|
||
const res = mockRes();
|
||
|
||
await dealerCtl.createDealer_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
// The returned _id must differ from the injected one because createDealer_post deletes it
|
||
expect(String(res._data._id)).not.toBe(String(injectedId));
|
||
});
|
||
|
||
it('throws when body is null', async () => {
|
||
const req = mockReq({ body: null });
|
||
const res = mockRes();
|
||
await expect(dealerCtl.createDealer_post(req, res)).rejects.toThrow();
|
||
});
|
||
|
||
it('throws when companyName is missing', async () => {
|
||
const req = mockReq({ body: { country: 'US' } });
|
||
const res = mockRes();
|
||
await expect(dealerCtl.createDealer_post(req, res)).rejects.toThrow();
|
||
});
|
||
|
||
it('throws when country is missing', async () => {
|
||
const req = mockReq({ body: { companyName: 'Test Dealer' } });
|
||
const res = mockRes();
|
||
await expect(dealerCtl.createDealer_post(req, res)).rejects.toThrow();
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('updateDealer_put', () => {
|
||
let dealerId;
|
||
|
||
beforeAll(async () => {
|
||
const doc = await Dealer.create(mockDealer());
|
||
dealerId = String(doc._id);
|
||
});
|
||
|
||
it('updates a dealer and returns the updated document', async () => {
|
||
const req = mockReq({
|
||
params: { id: dealerId },
|
||
body: { contactName: 'Jane Doe' },
|
||
});
|
||
const res = mockRes();
|
||
|
||
await dealerCtl.updateDealer_put(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data.contactName).toBe('Jane Doe');
|
||
expect(String(res._data._id)).toBe(dealerId);
|
||
});
|
||
|
||
it('throws when id param is missing', async () => {
|
||
const req = mockReq({ params: {}, body: { contactName: 'Jane Doe' } });
|
||
const res = mockRes();
|
||
await expect(dealerCtl.updateDealer_put(req, res)).rejects.toThrow();
|
||
});
|
||
|
||
it('throws when id does not match any dealer', async () => {
|
||
const mongoose = require('mongoose');
|
||
const req = mockReq({
|
||
params: { id: new mongoose.Types.ObjectId().toHexString() },
|
||
body: { contactName: 'Nobody' },
|
||
});
|
||
const res = mockRes();
|
||
await expect(dealerCtl.updateDealer_put(req, res)).rejects.toThrow();
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('deleteDealer_delete', () => {
|
||
let dealerId;
|
||
|
||
beforeAll(async () => {
|
||
const doc = await Dealer.create(mockDealer());
|
||
dealerId = String(doc._id);
|
||
});
|
||
|
||
it('deletes a dealer and returns { ok: true }', async () => {
|
||
const req = mockReq({ params: { id: dealerId } });
|
||
const res = mockRes();
|
||
|
||
await dealerCtl.deleteDealer_delete(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data).toEqual({ ok: true });
|
||
|
||
const found = await Dealer.findById(dealerId);
|
||
expect(found).toBeNull();
|
||
});
|
||
|
||
it('throws when id param is missing', async () => {
|
||
const req = mockReq({ params: {} });
|
||
const res = mockRes();
|
||
await expect(dealerCtl.deleteDealer_delete(req, res)).rejects.toThrow();
|
||
});
|
||
|
||
it('throws when id does not match any dealer', async () => {
|
||
const mongoose = require('mongoose');
|
||
const req = mockReq({
|
||
params: { id: new mongoose.Types.ObjectId().toHexString() },
|
||
});
|
||
const res = mockRes();
|
||
await expect(dealerCtl.deleteDealer_delete(req, res)).rejects.toThrow();
|
||
});
|
||
});
|
||
});
|