Skip to content

Commit

Permalink
Add test cases for getFoundRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshGajabi committed Dec 15, 2023
1 parent eaa11ce commit be0c5de
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module.exports.getFoundRequest = async (req, res, next) => {
return res.status(404).json({ message: 'Found item not found' });
}

res.json({ foundItem });
res.status(200).json({ foundItem });
} catch (err) {
console.error("Error fetching foundItem details:", err);
res.status(500).json({ message: 'Error fetching foundItem details' });
Expand Down
53 changes: 52 additions & 1 deletion backend/tests/item.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require('chai');
const sinon = require('sinon');
const { LostItem } = require('../models/Item');
const { LostItem, FoundItem } = require('../models/Item');
const itemController = require('../controllers/item');

describe('GetLostRequest Controller', () => {
Expand Down Expand Up @@ -53,3 +53,54 @@ describe('GetLostRequest Controller', () => {
expect(res.json.calledWith({ message: 'Lost item not found' })).to.be.true;
});
});

describe('GetFoundRequest Controller', () => {
let sandbox;
let req, res, next;
let findByIdStub;

before(() => {
sandbox = sinon.createSandbox();
});

after(() => {
sinon.restore();
});

beforeEach(() => {
req = {
params: { id: 'valid_item_id' },
};

res = {
json: sandbox.spy(),
status: sandbox.stub().returnsThis(),
};

next = sandbox.spy();
findByIdStub = sandbox.stub(FoundItem, 'findById');
});

afterEach(() => {
sandbox.restore();
});

it('should return found item details for a valid ID', async () => {
const mockItem = { itemName: 'Test Item', type: 'Test Type' };
findByIdStub.withArgs('valid_item_id').resolves(mockItem);

await itemController.getFoundRequest(req, res, next);

expect(res.status.calledWith(200)).to.be.true;
expect(res.json.calledWith({ foundItem: mockItem })).to.be.true;
});

it('should return a 404 status when the found item is not found', async () => {
findByIdStub.withArgs('invalid_item_id').resolves(null);

await itemController.getFoundRequest(req, res, next);

expect(res.status.calledWith(404)).to.be.true;
expect(res.json.calledWith({ message: 'Found item not found' })).to.be.true;
});
});

0 comments on commit be0c5de

Please sign in to comment.