-
Notifications
You must be signed in to change notification settings - Fork 0
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 #8 from kistgab/feat/load-survey-result
Feat/load survey result
- Loading branch information
Showing
24 changed files
with
745 additions
and
71 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,13 @@ | ||
export type SurveyResultModel = { | ||
surveyId: string; | ||
question: string; | ||
date: Date; | ||
answers: SurveyResultAnswerModel[]; | ||
}; | ||
|
||
type SurveyResultAnswerModel = { | ||
image?: string; | ||
answer: string; | ||
count: number; | ||
percent: number; | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/data/protocols/db/survey-answer/load-survey-result-repository.ts
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,5 @@ | ||
import { SurveyResultModel } from "@src/data/models/survey-result-model"; | ||
|
||
export interface LoadSurveyResultRepository { | ||
loadBySurveyId(id: string): Promise<SurveyResultModel | null>; | ||
} |
7 changes: 2 additions & 5 deletions
7
src/data/protocols/db/survey/save-survey-answer-repository.ts
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 |
---|---|---|
@@ -1,8 +1,5 @@ | ||
import { | ||
SaveSurveyAnswerModel, | ||
SurveyAnswerModel, | ||
} from "@src/data/models/save-survey-answer-model"; | ||
import { SaveSurveyAnswerModel } from "@src/data/models/save-survey-answer-model"; | ||
|
||
export interface SaveSurveyAnswerRepository { | ||
save(data: SaveSurveyAnswerModel): Promise<SurveyAnswerModel>; | ||
save(data: SaveSurveyAnswerModel): Promise<void>; | ||
} |
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,12 @@ | ||
import { SurveyResultModel } from "@src/data/models/survey-result-model"; | ||
import { LoadSurveyResultRepository } from "@src/data/protocols/db/survey-answer/load-survey-result-repository"; | ||
import { mockSurveyResultModel } from "@src/domain/test/mock-survey"; | ||
|
||
export function mockLoadSurveyResultRepository(): LoadSurveyResultRepository { | ||
class LoadSurveyResultRepositoryStub implements LoadSurveyResultRepository { | ||
async loadBySurveyId(): Promise<SurveyResultModel> { | ||
return Promise.resolve(mockSurveyResultModel()); | ||
} | ||
} | ||
return new LoadSurveyResultRepositoryStub(); | ||
} |
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
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
16 changes: 11 additions & 5 deletions
16
src/data/usecases/survey-answer/answer-survey/db-answer-survey.ts
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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import { SurveyAnswerModel } from "@src/data/models/save-survey-answer-model"; | ||
import { SurveyResultModel } from "@src/data/models/survey-result-model"; | ||
import { LoadSurveyResultRepository } from "@src/data/protocols/db/survey-answer/load-survey-result-repository"; | ||
import { SaveSurveyAnswerRepository } from "@src/data/protocols/db/survey/save-survey-answer-repository"; | ||
import { InputAnswerSurveyDto } from "@src/domain/dtos/answer-survey-dto"; | ||
import AnswerSurvey from "@src/domain/usecases/survey-answer/answer-survey"; | ||
|
||
export class DbAnswerSurvey implements AnswerSurvey { | ||
constructor(private readonly saveSurveyAnswerRepository: SaveSurveyAnswerRepository) {} | ||
constructor( | ||
private readonly saveSurveyAnswerRepository: SaveSurveyAnswerRepository, | ||
private readonly loadSurveyResultRepository: LoadSurveyResultRepository, | ||
) {} | ||
|
||
async answer(data: InputAnswerSurveyDto): Promise<SurveyAnswerModel> { | ||
const savedAnswer = await this.saveSurveyAnswerRepository.save(data); | ||
return savedAnswer; | ||
async answer(data: InputAnswerSurveyDto): Promise<SurveyResultModel> { | ||
await this.saveSurveyAnswerRepository.save(data); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const surveyResult = (await this.loadSurveyResultRepository.loadBySurveyId(data.surveyId))!; | ||
return surveyResult; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/data/usecases/survey-answer/load-survey-result/db-load-survey-result.spec.ts
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,100 @@ | ||
import { SurveyResultModel } from "@src/data/models/survey-result-model"; | ||
import { LoadSurveyResultRepository } from "@src/data/protocols/db/survey-answer/load-survey-result-repository"; | ||
import { FindSurveyByIdRepository } from "@src/data/protocols/db/survey/find-by-id-surveys-repository"; | ||
import { mockFindSurveyByIdRepository } from "@src/data/test/mock-db-survey"; | ||
import { mockLoadSurveyResultRepository } from "@src/data/test/mock-db-survey-result"; | ||
import { DbLoadSurveyResult } from "@src/data/usecases/survey-answer/load-survey-result/db-load-survey-result"; | ||
import { mockSurveyResultModel } from "@src/domain/test/mock-survey"; | ||
import * as Mockdate from "mockdate"; | ||
|
||
type SutTypes = { | ||
sut: DbLoadSurveyResult; | ||
loadSurveyResultRepositoryStub: LoadSurveyResultRepository; | ||
findSurveyByIdRepositoryStub: FindSurveyByIdRepository; | ||
}; | ||
|
||
function createSut(): SutTypes { | ||
const loadSurveyResultRepositoryStub = mockLoadSurveyResultRepository(); | ||
const findSurveyByIdRepositoryStub = mockFindSurveyByIdRepository(); | ||
const sut = new DbLoadSurveyResult(loadSurveyResultRepositoryStub, findSurveyByIdRepositoryStub); | ||
return { | ||
sut, | ||
loadSurveyResultRepositoryStub, | ||
findSurveyByIdRepositoryStub, | ||
}; | ||
} | ||
|
||
describe("DbLoadSurveyResult UseCase", () => { | ||
beforeAll(() => { | ||
Mockdate.set(new Date()); | ||
}); | ||
|
||
afterAll(() => { | ||
Mockdate.reset(); | ||
}); | ||
|
||
it("Should call LoadSurveyResultRepository", async () => { | ||
const { sut, loadSurveyResultRepositoryStub } = createSut(); | ||
const loadBySurveyIdSpy = jest.spyOn(loadSurveyResultRepositoryStub, "loadBySurveyId"); | ||
|
||
await sut.load("any_survey_id"); | ||
|
||
expect(loadBySurveyIdSpy).toHaveBeenCalledWith("any_survey_id"); | ||
}); | ||
|
||
it("should throw when LoadSurveyResultRepository throws", async () => { | ||
const { sut, loadSurveyResultRepositoryStub } = createSut(); | ||
jest | ||
.spyOn(loadSurveyResultRepositoryStub, "loadBySurveyId") | ||
.mockReturnValueOnce(Promise.reject(new Error("Repository error"))); | ||
|
||
await expect(sut.load("any_id")).rejects.toThrow(new Error("Repository error")); | ||
}); | ||
|
||
it("should return a SurveyResultModel on success", async () => { | ||
const { sut } = createSut(); | ||
|
||
const result = await sut.load("any_id"); | ||
|
||
expect(result).toEqual(mockSurveyResultModel()); | ||
}); | ||
|
||
it("should call FindSurveyByIdRepository if LoadSurveyResultRepository returns null", async () => { | ||
const { sut, loadSurveyResultRepositoryStub, findSurveyByIdRepositoryStub } = createSut(); | ||
jest | ||
.spyOn(loadSurveyResultRepositoryStub, "loadBySurveyId") | ||
.mockReturnValueOnce(Promise.resolve(null)); | ||
const findById = jest.spyOn(findSurveyByIdRepositoryStub, "findById"); | ||
|
||
await sut.load("any_id"); | ||
|
||
expect(findById).toHaveBeenCalledWith("any_id"); | ||
}); | ||
|
||
it("should return a SurveyResultModel that every answers have count and percent as 0", async () => { | ||
const { sut, loadSurveyResultRepositoryStub } = createSut(); | ||
jest | ||
.spyOn(loadSurveyResultRepositoryStub, "loadBySurveyId") | ||
.mockReturnValueOnce(Promise.resolve(null)); | ||
|
||
const result = await sut.load("any_id"); | ||
|
||
const expectedResult: SurveyResultModel = { | ||
...mockSurveyResultModel(), | ||
answers: [{ ...mockSurveyResultModel().answers[0], count: 0, percent: 0 }], | ||
}; | ||
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(); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
src/data/usecases/survey-answer/load-survey-result/db-load-survey-result.ts
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,33 @@ | ||
import { SurveyResultModel } from "@src/data/models/survey-result-model"; | ||
import { LoadSurveyResultRepository } from "@src/data/protocols/db/survey-answer/load-survey-result-repository"; | ||
import { FindSurveyByIdRepository } from "@src/data/protocols/db/survey/find-by-id-surveys-repository"; | ||
import { LoadSurveyResult } from "@src/domain/usecases/survey-answer/load-survey-result"; | ||
|
||
export class DbLoadSurveyResult implements LoadSurveyResult { | ||
constructor( | ||
private readonly loadSurveyResultRepository: LoadSurveyResultRepository, | ||
private readonly findSurveyByIdRepository: FindSurveyByIdRepository, | ||
) {} | ||
|
||
async load(surveyId: string): Promise<SurveyResultModel | null> { | ||
let surveyResult = await this.loadSurveyResultRepository.loadBySurveyId(surveyId); | ||
if (surveyResult) { | ||
return surveyResult; | ||
} | ||
const survey = await this.findSurveyByIdRepository.findById(surveyId); | ||
if (!survey) { | ||
return null; | ||
} | ||
surveyResult = { | ||
surveyId: survey.id, | ||
question: survey.question, | ||
date: survey.date, | ||
answers: survey.answers.map((answer) => ({ | ||
...answer, | ||
count: 0, | ||
percent: 0, | ||
})), | ||
}; | ||
return surveyResult; | ||
} | ||
} |
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
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
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,5 @@ | ||
import { SurveyResultModel } from "@src/data/models/survey-result-model"; | ||
|
||
export interface LoadSurveyResult { | ||
load(surveyId: string): Promise<SurveyResultModel | null>; | ||
} |
Oops, something went wrong.