1
1
import { ModuleDoesNotExistError } from '../../../../../src/devcomp/domain/errors.js' ;
2
+ import { Module } from '../../../../../src/devcomp/domain/models/module/Module.js' ;
3
+ import { Passage } from '../../../../../src/devcomp/domain/models/Passage.js' ;
4
+ import { PassageStartedEvent } from '../../../../../src/devcomp/domain/models/passage-events/passage-events.js' ;
2
5
import { createPassage } from '../../../../../src/devcomp/domain/usecases/create-passage.js' ;
3
6
import { UserNotFoundError } from '../../../../../src/shared/domain/errors.js' ;
4
7
import { NotFoundError } from '../../../../../src/shared/domain/errors.js' ;
@@ -48,11 +51,38 @@ describe('Unit | Devcomp | Domain | UseCases | create-passage', function () {
48
51
} ) ;
49
52
} ) ;
50
53
51
- it ( 'should call passage repository to save the passage ' , async function ( ) {
54
+ it ( 'should save the passage and record passage started event ' , async function ( ) {
52
55
// given
53
56
const moduleId = Symbol ( 'moduleId' ) ;
57
+ const passageId = Symbol ( 'passageId' ) ;
54
58
const userId = Symbol ( 'userId' ) ;
55
- const repositoryResult = Symbol ( 'repository-result' ) ;
59
+
60
+ const slug = 'les-adresses-email' ;
61
+ const title = 'Les adresses email' ;
62
+ const isBeta = false ;
63
+ const grains = [ Symbol ( 'text' ) ] ;
64
+ const transitionTexts = [ ] ;
65
+ const details = Symbol ( 'details' ) ;
66
+ const hash = 'AZERTY123456' ;
67
+ let module = new Module ( { id : moduleId , slug, title, isBeta, grains, details, transitionTexts } ) ;
68
+ module = {
69
+ ...module ,
70
+ hash : sinon . stub ( ) . returns ( hash ) ,
71
+ } ;
72
+
73
+ const passageCreatedAt = new Date ( '2025-03-05' ) ;
74
+ const passage = new Passage ( {
75
+ id : passageId ,
76
+ moduleId,
77
+ userId,
78
+ createdAt : passageCreatedAt ,
79
+ } ) ;
80
+
81
+ const passageStartedEvent = new PassageStartedEvent ( {
82
+ contentHash : hash ,
83
+ occurredAt : passageCreatedAt ,
84
+ passageId,
85
+ } ) ;
56
86
57
87
const userRepositoryStub = {
58
88
get : sinon . stub ( ) ,
@@ -61,17 +91,22 @@ describe('Unit | Devcomp | Domain | UseCases | create-passage', function () {
61
91
const moduleRepositoryStub = {
62
92
getBySlug : sinon . stub ( ) ,
63
93
} ;
64
- moduleRepositoryStub . getBySlug . withArgs ( { slug : moduleId } ) . resolves ( ) ;
94
+ moduleRepositoryStub . getBySlug . withArgs ( { slug : moduleId } ) . resolves ( module ) ;
65
95
const passageRepositoryStub = {
66
96
save : sinon . stub ( ) ,
67
97
} ;
68
- passageRepositoryStub . save . resolves ( repositoryResult ) ;
98
+ passageRepositoryStub . save . resolves ( passage ) ;
99
+
100
+ const passageEventRepositoryStub = {
101
+ record : sinon . stub ( ) ,
102
+ } ;
69
103
70
104
// when
71
105
const result = await createPassage ( {
72
106
moduleId,
73
107
userId,
74
108
passageRepository : passageRepositoryStub ,
109
+ passageEventRepository : passageEventRepositoryStub ,
75
110
moduleRepository : moduleRepositoryStub ,
76
111
userRepository : userRepositoryStub ,
77
112
} ) ;
@@ -81,6 +116,8 @@ describe('Unit | Devcomp | Domain | UseCases | create-passage', function () {
81
116
moduleId,
82
117
userId,
83
118
} ) ;
84
- expect ( result ) . to . equal ( repositoryResult ) ;
119
+ expect ( module . hash ) . to . have . been . calledOnce ;
120
+ expect ( passageEventRepositoryStub . record ) . to . have . been . calledOnceWith ( passageStartedEvent ) ;
121
+ expect ( result ) . to . equal ( passage ) ;
85
122
} ) ;
86
123
} ) ;
0 commit comments