@@ -268,32 +268,58 @@ describe('signalStore', () => {
268
268
expect ( message ) . toBe ( 'onDestroy' ) ;
269
269
} ) ;
270
270
271
- // FIX: injection context will be provided for `onDestroy` in a separate PR
272
- // see https://github.com/ngrx/platform/pull/4196#issuecomment-1875228588
273
- it ( 'executes hooks in injection context' , ( ) => {
271
+ it ( 'executes hooks factory in injection context' , ( ) => {
274
272
const messages : string [ ] = [ ] ;
275
- const TOKEN = new InjectionToken ( 'TOKEN' , {
273
+ const TOKEN_INIT = new InjectionToken ( 'TOKEN_INIT' , {
274
+ providedIn : 'root' ,
275
+ factory : ( ) => 'init' ,
276
+ } ) ;
277
+ const TOKEN_DESTROY = new InjectionToken ( 'TOKEN_DESTROY' , {
276
278
providedIn : 'root' ,
277
- factory : ( ) => 'ngrx ' ,
279
+ factory : ( ) => 'destroy ' ,
278
280
} ) ;
279
281
const Store = signalStore (
282
+ withState ( { name : 'NgRx Store' } ) ,
283
+ withHooks ( ( store ) => {
284
+ const tokenInit = inject ( TOKEN_INIT ) ;
285
+ const tokenDestroy = inject ( TOKEN_DESTROY ) ;
286
+ return {
287
+ onInit ( ) {
288
+ messages . push ( `${ tokenInit } ${ store . name ( ) } ` ) ;
289
+ } ,
290
+ onDestroy ( ) {
291
+ messages . push ( `${ tokenDestroy } ${ store . name ( ) } ` ) ;
292
+ } ,
293
+ } ;
294
+ } )
295
+ ) ;
296
+ const { destroy } = createLocalService ( Store ) ;
297
+
298
+ expect ( messages ) . toEqual ( [ 'init NgRx Store' ] ) ;
299
+
300
+ destroy ( ) ;
301
+ expect ( messages ) . toEqual ( [ 'init NgRx Store' , 'destroy NgRx Store' ] ) ;
302
+ } ) ;
303
+
304
+ it ( 'executes hooks without injection context' , ( ) => {
305
+ const messages : string [ ] = [ ] ;
306
+ const Store = signalStore (
307
+ withState ( { name : 'NgRx Store' } ) ,
280
308
withHooks ( {
281
- onInit ( ) {
282
- inject ( TOKEN ) ;
283
- messages . push ( 'onInit' ) ;
309
+ onInit ( store ) {
310
+ messages . push ( `init ${ store . name ( ) } ` ) ;
284
311
} ,
285
- onDestroy ( ) {
286
- // inject(TOKEN);
287
- messages . push ( 'onDestroy' ) ;
312
+ onDestroy ( store ) {
313
+ messages . push ( `destroy ${ store . name ( ) } ` ) ;
288
314
} ,
289
315
} )
290
316
) ;
291
317
const { destroy } = createLocalService ( Store ) ;
292
318
293
- expect ( messages ) . toEqual ( [ 'onInit ' ] ) ;
319
+ expect ( messages ) . toEqual ( [ 'init NgRx Store ' ] ) ;
294
320
295
321
destroy ( ) ;
296
- expect ( messages ) . toEqual ( [ 'onInit ' , 'onDestroy ' ] ) ;
322
+ expect ( messages ) . toEqual ( [ 'init NgRx Store ' , 'destroy NgRx Store ' ] ) ;
297
323
} ) ;
298
324
299
325
it ( 'succeeds with onDestroy and providedIn: root' , ( ) => {
0 commit comments