1
+ import { logger } from "../fn/logger.js" ;
1
2
import { DITest } from "../services/DITest.js" ;
2
3
import { bindContext , getAsyncStore } from "../utils/asyncHookContext.js" ;
3
4
import { DIContext } from "./DIContext.js" ;
4
5
5
6
describe ( "DIContext" , ( ) => {
6
7
beforeEach ( ( ) => DITest . create ( ) ) ;
8
+ beforeEach ( ( ) => {
9
+ vi . spyOn ( logger ( ) , "info" ) . mockReturnValue ( undefined ) ;
10
+ } ) ;
7
11
afterEach ( ( ) => DITest . reset ( ) ) ;
8
12
describe ( "constructor" , ( ) => {
9
13
it ( "should create a new Context and skip log" , ( ) => {
10
- const logger = {
11
- info : vi . fn ( )
12
- } ;
13
14
const context = new DIContext ( {
14
15
event : {
15
16
response : { } ,
@@ -18,9 +19,7 @@ describe("DIContext", () => {
18
19
}
19
20
} ,
20
21
id : "id" ,
21
- logger,
22
- maxStackSize : 0 ,
23
- injector : DITest . injector
22
+ maxStackSize : 0
24
23
} ) ;
25
24
26
25
expect ( context . id ) . toEqual ( "id" ) ;
@@ -31,23 +30,17 @@ describe("DIContext", () => {
31
30
32
31
context . logger . info ( "test" ) ;
33
32
34
- expect ( logger . info ) . toHaveBeenCalled ( ) ;
33
+ expect ( logger ( ) . info ) . toHaveBeenCalled ( ) ;
35
34
36
35
context . destroy ( ) ;
37
36
} ) ;
38
37
it ( "should create a new Context and log event" , ( ) => {
39
- const logger = {
40
- info : vi . fn ( )
41
- } ;
42
-
43
38
const context = new DIContext ( {
44
39
id : "id" ,
45
40
event : {
46
41
response : { } ,
47
42
request : { url : "/" }
48
43
} ,
49
- logger,
50
- injector : DITest . injector ,
51
44
maxStackSize : 0 ,
52
45
platform : "OTHER"
53
46
} ) ;
@@ -63,7 +56,7 @@ describe("DIContext", () => {
63
56
context . next ( ) ;
64
57
context . logger . info ( "test" ) ;
65
58
66
- expect ( logger . info ) . toHaveBeenCalled ( ) ;
59
+ expect ( logger ( ) . info ) . toHaveBeenCalled ( ) ;
67
60
} ) ;
68
61
} ) ;
69
62
@@ -75,11 +68,7 @@ describe("DIContext", () => {
75
68
request : { url : "/admin" }
76
69
} ,
77
70
id : "id" ,
78
- logger : {
79
- info : vi . fn ( )
80
- } ,
81
71
maxStackSize : 0 ,
82
- injector : { emit : vi . fn ( ) } as any ,
83
72
ignoreUrlPatterns : [ "/admin" , / \/ a d m i n 2 / ]
84
73
} ) ;
85
74
const resolver = vi . fn ( ) . mockReturnValue ( "test" ) ;
@@ -100,11 +89,7 @@ describe("DIContext", () => {
100
89
request : { url : "/admin" }
101
90
} ,
102
91
id : "id" ,
103
- logger : {
104
- info : vi . fn ( )
105
- } ,
106
92
maxStackSize : 0 ,
107
- injector : { emit : vi . fn ( ) } as any ,
108
93
ignoreUrlPatterns : [ "/admin" , / \/ a d m i n 2 / ]
109
94
} ) ;
110
95
const resolver = vi . fn ( ) . mockResolvedValue ( "test" ) ;
@@ -116,108 +101,4 @@ describe("DIContext", () => {
116
101
expect ( resolver ) . toHaveBeenCalledTimes ( 1 ) ;
117
102
} ) ;
118
103
} ) ;
119
- describe ( "emit()" , ( ) => {
120
- it ( "should emit event" , async ( ) => {
121
- const context = new DIContext ( {
122
- event : {
123
- response : { } ,
124
- request : { url : "/admin" }
125
- } ,
126
- id : "id" ,
127
- logger : {
128
- info : vi . fn ( )
129
- } ,
130
- maxStackSize : 0 ,
131
- injector : { emit : vi . fn ( ) } as any ,
132
- ignoreUrlPatterns : [ "/admin" , / \/ a d m i n 2 / ]
133
- } ) ;
134
-
135
- await context . emit ( "event" , "test" ) ;
136
-
137
- expect ( context . injector . emit ) . toHaveBeenCalledWith ( "event" , "test" ) ;
138
- } ) ;
139
- } ) ;
140
- describe ( "runInContext()" , ( ) => {
141
- it ( "should run handler in a context" , async ( ) => {
142
- const context = new DIContext ( {
143
- event : {
144
- response : { } ,
145
- request : { url : "/admin" }
146
- } ,
147
- id : "id" ,
148
- logger : {
149
- info : vi . fn ( )
150
- } ,
151
- maxStackSize : 0 ,
152
- injector : {
153
- alterAsync : vi . fn ( ) . mockImplementation ( ( event , fn , $ctx ) => {
154
- return fn ;
155
- } )
156
- } as any ,
157
- ignoreUrlPatterns : [ "/admin" , / \/ a d m i n 2 / ]
158
- } ) ;
159
-
160
- const stub = vi . fn ( ) ;
161
-
162
- await context . runInContext ( stub ) ;
163
-
164
- expect ( stub ) . toHaveBeenCalledWith ( ) ;
165
- expect ( context . injector . alterAsync ) . toHaveBeenCalledWith ( "$alterRunInContext" , stub ) ;
166
- } ) ;
167
- it ( "should run handler in a context + bind" , async ( ) => {
168
- const context = new DIContext ( {
169
- event : {
170
- response : { } ,
171
- request : { url : "/admin" }
172
- } ,
173
- id : "id" ,
174
- logger : {
175
- info : vi . fn ( )
176
- } ,
177
- maxStackSize : 0 ,
178
- injector : {
179
- alterAsync : vi . fn ( ) . mockImplementation ( ( event , fn , $ctx ) => {
180
- return fn ;
181
- } )
182
- } as any ,
183
- ignoreUrlPatterns : [ "/admin" , / \/ a d m i n 2 / ]
184
- } ) ;
185
-
186
- const stub = vi . fn ( ) ;
187
-
188
- await context . runInContext ( ( ) => {
189
- bindContext ( stub ) ( ) ;
190
- expect ( getAsyncStore ( ) . getStore ( ) ) . toEqual ( { current : context } ) ;
191
- } ) ;
192
-
193
- expect ( stub ) . toHaveBeenCalledWith ( ) ;
194
- expect ( context . injector . alterAsync ) . toHaveBeenCalledWith ( "$alterRunInContext" , expect . any ( Function ) ) ;
195
- } ) ;
196
- it ( "should run handler in a context and fallback to next" , async ( ) => {
197
- const context = new DIContext ( {
198
- event : {
199
- response : { } ,
200
- request : { url : "/admin" }
201
- } ,
202
- id : "id" ,
203
- logger : {
204
- info : vi . fn ( )
205
- } ,
206
- maxStackSize : 0 ,
207
- injector : {
208
- alterAsync : vi . fn ( ) . mockImplementation ( ( event , fn , $ctx ) => {
209
- return null ;
210
- } )
211
- } as any ,
212
- ignoreUrlPatterns : [ "/admin" , / \/ a d m i n 2 / ]
213
- } ) ;
214
-
215
- const stub = vi . fn ( ) ;
216
-
217
- await context . runInContext ( stub ) ;
218
-
219
- expect ( stub ) . toHaveBeenCalledWith ( ) ;
220
- expect ( context . injector . alterAsync ) . toHaveBeenCalledWith ( "$alterRunInContext" , stub ) ;
221
- } ) ;
222
- } ) ;
223
104
} ) ;
0 commit comments