Skip to content

Commit 951fe2e

Browse files
committed
Almost done with questionController
1 parent f937785 commit 951fe2e

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

services/question/src/controllers/questionController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export const addQuestion = async (req: Request, res: Response) => {
161161
const existingQuestion = await Question.findOne({
162162
$or: [{ title: title }, { description: description }],
163163
}).collation({ locale: 'en', strength: 2 });
164-
console.log('existingQuestion:', existingQuestion);
164+
165165
if (existingQuestion) {
166166
return handleBadRequest(res, `A question with the same title or description already exists.`);
167167
}

services/question/tests/controllers/questionController.spec.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ describe('addQuestion', () => {
497497
let saveStub: SinonStub;
498498
let findOneStub: SinonStub;
499499
let getNextSequenceValueStub: SinonStub;
500+
let collationStub: SinonStub;
500501

501502
beforeEach(() => {
502503
req = {
@@ -509,6 +510,8 @@ describe('addQuestion', () => {
509510
saveStub = sinon.stub(Question.prototype, 'save');
510511
findOneStub = sinon.stub(Question, 'findOne');
511512
getNextSequenceValueStub = sinon.stub(seq, 'getNextSequenceValue');
513+
collationStub = sinon.stub().returnsThis();
514+
findOneStub.returns({ collation: collationStub });
512515
});
513516

514517
afterEach(() => {
@@ -523,7 +526,7 @@ describe('addQuestion', () => {
523526
difficulty: 'easy',
524527
};
525528

526-
findOneStub.resolves(null);
529+
collationStub.resolves(null);
527530
getNextSequenceValueStub.resolves(1);
528531
saveStub.resolves({
529532
id: 1,
@@ -627,7 +630,7 @@ describe('addQuestion', () => {
627630
difficulty: 'easy',
628631
};
629632

630-
findOneStub.resolves({
633+
collationStub.resolves({
631634
id: 1,
632635
title: 'Existing Question',
633636
description: 'Existing Description',
@@ -655,10 +658,10 @@ describe('addQuestion', () => {
655658
difficulty: 'easy',
656659
};
657660

658-
findOneStub.resolves({
661+
collationStub.resolves({
659662
id: 1,
660663
title: 'Existing Question',
661-
description: 'Existing Description',
664+
description: 'Exiing Dstescription',
662665
topics: ['topic1'],
663666
difficulty: 'easy',
664667
});
@@ -668,7 +671,7 @@ describe('addQuestion', () => {
668671
expect(findOneStub).to.have.been.calledWith({
669672
$or: [{ title: 'Non-Existing Question' }, { description: 'Existing Description' }],
670673
});
671-
expect(res.status).to.have.been.calledWith(500);
674+
expect(res.status).to.have.been.calledWith(400);
672675
expect(res.json).to.have.been.calledWith({
673676
status: 'Error',
674677
message: 'A question with the same title or description already exists.',
@@ -695,29 +698,6 @@ describe('addQuestion', () => {
695698
});
696699
});
697700

698-
it('should add a new question successfully', async () => {
699-
await addQuestion(req as Request, res as Response);
700-
701-
expect(findOneStub).to.have.been.calledWith({
702-
$or: [{ title: 'New Question' }, { description: 'New Description' }],
703-
});
704-
expect(getNextSequenceValueStub).to.have.been.calledWith('questionId');
705-
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
706-
expect(saveStub).to.have.been.calledOnce;
707-
expect(res.status).to.have.been.calledWith(201);
708-
expect(res.json).to.have.been.calledWith({
709-
status: 'Success',
710-
message: 'Question created successfully',
711-
data: {
712-
id: 1,
713-
title: 'New Question',
714-
description: 'New Description',
715-
topics: ['topic1'],
716-
difficulty: 'easy',
717-
},
718-
});
719-
});
720-
721701
it('should return bad request if title is missing', async () => {
722702
req.body = {
723703
description: 'New Description',
@@ -790,7 +770,7 @@ describe('addQuestion', () => {
790770
difficulty: 'easy',
791771
};
792772

793-
findOneStub.resolves({
773+
collationStub.resolves({
794774
id: 1,
795775
title: 'Existing Question',
796776
description: 'Existing Description',

0 commit comments

Comments
 (0)