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 #219 from agiledev-students-fall2023/user/26/task/…
…155/Test-issue-details-API-endpoint-for-Admin-side User/26/task/155/test issue details api endpoint for admin side
Showing
1 changed file
with
119 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,119 @@ | ||
/* eslint-disable */ | ||
|
||
// Unit Testing | ||
import chai, { assert } from "chai"; | ||
import sinon from "sinon"; | ||
import axios from "axios"; | ||
import fs from "fs/promises"; | ||
import { adminIssueViewDetailsHandler } from "../src/controllers/adminIssueViewDetailsHandler.js"; | ||
|
||
const data = await fs.readFile("./public/json/mockapi.json", "utf8"); | ||
const mockData = JSON.parse(data); | ||
// Filter the mock data to match the handler's expected behavior | ||
|
||
const filteredData = mockData.filter( | ||
(item) => String(item.index) === String("3") | ||
); | ||
|
||
describe("Unit Tests for Retrieval of Admin specific indexed issue of a department", () => { | ||
let req, res, axiosGetStub; | ||
let sendSpy, jsonSpy; | ||
|
||
beforeEach(() => { | ||
req = { params: { paramName: "3" } }; | ||
res = { | ||
json: sinon.spy(), | ||
status: sinon.stub().returns({ send: sinon.spy() }) // Stubbed here | ||
}; | ||
sendSpy = res.status().send; // Save a reference to the send spy created above | ||
jsonSpy = res.json; // Save a reference to the json spy | ||
axiosGetStub = sinon.stub(axios, "get"); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); // Restores all stubs and spies created through sinon | ||
}); | ||
|
||
it('should filter and return the correct number of data records for the specific indexed issue in that department"', async () => { | ||
// Stub the axios call to resolve with the entire mock data | ||
axiosGetStub.resolves({ data: mockData }); | ||
|
||
// Execute the handler | ||
await adminIssueViewDetailsHandler(req, res); | ||
|
||
// Assert the axios call was made once | ||
assert(axiosGetStub.calledOnce); | ||
// Assert the correct amount of data is returned | ||
assert.equal( | ||
res.json.firstCall.args[0].length, | ||
filteredData.length, | ||
"Number of records should match the filtered data length" | ||
); | ||
// Assert the returned data matches the filtered data | ||
assert.deepEqual( | ||
res.json.firstCall.args[0], | ||
filteredData, | ||
"Returned data should match the filtered data" | ||
); | ||
}); | ||
|
||
// Edge Case Test Case | ||
it('should return an empty array when specific index of the issue is not found', async () => { | ||
req.params.paramName = "darkSoul"; | ||
axiosGetStub.resolves({ data: mockData }); | ||
await adminIssueViewDetailsHandler(req, res); | ||
assert.isTrue(axiosGetStub.calledOnce, "axios.get should be called once"); | ||
assert.equal( | ||
res.json.firstCall.args[0].length, | ||
0, | ||
"Number of records should be 0 when paramName is not found" | ||
); | ||
}); | ||
}); | ||
|
||
// Integration Testing | ||
import chaiHttp from "chai-http"; | ||
chai.use(chaiHttp); | ||
|
||
describe("Integration Tests for Retrieval of Admin specific indexed issue of a department Endpoints", () => { | ||
describe("GET /api/issues/student/:department/:paramName", () => { | ||
it("should retrieve issues for a given department with a specific issue index", async () => { | ||
const department = "IT"; | ||
const issueindex = "3"; | ||
const res = await chai | ||
.request("http://localhost:5000") | ||
.get(`/api/issues/admin/${department}/${issueindex}`); | ||
|
||
assert.equal(res.status, 200, "Response status should be 200"); | ||
assert.isArray(res.body, "Response body should be an array"); | ||
assert.lengthOf( | ||
res.body, | ||
filteredData.length, | ||
`Response body should have ${filteredData.length} records` | ||
); | ||
|
||
// Check if each record in the response matches the corresponding record in filteredData | ||
res.body.forEach((record, index) => { | ||
assert.deepEqual( | ||
record, | ||
filteredData[index], | ||
`Record at index ${index} should match the expected data` | ||
); | ||
}); | ||
}); | ||
|
||
it("should handle requests for a non-existent department & non existent issue index gracefully", async () => { | ||
const nonExistentDepartment = "nonexistent"; | ||
const nonExistentissueindex = "nonexistent"; | ||
const res = await chai | ||
.request("http://localhost:5000") | ||
.get(`/api/issues/admin/${nonExistentDepartment}/${nonExistentissueindex}`); | ||
assert.lengthOf( | ||
res.body, | ||
0, | ||
"Response body should be empty for nonExistentDepartment and nonExistentissueIndex" | ||
); | ||
}); | ||
}); | ||
}); | ||
/* eslint-enable */ |