Skip to content

Commit

Permalink
feat: increase code test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ogabrielkist committed Mar 25, 2024
1 parent d44acbe commit 0101267
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ describe("DbLoadSurveyResult UseCase", () => {
};
expect(result).toEqual(expectedResult);
});

it("should return null if there is no survey with the specified id", async () => {
const { sut, loadSurveyResultRepositoryStub, findSurveyByIdRepositoryStub } = createSut();
jest
.spyOn(loadSurveyResultRepositoryStub, "loadBySurveyId")
.mockReturnValueOnce(Promise.resolve(null));
jest.spyOn(findSurveyByIdRepositoryStub, "findById").mockReturnValueOnce(Promise.resolve(null));

const result = await sut.load("any_non_existing_id");

expect(result).toBeNull();
});
});
11 changes: 0 additions & 11 deletions src/domain/test/mock-survey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SurveyAnswerModel } from "@src/data/models/save-survey-answer-model";
import { AddSurveyModel, SurveyModel } from "@src/data/models/survey-model";
import { SurveyResultModel } from "@src/data/models/survey-result-model";
import { InputAddSurveyDto } from "@src/domain/dtos/add-survey-dto";
Expand Down Expand Up @@ -57,16 +56,6 @@ export function mockSurveyModelList(): SurveyModel[] {
];
}

export function mockSurveyAnswerModel(): SurveyAnswerModel {
return {
id: "valid_id",
accountId: "valid_account_id",
surveyId: "valid_survey_id",
answer: "valid_answer",
date: new Date(),
};
}

export function mockSurveyResultModel(): SurveyResultModel {
return {
answers: [
Expand Down
5 changes: 0 additions & 5 deletions src/infra/db/mongodb/helpers/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ export class QueryBuilder {
return this;
}

addFields(data: object): QueryBuilder {
this.query.push({ $addFields: data });
return this;
}

sort(data: object): QueryBuilder {
this.query.push({ $sort: data });
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ describe("AnswerSurvey Controller", () => {
expect(response).toEqual(unprocessableContent(new InvalidParamError("answer")));
});

it("should return 422 if no surveyId is provided", async () => {
const { sut } = createSut();

const response = await sut.handle({
// @ts-expect-error - ignoring for test
params: {},
body: {
answer: "invalid_answer",
date: new Date(),
accountId: "any_account_id",
surveyId: "any_survey_id",
},
});

expect(response).toEqual(unprocessableContent(new MissingParamError("surveyId")));
});

it("should return 422 if no body was provided", async () => {
const { sut } = createSut();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ export class AnswerSurveyController
if (!body) {
return unprocessableContent(new MissingParamError("body"));
}
const survey = await this.listSurveyById.list(params?.surveyId || "");
if (!params?.surveyId) {
return unprocessableContent(new MissingParamError("surveyId"));
}
const survey = await this.listSurveyById.list(params.surveyId);
if (!survey) {
return unprocessableContent(new Error("Survey not found"));
}
const surveyAnswers = survey.answers.map((answer) => answer.answer);
if (!surveyAnswers.includes(body?.answer || "")) {
if (!surveyAnswers.includes(body.answer)) {
return unprocessableContent(new InvalidParamError("answer"));
}
const response = await this.answerSurvey.answer({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
surveyId: params!.surveyId,
surveyId: params.surveyId,
answer: body.answer,
date: new Date(),
accountId: body.accountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
unprocessableContent,
} from "@src/presentation/helpers/http/http-helper";
import { HttpRequest } from "@src/presentation/protocols/http";
import * as Mockdate from "mockdate";

function createRequest(): HttpRequest<void, { surveyId: string }> {
return {
Expand Down Expand Up @@ -40,6 +41,14 @@ function createSut(): SutTypes {
}

describe("LoadSurveyResultController", () => {
beforeAll(() => {
Mockdate.set(new Date());
});

afterAll(() => {
Mockdate.reset();
});

it("should return 422 when no surveyId was provided", async () => {
const { sut } = createSut();
const result = await sut.handle({});
Expand Down

0 comments on commit 0101267

Please sign in to comment.