Skip to content

Commit

Permalink
added unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueweiand committed Nov 3, 2023
1 parent 3ddbfef commit 1dd6045
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/modules/question/use-case/get-many-questions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Test, TestingModule } from '@nestjs/testing';
import { QuestionRepository } from '@app/common';
import { Question } from '@prisma/client';
import { GetManyQuestionsUseCase } from './get-many-questions';

class QuestionRepositoryMock {
findMany = jest.fn();
}

describe('GetManyQuestionsUseCase', () => {
let getManyQuestionsUseCase: GetManyQuestionsUseCase;
let questionRepository: QuestionRepository;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
GetManyQuestionsUseCase,
{
provide: QuestionRepository,
useClass: QuestionRepositoryMock,
},
],
}).compile();

getManyQuestionsUseCase = module.get<GetManyQuestionsUseCase>(GetManyQuestionsUseCase);
questionRepository = module.get<QuestionRepository>(QuestionRepository);
});

it('should be defined', () => {
expect(getManyQuestionsUseCase).toBeDefined();
});

it('should get many questions', async () => {
const questions: Question[] = [
{
id: 'aaaaa-sdfsdfsd-sdfsdfsd-bbbbb',
content: 'What is your favorite color?',
authorId: 'fsdfsd-sdfsdfsd-sdfsdfsd-sdfsdf',
createdAt: new Date(),
updatedAt: new Date(),
},
// Add more questions as needed
];

questionRepository.findMany = jest.fn().mockResolvedValue(questions);

const result = await getManyQuestionsUseCase.execute();

expect(result).toEqual(questions);
expect(questionRepository.findMany).toHaveBeenCalled();
});
});
58 changes: 58 additions & 0 deletions src/modules/question/use-case/get-question-by-id.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Test, TestingModule } from '@nestjs/testing';
import { QuestionRepository } from '@app/common';
import { Question } from '@prisma/client';
import { GetQuestionByIdUseCase } from './get-question-by-id';

class QuestionRepositoryMock {
findById = jest.fn();
}

describe('GetQuestionByIdUseCase', () => {
let getQuestionByIdUseCase: GetQuestionByIdUseCase;
let questionRepository: QuestionRepository;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
GetQuestionByIdUseCase,
{
provide: QuestionRepository,
useClass: QuestionRepositoryMock,
},
],
}).compile();

getQuestionByIdUseCase = module.get<GetQuestionByIdUseCase>(GetQuestionByIdUseCase);
questionRepository = module.get<QuestionRepository>(QuestionRepository);
});

it('should be defined', () => {
expect(getQuestionByIdUseCase).toBeDefined();
});

it('should get a question by id', async () => {
const question: Question = {
id: 'aaaaa-sdfsdfsd-sdfsdfsd-bbbbb',
content: 'What is your favorite color?',
authorId: 'fsdfsd-sdfsdfsd-sdfsdfsd-sdfsdf',
createdAt: new Date(),
updatedAt: new Date(),
};

questionRepository.findById = jest.fn().mockResolvedValue(question);

const result = await getQuestionByIdUseCase.execute(question.id);

expect(result).toEqual(question);
expect(questionRepository.findById).toHaveBeenCalledWith(question.id);
});

it('should throw a not found exception when the question is not found', async () => {
const questionId = 'aaaaa-sdfsdfsd-sdfsdfsd-bbbbb';
questionRepository.findById = jest.fn().mockResolvedValue(null);

await expect(getQuestionByIdUseCase.execute(questionId)).rejects.toThrow('Not Found');

expect(questionRepository.findById).toHaveBeenCalledWith(questionId);
});
});

0 comments on commit 1dd6045

Please sign in to comment.