Skip to content

Commit 2543df4

Browse files
ivanportilloaliondevrsaladocid
committed
Refactor CourseCreator test to use command handler
Co-authored-by: aliondev <antonioleon@audiense.com> Co-authored-by: Rubén Salado <rsaladocid@users.noreply.github.com>
1 parent 784ab21 commit 2543df4

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { CourseCreator } from '../../../../../src/Contexts/Mooc/Courses/application/CourseCreator';
22
import { CourseMother } from '../domain/CourseMother';
33
import { CourseRepositoryMock } from '../__mocks__/CourseRepositoryMock';
4-
import { CreateCourseRequestMother } from './CreateCourseRequestMother';
4+
import { CreateCourseCommandMother } from './CreateCourseCommandMother';
55
import EventBusMock from '../__mocks__/EventBusMock';
6+
import { CreateCourseCommandHandler } from '../../../../../src/Contexts/Mooc/Courses/application/CreateCourseCommandHandler';
67

78
let repository: CourseRepositoryMock;
8-
let creator: CourseCreator;
9+
let handler: CreateCourseCommandHandler;
910

1011
const eventBus = new EventBusMock();
1112

1213
beforeEach(() => {
1314
repository = new CourseRepositoryMock();
14-
creator = new CourseCreator(repository, eventBus);
15+
const creator = new CourseCreator(repository, eventBus);
16+
handler = new CreateCourseCommandHandler(creator);
1517
});
1618

1719
it('should create a valid course', async () => {
18-
const request = CreateCourseRequestMother.random();
19-
20-
const course = CourseMother.fromRequest(request);
21-
22-
await creator.run(request);
20+
const command = CreateCourseCommandMother.random();
21+
await handler.handle(command);
2322

23+
const course = CourseMother.fromCommand(command);
2424
repository.assertLastSavedCourseIs(course);
2525
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CourseDurationMother } from '../domain/CourseDurationMother';
2+
import { CourseIdMother } from '../../Shared/domain/Courses/CourseIdMother';
3+
import { CourseNameMother } from '../domain/CourseNameMother';
4+
import { CreateCourseCommand } from '../../../../../src/Contexts/Mooc/Courses/application/CreateCourseCommand';
5+
6+
export class CreateCourseCommandMother {
7+
static create(id: string, name: string, duration: string): CreateCourseCommand {
8+
return new CreateCourseCommand({ id, name, duration });
9+
}
10+
11+
static random(): CreateCourseCommand {
12+
return this.create(CourseIdMother.random().value, CourseNameMother.random().value, CourseDurationMother.random().value);
13+
}
14+
}

tests/Contexts/Mooc/Courses/application/CreateCourseRequestMother.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/Contexts/Mooc/Courses/domain/Course.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CreateCourseRequestMother } from '../application/CreateCourseRequestMother';
1+
import { CreateCourseCommandMother } from '../application/CreateCourseCommandMother';
22
import { CourseMother } from './CourseMother';
33
import { Course } from '../../../../../src/Contexts/Mooc/Courses/domain/Course';
44
import { CourseIdMother } from '../../Shared/domain/Courses/CourseIdMother';
@@ -8,13 +8,13 @@ import { CourseDurationMother } from './CourseDurationMother';
88
describe('Course', () => {
99

1010
it('should return a new course instance', () => {
11-
const request = CreateCourseRequestMother.random();
11+
const command = CreateCourseCommandMother.random();
1212

13-
const course = CourseMother.fromRequest(request);
13+
const course = CourseMother.fromCommand(command);
1414

15-
expect(course.id.value).toBe(request.id);
16-
expect(course.name.value).toBe(request.name);
17-
expect(course.duration.value).toBe(request.duration);
15+
expect(course.id.value).toBe(command.id);
16+
expect(course.name.value).toBe(command.name);
17+
expect(course.duration.value).toBe(command.duration);
1818
});
1919

2020
it('should record a CourseCreatedDomainEvent after its creation', () => {

tests/Contexts/Mooc/Courses/domain/CourseMother.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import { CreateCourseRequest } from '../../../../../src/Contexts/Mooc/Courses/ap
66
import { CourseIdMother } from '../../Shared/domain/Courses/CourseIdMother';
77
import { CourseNameMother } from './CourseNameMother';
88
import { CourseDurationMother } from './CourseDurationMother';
9+
import { CreateCourseCommand } from '../../../../../src/Contexts/Mooc/Courses/application/CreateCourseCommand';
910

1011
export class CourseMother {
1112
static create(id: CourseId, name: CourseName, duration: CourseDuration): Course {
1213
return new Course(id, name, duration);
1314
}
1415

15-
static fromRequest(request: CreateCourseRequest): Course {
16+
static fromCommand(command: CreateCourseCommand): Course {
1617
return this.create(
17-
CourseIdMother.create(request.id),
18-
CourseNameMother.create(request.name),
19-
CourseDurationMother.create(request.duration)
18+
CourseIdMother.create(command.id),
19+
CourseNameMother.create(command.name),
20+
CourseDurationMother.create(command.duration)
2021
);
2122
}
2223

0 commit comments

Comments
 (0)