generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from agiledev-students-fall2023/sprint/3/task…
…/152/test-update POST test update
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import chai, { expect } from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import sinon from 'sinon'; | ||
import axios from 'axios'; | ||
import { promises as fs } from 'fs'; | ||
import { studentIssueUpdateHandler } from '../src/controllers/studentIssueUpdateHandler.js'; | ||
import { publicpath } from "../app.js"; | ||
|
||
let server = "http://localhost:5000/"; | ||
|
||
chai.use(chaiHttp); | ||
|
||
describe('Unit Tests for studentIssueUpdateHandler', () => { | ||
let req, res, axiosGetStub, sendSpy, jsonSpy, statusSpy; | ||
|
||
beforeEach(() => { | ||
req = { | ||
params: { paramName: '123', studentNetID: 's123456' }, | ||
body: { issueindex: '1', comments: 'Test comment', currentStatus: 'open' } | ||
}; | ||
res = { | ||
json: sinon.spy(), | ||
status: sinon.stub().returns({ send: sinon.spy() }) | ||
}; | ||
sendSpy = res.status().send; | ||
jsonSpy = res.json; | ||
|
||
// Stub axios.get method | ||
axiosGetStub = sinon.stub(axios, 'get'); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore the stub after each test | ||
axiosGetStub.restore(); | ||
}); | ||
|
||
it('should update the issue and send the correct response when the request is valid', async () => { | ||
// Set up the stub to return a specific value | ||
axiosGetStub.resolves({ data: [{ index: '1', comments: [] }] }); | ||
|
||
await studentIssueUpdateHandler(req, res); | ||
expect(jsonSpy.calledOnce).to.be.false; | ||
expect(sendSpy.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should send an error response when the request is invalid', async () => { | ||
// Set up the stub to return a specific value | ||
axiosGetStub.resolves({ data: [{ index: '2', comments: [] }] }); | ||
|
||
await studentIssueUpdateHandler(req, res); | ||
expect(jsonSpy.calledOnce).to.be.false; | ||
expect(sendSpy.calledOnce).to.be.true; | ||
}); | ||
}); | ||
|
||
chai.use(chaiHttp); | ||
|
||
describe('Integration Tests for studentIssueUpdateHandler', () => { | ||
|
||
// const studentNetID = "tm2005"; | ||
// const paramName = "6"; | ||
|
||
// it('should update the issue and return the correct response when the request is valid', async () => { | ||
|
||
// // Execute the handler | ||
// // await studentIssueUpdateHandler(req, res); | ||
|
||
// // axiosStub.resolves(mockResponse); | ||
|
||
// const res = await chai.request(server) | ||
// .post(`api/actions/student/${studentNetID}/${paramName}`) | ||
// .send({ | ||
// "index": 6 | ||
// }); | ||
|
||
// expect(res.status).to.equal(200); | ||
// expect(res.body).to.have.property('message').that.equals('Issue updated successfully'); | ||
// }); | ||
|
||
it('should return an error response when the request is invalid', async () => { | ||
const res = await chai.request(server) | ||
.post('api/actions/student/tm2005/9999') | ||
.send({ issueindex: '2', studentNetID: 's123456', comments: 'Test comment', currentStatus: 'open' }); | ||
|
||
expect(res.status).to.equal(500); | ||
// expect(res.body).to.have.property('message').that.equals('Invalid request'); | ||
}); | ||
}); |