Skip to content

Commit

Permalink
wrote 2 new test cases and and one no content cond.
Browse files Browse the repository at this point in the history
  • Loading branch information
vikasosmium committed Nov 18, 2024
1 parent 98e6607 commit 71b2876
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/arts.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ const getUserArts = async (req, res) => {
try {
const userId = req.params.userId;
const arts = await artsQuery.fetchUserArts(userId);

if (!arts || arts.length === 0) {
return res.status(204).send();
}

return res.json({
message: `User Arts of userId ${userId} returned successfully`,
arts,
Expand Down
47 changes: 47 additions & 0 deletions test/integration/arts.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const chai = require("chai");
const { expect } = chai;
const chaiHttp = require("chai-http");
const sinon = require("sinon");
const artsQuery = require("../../models/arts");

const app = require("../../server");
const authService = require("../../services/authService");
Expand Down Expand Up @@ -187,5 +189,50 @@ describe("Arts", function () {
return done();
});
});

it("Should return 204 No Content if no arts are found", function (done) {
sinon.stub(artsQuery, "fetchUserArts").resolves([]);

chai
.request(app)
.get(`/arts/${userId}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.end((err, res) => {
artsQuery.fetchUserArts.restore();

if (err) {
return done(err);
}

expect(res).to.have.status(204);
expect(res.body).to.deep.equal({});
return done();
});
});

it("Should return 500 Internal Server Error if there is an exception", function (done) {
sinon.stub(artsQuery, "fetchUserArts").throws(new Error("Database error"));

chai
.request(app)
.get(`/arts/${userId}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.end((err, res) => {
artsQuery.fetchUserArts.restore();

if (err) {
return done(err);
}

expect(res).to.have.status(500);
expect(res.body).to.deep.equal({
statusCode: 500,
error: "Internal Server Error",
message: "An internal server error occurred",
});

return done();
});
});
});
});

0 comments on commit 71b2876

Please sign in to comment.