Skip to content

Commit

Permalink
Almost done with questionController
Browse files Browse the repository at this point in the history
  • Loading branch information
LimZiJia committed Nov 13, 2024
1 parent f937785 commit 951fe2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
2 changes: 1 addition & 1 deletion services/question/src/controllers/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const addQuestion = async (req: Request, res: Response) => {
const existingQuestion = await Question.findOne({
$or: [{ title: title }, { description: description }],
}).collation({ locale: 'en', strength: 2 });
console.log('existingQuestion:', existingQuestion);

if (existingQuestion) {
return handleBadRequest(res, `A question with the same title or description already exists.`);
}
Expand Down
38 changes: 9 additions & 29 deletions services/question/tests/controllers/questionController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ describe('addQuestion', () => {
let saveStub: SinonStub;
let findOneStub: SinonStub;
let getNextSequenceValueStub: SinonStub;
let collationStub: SinonStub;

beforeEach(() => {
req = {
Expand All @@ -509,6 +510,8 @@ describe('addQuestion', () => {
saveStub = sinon.stub(Question.prototype, 'save');
findOneStub = sinon.stub(Question, 'findOne');
getNextSequenceValueStub = sinon.stub(seq, 'getNextSequenceValue');
collationStub = sinon.stub().returnsThis();
findOneStub.returns({ collation: collationStub });
});

afterEach(() => {
Expand All @@ -523,7 +526,7 @@ describe('addQuestion', () => {
difficulty: 'easy',
};

findOneStub.resolves(null);
collationStub.resolves(null);
getNextSequenceValueStub.resolves(1);
saveStub.resolves({
id: 1,
Expand Down Expand Up @@ -627,7 +630,7 @@ describe('addQuestion', () => {
difficulty: 'easy',
};

findOneStub.resolves({
collationStub.resolves({
id: 1,
title: 'Existing Question',
description: 'Existing Description',
Expand Down Expand Up @@ -655,10 +658,10 @@ describe('addQuestion', () => {
difficulty: 'easy',
};

findOneStub.resolves({
collationStub.resolves({
id: 1,
title: 'Existing Question',
description: 'Existing Description',
description: 'Exiing Dstescription',
topics: ['topic1'],
difficulty: 'easy',
});
Expand All @@ -668,7 +671,7 @@ describe('addQuestion', () => {
expect(findOneStub).to.have.been.calledWith({
$or: [{ title: 'Non-Existing Question' }, { description: 'Existing Description' }],
});
expect(res.status).to.have.been.calledWith(500);
expect(res.status).to.have.been.calledWith(400);
expect(res.json).to.have.been.calledWith({
status: 'Error',
message: 'A question with the same title or description already exists.',
Expand All @@ -695,29 +698,6 @@ describe('addQuestion', () => {
});
});

it('should add a new question successfully', async () => {
await addQuestion(req as Request, res as Response);

expect(findOneStub).to.have.been.calledWith({
$or: [{ title: 'New Question' }, { description: 'New Description' }],
});
expect(getNextSequenceValueStub).to.have.been.calledWith('questionId');
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
expect(saveStub).to.have.been.calledOnce;
expect(res.status).to.have.been.calledWith(201);
expect(res.json).to.have.been.calledWith({
status: 'Success',
message: 'Question created successfully',
data: {
id: 1,
title: 'New Question',
description: 'New Description',
topics: ['topic1'],
difficulty: 'easy',
},
});
});

it('should return bad request if title is missing', async () => {
req.body = {
description: 'New Description',
Expand Down Expand Up @@ -790,7 +770,7 @@ describe('addQuestion', () => {
difficulty: 'easy',
};

findOneStub.resolves({
collationStub.resolves({
id: 1,
title: 'Existing Question',
description: 'Existing Description',
Expand Down

0 comments on commit 951fe2e

Please sign in to comment.