-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ddbfef
commit 1dd6045
Showing
2 changed files
with
110 additions
and
0 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,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(); | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); |