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' ;
6
+ import { DomainTransaction } from '../../../../../src/shared/domain/DomainTransaction.js' ;
3
7
import { UserNotFoundError } from '../../../../../src/shared/domain/errors.js' ;
4
8
import { NotFoundError } from '../../../../../src/shared/domain/errors.js' ;
5
9
import { catchErr , expect , sinon } from '../../../../test-helper.js' ;
6
10
7
11
describe ( 'Unit | Devcomp | Domain | UseCases | create-passage' , function ( ) {
12
+ beforeEach ( function ( ) {
13
+ sinon . stub ( DomainTransaction , 'execute' ) . callsFake ( ( lambda ) => lambda ( ) ) ;
14
+ } ) ;
15
+
8
16
describe ( 'when module does not exist' , function ( ) {
9
17
it ( 'should throw a ModuleDoesNotExist error' , async function ( ) {
10
18
// given
@@ -48,11 +56,35 @@ describe('Unit | Devcomp | Domain | UseCases | create-passage', function () {
48
56
} ) ;
49
57
} ) ;
50
58
51
- it ( 'should call passage repository to save the passage ' , async function ( ) {
59
+ it ( 'should save the passage and record passage started event ' , async function ( ) {
52
60
// given
53
61
const moduleId = Symbol ( 'moduleId' ) ;
62
+ const passageId = Symbol ( 'passageId' ) ;
54
63
const userId = Symbol ( 'userId' ) ;
55
- const repositoryResult = Symbol ( 'repository-result' ) ;
64
+
65
+ const slug = 'les-adresses-email' ;
66
+ const title = 'Les adresses email' ;
67
+ const isBeta = false ;
68
+ const grains = [ Symbol ( 'text' ) ] ;
69
+ const transitionTexts = [ ] ;
70
+ const details = Symbol ( 'details' ) ;
71
+ const version = Symbol ( 'version' ) ;
72
+ const module = new Module ( { id : moduleId , slug, title, isBeta, grains, details, transitionTexts, version } ) ;
73
+
74
+ const occurredAt = new Date ( '2025-01-01' ) ;
75
+ const passageCreatedAt = new Date ( '2025-03-05' ) ;
76
+ const passage = new Passage ( {
77
+ id : passageId ,
78
+ moduleId,
79
+ userId,
80
+ createdAt : passageCreatedAt ,
81
+ } ) ;
82
+
83
+ const passageStartedEvent = new PassageStartedEvent ( {
84
+ contentHash : version ,
85
+ occurredAt,
86
+ passageId,
87
+ } ) ;
56
88
57
89
const userRepositoryStub = {
58
90
get : sinon . stub ( ) ,
@@ -61,17 +93,23 @@ describe('Unit | Devcomp | Domain | UseCases | create-passage', function () {
61
93
const moduleRepositoryStub = {
62
94
getBySlug : sinon . stub ( ) ,
63
95
} ;
64
- moduleRepositoryStub . getBySlug . withArgs ( { slug : moduleId } ) . resolves ( ) ;
96
+ moduleRepositoryStub . getBySlug . withArgs ( { slug : moduleId } ) . resolves ( module ) ;
65
97
const passageRepositoryStub = {
66
98
save : sinon . stub ( ) ,
67
99
} ;
68
- passageRepositoryStub . save . resolves ( repositoryResult ) ;
100
+ passageRepositoryStub . save . resolves ( passage ) ;
101
+
102
+ const passageEventRepositoryStub = {
103
+ record : sinon . stub ( ) ,
104
+ } ;
69
105
70
106
// when
71
107
const result = await createPassage ( {
108
+ occurredAt,
72
109
moduleId,
73
110
userId,
74
111
passageRepository : passageRepositoryStub ,
112
+ passageEventRepository : passageEventRepositoryStub ,
75
113
moduleRepository : moduleRepositoryStub ,
76
114
userRepository : userRepositoryStub ,
77
115
} ) ;
@@ -81,6 +119,7 @@ describe('Unit | Devcomp | Domain | UseCases | create-passage', function () {
81
119
moduleId,
82
120
userId,
83
121
} ) ;
84
- expect ( result ) . to . equal ( repositoryResult ) ;
122
+ expect ( passageEventRepositoryStub . record ) . to . have . been . calledOnceWith ( passageStartedEvent ) ;
123
+ expect ( result ) . to . equal ( passage ) ;
85
124
} ) ;
86
125
} ) ;
0 commit comments