From 010126787eb9293272950f29fff9542da782221f Mon Sep 17 00:00:00 2001 From: kistgab Date: Mon, 25 Mar 2024 11:53:08 -0300 Subject: [PATCH] feat: increase code test coverage --- .../db-load-survey-result.spec.ts | 12 ++++++++++++ src/domain/test/mock-survey.ts | 11 ----------- src/infra/db/mongodb/helpers/query-builder.ts | 5 ----- .../answer-survey-controller.spec.ts | 17 +++++++++++++++++ .../survey-answer/answer-survey-controller.ts | 10 ++++++---- .../load-survey-result-controller.spec.ts | 9 +++++++++ 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/data/usecases/survey-answer/load-survey-result/db-load-survey-result.spec.ts b/src/data/usecases/survey-answer/load-survey-result/db-load-survey-result.spec.ts index 6789fbd..af96c69 100644 --- a/src/data/usecases/survey-answer/load-survey-result/db-load-survey-result.spec.ts +++ b/src/data/usecases/survey-answer/load-survey-result/db-load-survey-result.spec.ts @@ -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(); + }); }); diff --git a/src/domain/test/mock-survey.ts b/src/domain/test/mock-survey.ts index 87f8fa0..7606713 100644 --- a/src/domain/test/mock-survey.ts +++ b/src/domain/test/mock-survey.ts @@ -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"; @@ -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: [ diff --git a/src/infra/db/mongodb/helpers/query-builder.ts b/src/infra/db/mongodb/helpers/query-builder.ts index a5a99a9..2ffe8b2 100644 --- a/src/infra/db/mongodb/helpers/query-builder.ts +++ b/src/infra/db/mongodb/helpers/query-builder.ts @@ -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; diff --git a/src/presentation/controllers/survey-answer/answer-survey-controller.spec.ts b/src/presentation/controllers/survey-answer/answer-survey-controller.spec.ts index 3950716..d89ec39 100644 --- a/src/presentation/controllers/survey-answer/answer-survey-controller.spec.ts +++ b/src/presentation/controllers/survey-answer/answer-survey-controller.spec.ts @@ -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(); diff --git a/src/presentation/controllers/survey-answer/answer-survey-controller.ts b/src/presentation/controllers/survey-answer/answer-survey-controller.ts index cb5e450..6276581 100644 --- a/src/presentation/controllers/survey-answer/answer-survey-controller.ts +++ b/src/presentation/controllers/survey-answer/answer-survey-controller.ts @@ -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, diff --git a/src/presentation/controllers/survey-answer/load-survey-result/load-survey-result-controller.spec.ts b/src/presentation/controllers/survey-answer/load-survey-result/load-survey-result-controller.spec.ts index d919bb9..c479e6b 100644 --- a/src/presentation/controllers/survey-answer/load-survey-result/load-survey-result-controller.spec.ts +++ b/src/presentation/controllers/survey-answer/load-survey-result/load-survey-result-controller.spec.ts @@ -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 { return { @@ -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({});