Skip to content

Commit d404f8b

Browse files
committed
Add test cases for tasks
1 parent ba355b7 commit d404f8b

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
const chai = require('chai')
2+
const sinon = require('sinon')
3+
const { expect } = chai
4+
const chaiHttp = require('chai-http')
5+
6+
const app = require('../../server')
7+
const tasks = require('../../models/tasks')
8+
9+
chai.use(chaiHttp)
10+
11+
describe('Tasks', function () {
12+
afterEach(function () {
13+
sinon.restore()
14+
})
15+
16+
describe('POST /tasks - creates a new task', function () {
17+
it('Should return success response after adding the task', function (done) {
18+
sinon.stub(tasks, 'updateTask').callsFake((userData) => {
19+
return { taskId: 'taskId', taskDetails: {} }
20+
})
21+
22+
chai
23+
.request(app)
24+
.post('/tasks')
25+
.send({
26+
type: 'Dev | Group',
27+
links: [
28+
'test1'
29+
],
30+
endsOn: '<unix timestamp>',
31+
startedOn: '<unix timestamp>',
32+
status: 'Active',
33+
ownerId: '<app owner user id>',
34+
percentCompleted: 10,
35+
dependsOn: [
36+
'd12',
37+
'd23'
38+
],
39+
participants: ['id1'],
40+
completionAward: { gold: 3, bronze: 300 },
41+
lossRate: { gold: 1 }
42+
})
43+
.end((err, res) => {
44+
if (err) { return done() }
45+
expect(res).to.have.status(200)
46+
expect(res.body).to.be.a('object')
47+
expect(res.body.message).to.equal('Task created successfully!')
48+
expect(res.body.id).to.be.a('string')
49+
expect(res.body.task).to.be.a('object')
50+
51+
return done()
52+
})
53+
})
54+
})
55+
56+
describe('GET /tasks', function () {
57+
it('Should get all the list of tasks', function (done) {
58+
sinon.stub(tasks, 'fetchTasks').callsFake((query) => {
59+
return [
60+
{
61+
type: 'Dev | Group',
62+
links: [
63+
'test1'
64+
],
65+
endsOn: '<unix timestamp>',
66+
startedOn: '<unix timestamp>',
67+
status: 'Active',
68+
ownerId: '<app owner user id>',
69+
percentCompleted: 10,
70+
dependsOn: [
71+
'd1',
72+
'd2'
73+
],
74+
participants: ['id1'],
75+
completionAward: { gold: 3, bronze: 300 },
76+
lossRate: { gold: 1 }
77+
}
78+
]
79+
})
80+
81+
chai
82+
.request(app)
83+
.get('/tasks')
84+
.end((err, res) => {
85+
if (err) { return done() }
86+
expect(res).to.have.status(200)
87+
expect(res.body).to.be.a('object')
88+
expect(res.body.message).to.equal('Tasks returned successfully!')
89+
expect(res.body.tasks).to.be.a('array')
90+
91+
return done()
92+
})
93+
})
94+
})
95+
96+
describe('PATCH /tasks', function () {
97+
it('Should update the task for the given taskid', function (done) {
98+
sinon.stub(tasks, 'updateTask').callsFake((taskData, taskId) => {
99+
return { taskId: 'taskId' }
100+
})
101+
102+
chai
103+
.request(app)
104+
.patch('/tasks/78xu384O5TurRxZ58AjV')
105+
.send({
106+
ownerId: 'sumit'
107+
})
108+
.end((err, res) => {
109+
if (err) { return done() }
110+
expect(res).to.have.status(204)
111+
112+
return done()
113+
})
114+
})
115+
116+
it('Should return 404 if task does not exist', function (done) {
117+
sinon.stub(tasks, 'updateTask').callsFake((taskData, taskId) => {
118+
return { taskId: 'taskId' }
119+
})
120+
121+
chai
122+
.request(app)
123+
.patch('/tasks/taskid')
124+
.send({
125+
ownerId: 'umit'
126+
})
127+
.end((err, res) => {
128+
if (err) { return done() }
129+
expect(res).to.have.status(404)
130+
expect(res.body).to.be.a('object')
131+
expect(res.body.message).to.equal('Task not found')
132+
133+
return done()
134+
})
135+
})
136+
})
137+
})

utils/swaggerDefinition.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ const swaggerOptions = {
9090
}
9191
}
9292
},
93+
participants: {
94+
type: 'array',
95+
items: {
96+
userid: {
97+
type: 'string'
98+
}
99+
}
100+
},
93101
completionAward: {
94102
type: 'object',
95103
properties: {

0 commit comments

Comments
 (0)