Skip to content

Commit 034f45d

Browse files
committed
Same as above commit
1 parent 9fb89f7 commit 034f45d

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Visit https://aka.ms/tsconfig to read more about this file */
2+
{
3+
"compilerOptions": {
4+
"target": "es2016",
5+
"module": "CommonJS",
6+
"outDir": "../dist",
7+
"composite": true,
8+
"strict": true,
9+
"esModuleInterop": true,
10+
"skipLibCheck": true,
11+
"forceConsistentCasingInFileNames": true,
12+
},
13+
"include": ["**/*.ts"]
14+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import chai, { expect } from 'chai';
2+
import sinon from 'sinon';
3+
import sinonChai from 'sinon-chai';
4+
import { Request, Response } from 'express';
5+
import { getQuestions } from '../../src/controllers/questionController';
6+
import { Question } from '../../src/models/questionModel';
7+
8+
chai.use(sinonChai);
9+
10+
describe('getQuestions', () => {
11+
let req: Partial<Request>;
12+
let res: Partial<Response>;
13+
let findStub: sinon.SinonStub;
14+
15+
beforeEach(() => {
16+
req = {
17+
query: {},
18+
};
19+
res = {
20+
status: sinon.stub().returnsThis(),
21+
json: sinon.stub(),
22+
};
23+
findStub = sinon.stub(Question, 'find');
24+
});
25+
26+
afterEach(() => {
27+
sinon.restore();
28+
});
29+
30+
it('should retrieve all questions', async () => {
31+
const mockQuestions = [
32+
{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' },
33+
{ title: 'Question 2', description: 'Description 2', topics: ['topic2'], difficulty: 'medium' },
34+
];
35+
36+
findStub.resolves(mockQuestions);
37+
38+
await getQuestions(req as Request, res as Response);
39+
40+
expect(findStub).to.have.been.calledWith({});
41+
expect(res.status).to.have.been.calledWith(200);
42+
expect(res.json).to.have.been.calledWith({
43+
status: 'Success',
44+
message: 'Questions retrieved successfully',
45+
data: mockQuestions,
46+
});
47+
});
48+
49+
it('should filter questions by title', async () => {
50+
req.query = { title: 'Question 1' };
51+
const mockQuestions = [
52+
{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' },
53+
{ title: 'Question 2', description: 'Description 2', topics: ['topic2'], difficulty: 'medium' },
54+
];
55+
56+
findStub.resolves(mockQuestions);
57+
58+
await getQuestions(req as Request, res as Response);
59+
60+
expect(findStub).to.have.been.calledWith({ title: 'Question 1' });
61+
expect(res.status).to.have.been.calledWith(200);
62+
expect(res.json).to.have.been.calledWith({
63+
status: 'Success',
64+
message: 'Questions retrieved successfully',
65+
data:[{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' }],
66+
});
67+
});
68+
69+
it('should filter questions by description', async () => {
70+
req.query = { description: 'Description 1' };
71+
const mockQuestions = [
72+
{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' },
73+
{ title: 'Question 2', description: 'Description 2', topics: ['topic2'], difficulty: 'medium' },
74+
];
75+
76+
findStub.resolves(mockQuestions);
77+
78+
await getQuestions(req as Request, res as Response);
79+
80+
expect(findStub).to.have.been.calledWith({ description: '1' });
81+
expect(res.status).to.have.been.calledWith(200);
82+
expect(res.json).to.have.been.calledWith({
83+
status: 'Success',
84+
message: 'Questions retrieved successfully',
85+
data: [{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' }],
86+
});
87+
});
88+
89+
it('should filter questions by topics', async () => {
90+
req.query = { topics: 'topic1,topic2' };
91+
const mockQuestions = [
92+
{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' },
93+
{ title: 'Question 2', description: 'Description 2', topics: ['topic2'], difficulty: 'medium' },
94+
{ title: 'Question 3', description: 'Description 3', topics: ['topic3'], difficulty: 'hard' },
95+
];
96+
97+
findStub.resolves(mockQuestions);
98+
99+
await getQuestions(req as Request, res as Response);
100+
101+
expect(findStub).to.have.been.calledWith({ topics: 'topic1, topic2' });
102+
expect(res.status).to.have.been.calledWith(200);
103+
expect(res.json).to.have.been.calledWith({
104+
status: 'Success',
105+
message: 'Questions retrieved successfully',
106+
data: [
107+
{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' },
108+
{ title: 'Question 2', description: 'Description 2', topics: ['topic2'], difficulty: 'medium' },
109+
],
110+
});
111+
});
112+
113+
it('should filter questions by difficulty', async () => {
114+
req.query = { difficulty: 'easy' };
115+
const mockQuestions = [
116+
{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' },
117+
{ title: 'Question 2', description: 'Description 2', topics: ['topic2'], difficulty: 'medium' },
118+
];
119+
120+
findStub.resolves(mockQuestions);
121+
122+
await getQuestions(req as Request, res as Response);
123+
124+
expect(findStub).to.have.been.calledWith({ difficulty: 'easy' });
125+
expect(res.status).to.have.been.calledWith(200);
126+
expect(res.json).to.have.been.calledWith({
127+
status: 'Success',
128+
message: 'Questions retrieved successfully',
129+
data: [{ title: 'Question 1', description: 'Description 1', topics: ['topic1'], difficulty: 'easy' }],
130+
});
131+
});
132+
133+
it('should handle errors', async () => {
134+
const error = new Error('Database error');
135+
findStub.rejects(error);
136+
137+
await getQuestions(req as Request, res as Response);
138+
139+
expect(res.status).to.have.been.calledWith(500);
140+
expect(res.json).to.have.been.calledWith({
141+
status: 'Error',
142+
message: 'Failed to retrieve questions',
143+
});
144+
});
145+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2016",
4+
"module": "CommonJS",
5+
"composite": true,
6+
"strict": true,
7+
"esModuleInterop": true,
8+
"skipLibCheck": true,
9+
"forceConsistentCasingInFileNames": true,
10+
},
11+
"references": [
12+
{ "path": "../src" }
13+
],
14+
"include": ["**/*.ts"]
15+
}

0 commit comments

Comments
 (0)