Skip to content

Commit ee8ea8f

Browse files
committed
Add increment courses counter unit tests
1 parent af56cb0 commit ee8ea8f

File tree

7 files changed

+92
-12
lines changed

7 files changed

+92
-12
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"name": "typescript-ddd-skeleton",
33
"version": "1.0.0",
44
"description": "",
5+
"repository": {
6+
"url": "https://github.com/CodelyTV/typescript-ddd-skeleton"
7+
},
8+
"license": "",
59
"engines": {
610
"node": ">=10.15.0",
711
"npm": ">=6.7.0"

tests/Contexts/Mooc/CoursesCounter/__mocks__/CoursesCounterRepositoryMock.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CoursesCounterRepository } from '../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounterRepository';
22
import { CoursesCounter } from '../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounter';
33
import { Nullable } from '../../../../../src/Contexts/Shared/domain/Nullable';
4+
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
45

56
export class CoursesCounterRepositoryMock implements CoursesCounterRepository {
67
private mockSave = jest.fn();
@@ -23,4 +24,18 @@ export class CoursesCounterRepositoryMock implements CoursesCounterRepository {
2324
assertSearch() {
2425
expect(this.mockSearch).toHaveBeenCalled();
2526
}
27+
28+
assertNotSave() {
29+
expect(this.mockSave).toHaveBeenCalledTimes(0);
30+
}
31+
32+
assertLastCoursesCounterSaved(counter: CoursesCounter) {
33+
const mock = this.mockSave.mock;
34+
const lastCoursesCounter = mock.calls[mock.calls.length - 1][0] as CoursesCounter;
35+
const { id: id1, ...counterPrimitives } = counter.toPrimitives();
36+
const { id: id2, ...lastSavedPrimitives } = lastCoursesCounter.toPrimitives();
37+
38+
expect(lastCoursesCounter).toBeInstanceOf(CoursesCounter);
39+
expect(lastSavedPrimitives).toEqual(counterPrimitives);
40+
}
2641
}

tests/Contexts/Mooc/CoursesCounter/application/CoursesCounterFinder.test.ts renamed to tests/Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { CoursesCounterFinder } from '../../../../../src/Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder';
2-
import { CoursesCounterMother } from '../domain/CoursesCounterMother';
3-
import { CoursesCounterRepositoryMock } from '../__mocks__/CoursesCounterRepositoryMock';
4-
import { CoursesCounterResponseMother } from '../domain/CoursesCounterResponseMother';
5-
import { CoursesCounterNotExist } from '../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounterNotExist';
1+
import { CoursesCounterFinder } from '../../../../../../src/Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder';
2+
import { CoursesCounterMother } from '../../domain/CoursesCounterMother';
3+
import { CoursesCounterRepositoryMock } from '../../__mocks__/CoursesCounterRepositoryMock';
4+
import { CoursesCounterResponseMother } from '../../domain/CoursesCounterResponseMother';
5+
import { CoursesCounterNotExist } from '../../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounterNotExist';
66

77
describe('CoursesCounter Finder', () => {
88
let finder: CoursesCounterFinder;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { CoursesCounterMother } from '../../domain/CoursesCounterMother';
2+
import { CoursesCounterRepositoryMock } from '../../__mocks__/CoursesCounterRepositoryMock';
3+
import { CoursesCounterIncrementer } from '../../../../../../src/Contexts/Mooc/CoursesCounter/application/Increment/CoursesCounterIncrementer';
4+
import { EventBus } from '../../../../../../src/Contexts/Shared/domain/EventBus';
5+
import EventBusDouble from '../../../Courses/doubles/EventBusDouble';
6+
import { CourseIdMother } from '../../../Shared/domain/Courses/CourseIdMother';
7+
import { CoursesCounter } from '../../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounter';
8+
9+
describe('CoursesCounter Incrementer', () => {
10+
let incrementer: CoursesCounterIncrementer;
11+
let eventBus: EventBus;
12+
let repository: CoursesCounterRepositoryMock;
13+
14+
beforeEach(() => {
15+
eventBus = new EventBusDouble();
16+
repository = new CoursesCounterRepositoryMock();
17+
incrementer = new CoursesCounterIncrementer(repository, eventBus);
18+
});
19+
20+
it('should initialize a new counter', async () => {
21+
const courseId = CourseIdMother.random();
22+
const counter = CoursesCounterMother.withOne(courseId);
23+
24+
await incrementer.run(courseId);
25+
26+
repository.assertLastCoursesCounterSaved(counter);
27+
});
28+
29+
it('should increment an existing counter', async () => {
30+
const existingCounter = CoursesCounterMother.random();
31+
repository.returnOnSearch(existingCounter);
32+
const courseId = CourseIdMother.random();
33+
const expected = CoursesCounter.fromPrimitives(existingCounter.toPrimitives());
34+
expected.increment(courseId);
35+
36+
await incrementer.run(courseId);
37+
38+
repository.assertLastCoursesCounterSaved(expected);
39+
});
40+
41+
it('should not increment an already incremented counter', async () => {
42+
const existingCounter = CoursesCounterMother.random();
43+
repository.returnOnSearch(existingCounter);
44+
const courseId = existingCounter.existingCourses[0];
45+
46+
await incrementer.run(courseId);
47+
48+
repository.assertNotSave();
49+
});
50+
});

tests/Contexts/Mooc/CoursesCounter/domain/CoursesCounterMother.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import { CoursesCounterId } from '../../../../../src/Contexts/Mooc/CoursesCounte
33
import { CoursesCounterTotal } from '../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounterTotal';
44
import { CourseIdMother } from '../../Shared/domain/Courses/CourseIdMother';
55
import { Repeater } from '../../../Shared/domain/Repeater';
6+
import { CoursesCounterTotalMother } from './CoursesCounterTotalMother';
7+
import { CourseId } from '../../../../../src/Contexts/Mooc/Shared/domain/Courses/CourseId';
68

79
export class CoursesCounterMother {
810
static random() {
9-
return new CoursesCounter(
10-
CoursesCounterId.random(),
11-
new CoursesCounterTotal(0),
12-
Repeater.random(CourseIdMother.creator())
13-
);
11+
const total = CoursesCounterTotalMother.random();
12+
return new CoursesCounter(CoursesCounterId.random(), total, Repeater.random(CourseIdMother.creator(), total.value));
13+
}
14+
15+
static withOne(courseId: CourseId) {
16+
return new CoursesCounter(CoursesCounterId.random(), new CoursesCounterTotal(1), [courseId]);
1417
}
1518
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CoursesCounterTotal } from '../../../../../src/Contexts/Mooc/CoursesCounter/domain/CoursesCounterTotal';
2+
import { IntegerMother } from '../../../Shared/domain/IntegerMother';
3+
4+
export class CoursesCounterTotalMother {
5+
static random() {
6+
return new CoursesCounterTotal(IntegerMother.random());
7+
}
8+
}

tests/Contexts/Shared/domain/Repeater.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IntegerMother } from './IntegerMother';
22
export class Repeater {
3-
static random(callable: Function) {
4-
return Array(IntegerMother.random(20))
3+
static random(callable: Function, iterations: number) {
4+
return Array(iterations || IntegerMother.random(20))
55
.fill({})
66
.map(() => callable());
77
}

0 commit comments

Comments
 (0)