Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/controllers/problemSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ const problemSetController = {
} else {
try {
const problemSetData: IProblemSet = {
...req.body
...req.body,
problemCount: 0
};

const newProblemSet: IProblemSetModel = await problemSetDBInteractions.create(
Expand Down Expand Up @@ -118,7 +119,8 @@ const problemSetController = {
});
else {
const updatedProblemSetBody: IProblemSet = {
...req.body
...req.body,
problemCount: problemSet.problemCount
};

const updatedProblemSet: IProblemSetModel = await problemSetDBInteractions.update(
Expand Down
40 changes: 38 additions & 2 deletions tests/unit/controllers/problemSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ describe("Problem sets controller tests", () => {
);
await problemSetController.create(req, mockRes);
sinon.assert.calledOnce(stubs.problemSetDB.create);
sinon.assert.calledWith(stubs.problemSetDB.create, req.body);
sinon.assert.calledWith(stubs.problemSetDB.create, {
...req.body,
problemCount: 0
});
sinon.assert.calledWith(mockRes.status, statusCodes.SUCCESS);
sinon.assert.calledWith(mockRes.json, testProblemSetModel1);
});
Expand All @@ -259,7 +262,10 @@ describe("Problem sets controller tests", () => {
);
await problemSetController.create(req, mockRes);
sinon.assert.calledOnce(stubs.problemSetDB.create);
sinon.assert.calledWith(stubs.problemSetDB.create, req.body);
sinon.assert.calledWith(stubs.problemSetDB.create, {
...req.body,
problemCount: 0
});
sinon.assert.calledWith(mockRes.status, statusCodes.SERVER_ERROR);
});
});
Expand Down Expand Up @@ -306,6 +312,36 @@ describe("Problem sets controller tests", () => {
sinon.assert.calledWith(mockRes.json, updatedUser);
});

it("status 200: returns successfully unchanged problemCount", async () => {
stubs.validators.validationResult.returns(
<any>emptyValidationError()
);
stubs.problemSetDB.find.resolves(testProblemSetModel1);

const updatedUser = JSON.parse(JSON.stringify(testProblemSet1));
updatedUser._id = req.params.problemSetId;
updatedUser.title = "Test Problem Set Title 1 Updated";
updatedUser.problemCount = 9;
stubs.problemSetDB.update.resolves(updatedUser);
Comment on lines +321 to +325
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusing variable name updatedUser. Change to updatedProblemSet.
Also, why do you need to stringify then parse? Isn't testProblemSet1 already a javascript object (IProblemSet)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept both in line with test that precedes - will change after I update with Val's changes.


req.body.title = "Test Problem Set Title 1 Updated";
await problemSetController.update(req, mockRes);

sinon.assert.calledOnce(stubs.problemSetDB.find);
sinon.assert.calledOnce(stubs.problemSetDB.update);
sinon.assert.calledWith(
stubs.problemSetDB.find,
req.params.problemSetId
);
sinon.assert.calledWith(
stubs.problemSetDB.update,
req.params.problemSetId,
{ ...req.body, problemCount: testProblemSet1.problemCount }
);
sinon.assert.calledWith(mockRes.status, statusCodes.SUCCESS);
sinon.assert.calledWith(mockRes.json, updatedUser);
});

it("status 200: returns successfully multiple updated fields", async () => {
stubs.validators.validationResult.returns(
<any>emptyValidationError()
Expand Down