Skip to content

Commit 4f31d4e

Browse files
authored
Merge pull request #7 from topcoder-platform/update-unit-test-dependency
Update unit test dependency
2 parents 56d20b9 + 6a37de4 commit 4f31d4e

File tree

4 files changed

+111
-110
lines changed

4 files changed

+111
-110
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ The following parameters can be set in config files or in env variables:
5757

5858
## Testing
5959
- Run tools/mock-project-service app
60-
- Run `npm run init-db`
61-
- Run `npm run create-index`
6260
- Run `npm run test` to execute unit tests
6361
- Run `npm run cov` to execute unit tests and generate coverage report.
6462

test/unit/JobCandidateService.test.js

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,48 @@ const JobCandidate = models.JobCandidate
1919
const Job = models.Job
2020

2121
describe('jobCandidate service test', () => {
22+
beforeEach(() => {
23+
sinon.restore()
24+
})
25+
2226
describe('create job candidate test', () => {
27+
let stubESCreate
28+
beforeEach(() => {
29+
stubESCreate = sinon.stub(esClient, 'create').callsFake(async () => {})
30+
})
31+
2332
it('create job candidate with booking manager success ', async () => {
2433
const jobCandidateRes = _.cloneDeep(jobCandidateResponseBody)
25-
const stubCreate = sinon.stub(JobCandidate, 'create').callsFake(() => {
34+
const stubDBCreate = sinon.stub(JobCandidate, 'create').callsFake(() => {
2635
return jobCandidateResponseBody
2736
})
37+
2838
const entity = await service.createJobCandidate(bookingManagerUser, jobCandidateRequestBody)
2939
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
30-
expect(stubCreate.calledOnce).to.be.true
31-
stubCreate.restore()
40+
expect(stubDBCreate.calledOnce).to.be.true
41+
expect(stubESCreate.calledOnce).to.be.true
3242
})
3343

3444
it('create job candidate with connect user success ', async () => {
3545
const jobCandidateRes = _.cloneDeep(jobCandidateResponseBody)
36-
const stubCreate = sinon.stub(JobCandidate, 'create').callsFake(() => {
46+
const stubDBCreate = sinon.stub(JobCandidate, 'create').callsFake(() => {
3747
return jobCandidateRes
3848
})
3949
const entity = await service.createJobCandidate(connectUser, jobCandidateRequestBody)
4050
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
41-
expect(stubCreate.calledOnce).to.be.true
42-
stubCreate.restore()
51+
expect(stubDBCreate.calledOnce).to.be.true
52+
expect(stubESCreate.calledOnce).to.be.true
4353
})
4454

4555
it('create job candidate with topcoder user success', async () => {
4656
const jobCandidateRes = _.cloneDeep(jobCandidateResponseBody)
47-
const stubCreate = sinon.stub(JobCandidate, 'create').callsFake(() => {
57+
const stubDBCreate = sinon.stub(JobCandidate, 'create').callsFake(() => {
4858
return jobCandidateRes
4959
})
5060
const entity = await service.createJobCandidate(topCoderUser, jobCandidateRequestBody)
5161
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
52-
expect(stubCreate.calledOnce).to.be.true
53-
stubCreate.restore()
62+
expect(stubDBCreate.calledOnce).to.be.true
63+
expect(stubDBCreate.calledOnce).to.be.true
5464
})
5565
})
5666

@@ -63,7 +73,6 @@ describe('jobCandidate service test', () => {
6373
const entity = await service.getJobCandidate(jobCandidateResponseBody.dataValues.id)
6474
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
6575
expect(stubJobCandidateFindOne.calledOnce).to.be.true
66-
stubJobCandidateFindOne.restore()
6776
})
6877

6978
it('get job candidate with candidate not exist success', async () => {
@@ -76,12 +85,16 @@ describe('jobCandidate service test', () => {
7685
} catch (error) {
7786
expect(error.message).to.equal(`JobCandidate with id: ${jobCandidateResponseBody.dataValues.id} doesn't exists.`)
7887
expect(stubJobCandidateFindOne.calledOnce).to.be.true
79-
stubJobCandidateFindOne.restore()
8088
}
8189
})
8290
})
8391

8492
describe('fully update job candidate test', () => {
93+
let stubESUpdate
94+
beforeEach(() => {
95+
stubESUpdate = sinon.stub(esClient, 'update').callsFake(() => {})
96+
})
97+
8598
it('fully update job candidate test with booking manager success', async () => {
8699
const jobCandidateRes = _.cloneDeep(jobCandidateResponseBody)
87100
const stubJobCandidateFindOne = sinon.stub(JobCandidate, 'findOne').callsFake(() => {
@@ -94,14 +107,11 @@ describe('jobCandidate service test', () => {
94107
const stubJobFindOne = sinon.stub(Job, 'findOne').callsFake(() => {
95108
return _.cloneDeep(jobResponseBody)
96109
})
97-
const stubES = sinon.stub(esClient, 'update').callsFake(() => {})
98110
const entity = await service.fullyUpdateJobCandidate(bookingManagerUser, jobCandidateResponseBody.dataValues.id, fullyUpdateJobCandidateRequestBody)
99111
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
100112
expect(stubJobCandidateFindOne.calledOnce).to.be.true
101113
expect(stubJobFindOne.calledOnce).to.be.true
102-
stubJobCandidateFindOne.restore()
103-
stubJobFindOne.restore()
104-
stubES.restore()
114+
expect(stubESUpdate.calledOnce).to.be.true
105115
})
106116

107117
it('fully update job candidate test with connect user success', async () => {
@@ -115,14 +125,11 @@ describe('jobCandidate service test', () => {
115125
const stubJobFindOne = sinon.stub(Job, 'findOne').callsFake(() => {
116126
return _.cloneDeep(jobResponseBody)
117127
})
118-
const stubES = sinon.stub(esClient, 'update').callsFake(() => {})
119128
const entity = await service.fullyUpdateJobCandidate(connectUser, jobCandidateResponseBody.dataValues.id, fullyUpdateJobCandidateRequestBody)
120129
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
121130
expect(stubJobCandidateFindOne.calledOnce).to.be.true
122131
expect(stubJobFindOne.calledOnce).to.be.true
123-
stubJobCandidateFindOne.restore()
124-
stubJobFindOne.restore()
125-
stubES.restore()
132+
expect(stubESUpdate.calledOnce).to.be.true
126133
})
127134

128135
it('fully update job candidate test with topcoder user success', async () => {
@@ -136,19 +143,13 @@ describe('jobCandidate service test', () => {
136143
const stubJobFindOne = sinon.stub(Job, 'findOne').callsFake(() => {
137144
return _.cloneDeep(jobResponseBody)
138145
})
139-
const stubES = sinon.stub(esClient, 'update').callsFake(() => {})
140146
const user = _.assign({}, topCoderUser, { userId: 40152856 })
141-
try {
142-
const entity = await service.fullyUpdateJobCandidate(user, jobCandidateResponseBody.dataValues.id, fullyUpdateJobCandidateRequestBody)
143-
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
144-
expect(stubJobCandidateFindOne.calledOnce).to.be.true
145-
expect(stubJobFindOne.calledOnce).to.be.true
146-
} catch (err) {
147-
console.log(err.message)
148-
}
149-
stubJobCandidateFindOne.restore()
150-
stubJobFindOne.restore()
151-
stubES.restore()
147+
148+
const entity = await service.fullyUpdateJobCandidate(user, jobCandidateResponseBody.dataValues.id, fullyUpdateJobCandidateRequestBody)
149+
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
150+
expect(stubJobCandidateFindOne.calledOnce).to.be.true
151+
expect(stubJobFindOne.calledOnce).to.be.true
152+
expect(stubESUpdate.calledOnce).to.be.true
152153
})
153154

154155
it('fully update job candidate test with topcoder user failed', async () => {
@@ -168,13 +169,17 @@ describe('jobCandidate service test', () => {
168169
} catch (error) {
169170
expect(error.message).to.equal('You are not allowed to perform this action!')
170171
}
171-
172-
stubJobCandidateFindOne.restore()
173-
stubJobFindOne.restore()
172+
expect(stubJobCandidateFindOne.calledOnce).to.be.true
173+
expect(stubJobFindOne.calledOnce).to.be.true
174174
})
175175
})
176176

177177
describe('partially update job candidate test', () => {
178+
let stubESUpdate
179+
beforeEach(() => {
180+
stubESUpdate = sinon.stub(esClient, 'update').callsFake(() => {})
181+
})
182+
178183
it('partially update job candidate test with booking manager success', async () => {
179184
const jobCandidateRes = _.cloneDeep(jobCandidateResponseBody)
180185
const stubJobCandidateFindOne = sinon.stub(JobCandidate, 'findOne').callsFake(() => {
@@ -186,14 +191,12 @@ describe('jobCandidate service test', () => {
186191
const stubJobFindOne = sinon.stub(Job, 'findOne').callsFake(() => {
187192
return _.cloneDeep(jobResponseBody)
188193
})
189-
const stubES = sinon.stub(esClient, 'update').callsFake(() => {})
194+
190195
const entity = await service.partiallyUpdateJobCandidate(bookingManagerUser, jobCandidateResponseBody.dataValues.id, partiallyUpdateJobCandidateRequestBody)
191196
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
192197
expect(stubJobCandidateFindOne.calledOnce).to.be.true
193198
expect(stubJobFindOne.calledOnce).to.be.true
194-
stubJobCandidateFindOne.restore()
195-
stubJobFindOne.restore()
196-
stubES.restore()
199+
expect(stubESUpdate.calledOnce).to.be.true
197200
})
198201

199202
it('partially update job candidate test with connect user success', async () => {
@@ -207,14 +210,11 @@ describe('jobCandidate service test', () => {
207210
const stubJobFindOne = sinon.stub(Job, 'findOne').callsFake(() => {
208211
return _.cloneDeep(jobResponseBody)
209212
})
210-
const stubES = sinon.stub(esClient, 'update').callsFake(() => {})
211213
const entity = await service.partiallyUpdateJobCandidate(connectUser, jobCandidateResponseBody.dataValues.id, partiallyUpdateJobCandidateRequestBody)
212214
expect(entity).to.deep.eql(jobCandidateRes.dataValues)
213215
expect(stubJobCandidateFindOne.calledOnce).to.be.true
214216
expect(stubJobFindOne.calledOnce).to.be.true
215-
stubJobCandidateFindOne.restore()
216-
stubJobFindOne.restore()
217-
stubES.restore()
217+
expect(stubESUpdate.calledOnce).to.be.true
218218
})
219219

220220
it('partially update job candidate test with topcoder user failed', async () => {
@@ -234,8 +234,8 @@ describe('jobCandidate service test', () => {
234234
} catch (error) {
235235
expect(error.message).to.equal('You are not allowed to perform this action!')
236236
}
237-
stubJobCandidateFindOne.restore()
238-
stubJobFindOne.restore()
237+
expect(stubJobCandidateFindOne.calledOnce).to.be.true
238+
expect(stubJobFindOne.calledOnce).to.be.true
239239
})
240240

241241
it('partially update job candidate test with job not exist', async () => {
@@ -248,14 +248,11 @@ describe('jobCandidate service test', () => {
248248
const stubJobFindOne = sinon.stub(Job, 'findOne').callsFake(() => {
249249
return null
250250
})
251-
const stubES = sinon.stub(esClient, 'update').callsFake(() => {})
252251

253252
await service.partiallyUpdateJobCandidate(bookingManagerUser, jobCandidateResponseBody.dataValues.id, partiallyUpdateJobCandidateRequestBody)
254253
expect(stubJobCandidateFindOne.calledOnce).to.be.true
255254
expect(stubJobFindOne.calledOnce).to.be.true
256-
stubJobCandidateFindOne.restore()
257-
stubJobFindOne.restore()
258-
stubES.restore()
255+
expect(stubESUpdate.calledOnce).to.be.true
259256
})
260257
})
261258

@@ -267,11 +264,10 @@ describe('jobCandidate service test', () => {
267264
update: () => { return null }
268265
}
269266
})
270-
const stubES = sinon.stub(esClient, 'delete').callsFake(() => {})
267+
const stubESDelete = sinon.stub(esClient, 'delete').callsFake(() => {})
271268
await service.deleteJobCandidate(bookingManagerUser, jobCandidateResponseBody.dataValues.id)
272269
expect(stubJobCandidateFindOne.calledOnce).to.be.true
273-
stubJobCandidateFindOne.restore()
274-
stubES.restore()
270+
expect(stubESDelete.calledOnce).to.be.true
275271
})
276272

277273
it('delete job candidate test with connect user success', async () => {
@@ -313,7 +309,6 @@ describe('jobCandidate service test', () => {
313309
const entity = await service.searchJobCandidates({ sortBy: 'id', sortOrder: 'asc', page: 1, perPage: 1, jobId: '36762910-4efa-4db4-9b2a-c9ab54c232ed' })
314310
expect(entity.result[0]).to.deep.eql(jobCandidateResponseBody.dataValues)
315311
expect(stub.calledOnce).to.be.true
316-
stub.restore()
317312
})
318313

319314
it('search job candidates without query parameters success', async () => {
@@ -335,7 +330,6 @@ describe('jobCandidate service test', () => {
335330
const entity = await service.searchJobCandidates({})
336331
expect(entity.result[0]).to.deep.eql(jobCandidateResponseBody.dataValues)
337332
expect(stub.calledOnce).to.be.true
338-
stub.restore()
339333
})
340334
})
341335
})

0 commit comments

Comments
 (0)