Skip to content

Commit 05adb00

Browse files
committed
test: change SurveyResult to return isCurrentAccountAnswer property
1 parent 782abf5 commit 05adb00

File tree

8 files changed

+149
-62
lines changed

8 files changed

+149
-62
lines changed

src/data/tests/mock-db-survey-result.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ export class LoadSurveyResultRepositorySpy
2020
{
2121
surveyResultModel = mockSaveSurveyResultModel();
2222
surveyId: string;
23+
accountId: string;
2324

24-
async loadBySurveyId(surveyId: string): Promise<SurveyResultModel> {
25+
async loadBySurveyId(
26+
surveyId: string,
27+
accountId: string
28+
): Promise<SurveyResultModel> {
2529
this.surveyId = surveyId;
30+
this.accountId = accountId;
2631
return Promise.resolve(this.surveyResultModel);
2732
}
2833
}

src/data/usecases/survey-result/load-survey-result/db-load-survey-result.spec.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
LoadSurveyResultRepositorySpy,
44
LoadSurveyByIdRepositorySpy,
55
} from '@/data/tests';
6-
import { mockSaveSurveyResultModel, throwError } from '@/domain/tests';
6+
import { throwError } from '@/domain/tests';
77
import MockDate from 'mockdate';
88
import { faker } from '@faker-js/faker';
99

@@ -24,6 +24,7 @@ const makeSut = (): SutTypes => {
2424
};
2525

2626
let surveyId: string;
27+
let accountId: string;
2728

2829
describe('DbLoadSurveyResult UseCase', () => {
2930
beforeAll(() => MockDate.set(new Date()));
@@ -32,28 +33,30 @@ describe('DbLoadSurveyResult UseCase', () => {
3233

3334
beforeEach(() => {
3435
surveyId = faker.datatype.uuid();
36+
accountId = faker.datatype.uuid();
3537
});
3638

37-
test('Should call LoadSurveyResultRepository', async () => {
39+
test('Should call LoadSurveyResultRepository with correct values', async () => {
3840
const { sut, loadSurveyResultRepositorySpy } = makeSut();
39-
await sut.load(surveyId);
41+
await sut.load(surveyId, accountId);
4042
expect(loadSurveyResultRepositorySpy.surveyId).toBe(surveyId);
43+
expect(loadSurveyResultRepositorySpy.accountId).toBe(accountId);
4144
});
4245

4346
test('Should throw if LoadSurveyResultRepository throws', async () => {
4447
const { sut, loadSurveyResultRepositorySpy } = makeSut();
4548
jest
4649
.spyOn(loadSurveyResultRepositorySpy, 'loadBySurveyId')
4750
.mockImplementationOnce(throwError);
48-
const promise = sut.load(surveyId);
51+
const promise = sut.load(surveyId, accountId);
4952
await expect(promise).rejects.toThrow();
5053
});
5154

5255
test('Should call LoadSurveyByIdRepository if LoadSurveyResultRepository returns null', async () => {
5356
const { sut, loadSurveyResultRepositorySpy, loadSurveyByIdRepositorySpy } =
5457
makeSut();
5558
loadSurveyResultRepositorySpy.surveyResultModel = null;
56-
await sut.load(surveyId);
59+
await sut.load(surveyId, accountId);
5760
expect(loadSurveyByIdRepositorySpy.id).toBe(surveyId);
5861
});
5962

@@ -62,15 +65,15 @@ describe('DbLoadSurveyResult UseCase', () => {
6265
makeSut();
6366
loadSurveyResultRepositorySpy.surveyResultModel = null;
6467
loadSurveyByIdRepositorySpy.surveyModel = null;
65-
const surveyResult = await sut.load(surveyId);
68+
const surveyResult = await sut.load(surveyId, accountId);
6669
expect(surveyResult).toBeNull();
6770
});
6871

6972
test('Should return surveyResultModel with all answers with count and percent zero if LoadSurveyResultRepository returns null', async () => {
7073
const { sut, loadSurveyResultRepositorySpy, loadSurveyByIdRepositorySpy } =
7174
makeSut();
7275
loadSurveyResultRepositorySpy.surveyResultModel = null;
73-
const surveyResult = await sut.load(surveyId);
76+
const surveyResult = await sut.load(surveyId, accountId);
7477
const { surveyModel } = loadSurveyByIdRepositorySpy;
7578
expect(surveyResult).toEqual({
7679
surveyId: surveyModel.id,
@@ -80,14 +83,15 @@ describe('DbLoadSurveyResult UseCase', () => {
8083
Object.assign({}, answer, {
8184
count: 0,
8285
percent: 0,
86+
isCurrentAccountAnswer: false,
8387
})
8488
),
8589
});
8690
});
8791

8892
test('Should return surveyResultModel on success', async () => {
8993
const { sut, loadSurveyResultRepositorySpy } = makeSut();
90-
const surveyResult = await sut.load(surveyId);
94+
const surveyResult = await sut.load(surveyId, accountId);
9195
expect(surveyResult).toEqual(
9296
loadSurveyResultRepositorySpy.surveyResultModel
9397
);

src/data/usecases/survey-result/save-survey-result/db-save-survey-result.spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { DbSaveSurveyResult } from './db-save-survey-result';
2-
import {
3-
throwError,
4-
mockSaveSurveyResultModel,
5-
mockSaveSurveyResultParams,
6-
} from '@/domain/tests';
2+
import { throwError, mockSaveSurveyResultParams } from '@/domain/tests';
73
import {
84
LoadSurveyResultRepositorySpy,
95
SaveSurveyResultRepositorySpy,

src/domain/tests/mock-survey-result.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ export const mockSaveSurveyResultModel = (): SurveyResultModel => ({
1717
count: faker.datatype.number({ min: 0, max: 1000 }),
1818
percent: faker.datatype.number({ min: 0, max: 100 }),
1919
image: faker.image.imageUrl(),
20+
isCurrentAccountAnswer: faker.datatype.boolean(),
2021
},
2122
{
2223
answer: faker.random.word(),
2324
answerId: faker.datatype.uuid(),
2425
count: faker.datatype.number({ min: 0, max: 1000 }),
2526
percent: faker.datatype.number({ min: 0, max: 100 }),
2627
image: faker.image.imageUrl(),
28+
isCurrentAccountAnswer: faker.datatype.boolean(),
2729
},
2830
],
2931
date: faker.date.recent(),

src/infra/db/mongodb/survey-result/survey-result-mongo-repository.spec.ts

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,37 +102,113 @@ describe('SurveyResultMongoRepository', () => {
102102
});
103103

104104
describe('loadBySurveyId()', () => {
105-
test('Should load survey result', async () => {
105+
test('Should load survey result with two users', async () => {
106106
const sut = makeSut();
107107
const survey = await mockSurvey();
108-
const account = await mockAccount();
108+
const firstAccount = await mockAccount();
109+
const secondAccount = await mockAccount();
109110
await surveyResultCollection.insertMany([
110111
{
111112
surveyId: MongoHelper.objectId(survey.id),
112-
accountId: MongoHelper.objectId(account.id),
113+
accountId: MongoHelper.objectId(firstAccount.id),
113114
answerId: survey.answers[0].answerId,
114115
date: new Date(),
115116
},
116117
{
117118
surveyId: MongoHelper.objectId(survey.id),
118-
accountId: MongoHelper.objectId(account.id),
119+
accountId: MongoHelper.objectId(secondAccount.id),
119120
answerId: survey.answers[0].answerId,
120121
date: new Date(),
121122
},
122123
]);
123-
const surveyResult = await sut.loadBySurveyId(survey.id);
124+
const surveyResult = await sut.loadBySurveyId(survey.id, firstAccount.id);
124125
expect(surveyResult).toBeTruthy();
125126
expect(surveyResult.surveyId).toEqual(survey.id);
126127
expect(surveyResult.answers[0].count).toBe(2);
127128
expect(surveyResult.answers[0].percent).toBe(100);
129+
expect(surveyResult.answers[0].isCurrentAccountAnswer).toBe(true);
128130
expect(surveyResult.answers[1].count).toBe(0);
129131
expect(surveyResult.answers[1].percent).toBe(0);
132+
expect(surveyResult.answers[1].isCurrentAccountAnswer).toBe(false);
133+
});
134+
135+
test('Should load survey result with three users', async () => {
136+
const sut = makeSut();
137+
const survey = await mockSurvey();
138+
const firstAccount = await mockAccount();
139+
const secondAccount = await mockAccount();
140+
const thirdAccount = await mockAccount();
141+
await surveyResultCollection.insertMany([
142+
{
143+
surveyId: MongoHelper.objectId(survey.id),
144+
accountId: MongoHelper.objectId(firstAccount.id),
145+
answerId: survey.answers[0].answerId,
146+
date: new Date(),
147+
},
148+
{
149+
surveyId: MongoHelper.objectId(survey.id),
150+
accountId: MongoHelper.objectId(secondAccount.id),
151+
answerId: survey.answers[1].answerId,
152+
date: new Date(),
153+
},
154+
{
155+
surveyId: MongoHelper.objectId(survey.id),
156+
accountId: MongoHelper.objectId(thirdAccount.id),
157+
answerId: survey.answers[1].answerId,
158+
date: new Date(),
159+
},
160+
]);
161+
const surveyResult = await sut.loadBySurveyId(
162+
survey.id,
163+
secondAccount.id
164+
);
165+
console.log(surveyResult);
166+
expect(surveyResult).toBeTruthy();
167+
expect(surveyResult.surveyId).toEqual(survey.id);
168+
expect(surveyResult.answers[0].count).toBe(2);
169+
expect(surveyResult.answers[0].percent).toBe(67);
170+
expect(surveyResult.answers[0].isCurrentAccountAnswer).toBe(true);
171+
expect(surveyResult.answers[1].count).toBe(1);
172+
expect(surveyResult.answers[1].percent).toBe(33);
173+
expect(surveyResult.answers[1].isCurrentAccountAnswer).toBe(false);
174+
});
175+
176+
test('Should load survey result with third user no answers', async () => {
177+
const sut = makeSut();
178+
const survey = await mockSurvey();
179+
const firstAccount = await mockAccount();
180+
const secondAccount = await mockAccount();
181+
const thirdAccount = await mockAccount();
182+
await surveyResultCollection.insertMany([
183+
{
184+
surveyId: MongoHelper.objectId(survey.id),
185+
accountId: MongoHelper.objectId(firstAccount.id),
186+
answerId: survey.answers[0].answerId,
187+
date: new Date(),
188+
},
189+
{
190+
surveyId: MongoHelper.objectId(survey.id),
191+
accountId: MongoHelper.objectId(secondAccount.id),
192+
answerId: survey.answers[1].answerId,
193+
date: new Date(),
194+
},
195+
]);
196+
const surveyResult = await sut.loadBySurveyId(survey.id, thirdAccount.id);
197+
expect(surveyResult).toBeTruthy();
198+
expect(surveyResult.surveyId).toEqual(survey.id);
199+
expect(surveyResult.answers[0].count).toBe(1);
200+
expect(surveyResult.answers[0].percent).toBe(50);
201+
expect(surveyResult.answers[0].isCurrentAccountAnswer).toBe(false);
202+
expect(surveyResult.answers[1].count).toBe(1);
203+
expect(surveyResult.answers[1].percent).toBe(50);
204+
expect(surveyResult.answers[1].isCurrentAccountAnswer).toBe(false);
130205
});
131206

132207
test("Should return null if survey result don't have answers", async () => {
133208
const sut = makeSut();
134209
const survey = await mockSurvey();
135-
const surveyResult = await sut.loadBySurveyId(survey.id);
210+
const account = await mockAccount();
211+
const surveyResult = await sut.loadBySurveyId(survey.id, account.id);
136212
expect(surveyResult).toBeNull();
137213
});
138214
});

0 commit comments

Comments
 (0)