File tree Expand file tree Collapse file tree 2 files changed +35
-9
lines changed
tests/Contexts/Mooc/Courses Expand file tree Collapse file tree 2 files changed +35
-9
lines changed Original file line number Diff line number Diff line change
1
+ import { CourseRepository } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseRepository' ;
2
+ import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course' ;
3
+ import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId' ;
4
+ import { Nullable } from '../../../../../src/Contexts/Shared/domain/Nullable' ;
5
+
6
+ export class CourseRepositoryMock implements CourseRepository {
7
+ private mockSave = jest . fn ( ) ;
8
+ private mockSearch = jest . fn ( ) ;
9
+
10
+ async save ( course : Course ) : Promise < void > {
11
+ this . mockSave ( course ) ;
12
+ }
13
+
14
+ assertLastSavedCourseIs ( expected : Course ) : void {
15
+ expect ( this . mockSave ) . toHaveBeenCalledWith ( expected ) ;
16
+ }
17
+
18
+ async search ( id : CourseId ) : Promise < Nullable < Course > > {
19
+ return this . mockSearch ( id ) ;
20
+ }
21
+
22
+ whenSearchThenReturn ( value : Nullable < Course > ) : void {
23
+ this . mockSearch . mockReturnValue ( value ) ;
24
+ }
25
+
26
+ assertLastSearchedCourseIs ( expected : CourseId ) : void {
27
+ expect ( this . mockSearch ) . toHaveBeenCalledWith ( expected ) ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change 1
1
import { CourseCreator } from '../../../../../src/Contexts/Mooc/Courses/application/CourseCreator' ;
2
+ import { EventBus } from '../../../../../src/Contexts/Shared/domain/EventBus' ;
2
3
import { CourseMother } from '../domain/CourseMother' ;
4
+ import { CourseRepositoryMock } from '../__mocks__/CourseRepositoryMock' ;
3
5
import { CreateCourseRequestMother } from './CreateCourseRequestMother' ;
4
- import { CourseRepository } from '../../../../../src/Contexts/Mooc/Courses/domain/CourseRepository' ;
5
- import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course' ;
6
- import { EventBus } from '../../../../../src/Contexts/Shared/domain/EventBus' ;
7
6
8
- let repository : CourseRepository ;
7
+ let repository : CourseRepositoryMock ;
9
8
let creator : CourseCreator ;
10
9
11
- const createRepository = ( ) : CourseRepository => ( { save : jest . fn ( ) , search : jest . fn ( ) } ) ;
12
- const eventBus = ( ) : EventBus => ( { publish : jest . fn ( ) } ) ;
13
- const shouldSave = ( course : Course ) => expect ( repository . save ) . toHaveBeenCalledWith ( course ) ;
10
+ const eventBus = ( ) : EventBus => ( { publish : jest . fn ( ) } ) ;
14
11
15
12
beforeEach ( ( ) => {
16
- repository = createRepository ( ) ;
13
+ repository = new CourseRepositoryMock ( ) ;
17
14
creator = new CourseCreator ( repository , eventBus ( ) ) ;
18
15
} ) ;
19
16
@@ -24,5 +21,5 @@ it('should create a valid course', async () => {
24
21
25
22
await creator . run ( request ) ;
26
23
27
- shouldSave ( course ) ;
24
+ repository . assertLastSavedCourseIs ( course ) ;
28
25
} ) ;
You can’t perform that action at this time.
0 commit comments