Skip to content

Commit

Permalink
Add CreateLostRequest test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshGajabi committed Dec 15, 2023
1 parent af1a2e3 commit 83f395b
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions backend/tests/item.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,78 @@ describe('GetFoundRequest Controller', () => {
expect(res.json.calledWith({ message: 'Found item not found' })).to.be.true;
});
});

describe('CreateLostRequest Controller', function() {
let req, res, next, lostItemStub, uploadToS3Stub;

beforeEach(function() {
// Stubbing the response object
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub()
};

// Stubbing the next function
next = sinon.stub();

// Mocking the request object
req = {
body: {
type: 'Test Type',
brand: 'Test Brand',
size: 'M',
colour: 'Red',
locationLost: 'Test Location',
description: 'Test Description',
itemName: 'Test Item',
user: { _id: 'user_id' },
},
user: {
_id: 'testUserId'
},
files: []
};

// Stubbing the LostItem constructor
lostItemStub = sinon.stub(LostItem.prototype, 'save');

// Stubbing the uploadToS3 function
uploadToS3Stub = sinon.stub(itemController, 'uploadToS3');
});

afterEach(function() {
sinon.restore();
});

it('should create a lost item successfully', async function() {
// Set up stubs and mocks
lostItemStub.resolves({ _id: 'testItemId' });
uploadToS3Stub.resolves('testUrl');

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

expect(res.status.calledWith(200)).to.be.true;
expect(res.json.calledOnce).to.be.true;
expect(res.json.args[0][0]).to.deep.include({ message: 'Created lost item successfully ID: testItemId' });
});

it('should upload files to S3 if present', async function() {
// Update req to include files
req.files = [{}, {}]; // Mock files
uploadToS3Stub.resolves('testUrl');

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

expect(uploadToS3Stub.callCount).to.equal(2);
expect(uploadToS3Stub.calledWith('lost-items', sinon.match.object)).to.be.true;
});

it('should handle errors correctly', async function() {
const error = new Error('Test error');
lostItemStub.rejects(error);

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

expect(next.calledWith(error)).to.be.true;
});
});

0 comments on commit 83f395b

Please sign in to comment.