@@ -3,6 +3,7 @@ import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3
3
import {
4
4
delay ,
5
5
exhaustMap ,
6
+ map ,
6
7
mergeMap ,
7
8
Observable ,
8
9
of ,
@@ -25,6 +26,8 @@ import {
25
26
withEntities ,
26
27
} from '@ngrx/signals/entities' ;
27
28
import {
29
+ Dispatcher ,
30
+ event ,
28
31
eventGroup ,
29
32
Events ,
30
33
injectDispatch ,
@@ -230,6 +233,53 @@ describe('Integration Tests', () => {
230
233
} ) ;
231
234
expect ( console . error ) . toHaveBeenCalledWith ( 'Error!' ) ;
232
235
} ) ) ;
236
+
237
+ it ( 'handles events in the order they are dispatched' , ( ) => {
238
+ const handledEventsLog : string [ ] = [ ] ;
239
+ const first = event ( 'first' ) ;
240
+ const second = event ( 'second' ) ;
241
+ const save = event ( 'save' , type < string > ( ) ) ;
242
+ const Store = signalStore (
243
+ { providedIn : 'root' } ,
244
+ withState ( { savedEvents : [ ] as string [ ] } ) ,
245
+ withEffects ( ( _ , events = inject ( Events ) ) => ( {
246
+ emitSecond$ : events . on ( first ) . pipe (
247
+ tap ( ( { type } ) =>
248
+ handledEventsLog . push ( `emitSecond$ effect: ${ type } ` )
249
+ ) ,
250
+ map ( ( ) => second ( ) )
251
+ ) ,
252
+ save$ : events . on ( first , second ) . pipe (
253
+ tap ( ( { type } ) => handledEventsLog . push ( `save$ effect: ${ type } ` ) ) ,
254
+ map ( ( { type } ) => save ( type ) )
255
+ ) ,
256
+ } ) ) ,
257
+ withReducer (
258
+ on ( first , second , save , ( { type, payload } ) => {
259
+ handledEventsLog . push ( `reducer: ${ type } ${ payload ?? '' } ` ) ;
260
+ return { } ;
261
+ } ) ,
262
+ on ( save , ( { payload } , state ) => ( {
263
+ savedEvents : [ ...state . savedEvents , payload ] ,
264
+ } ) )
265
+ )
266
+ ) ;
267
+
268
+ const dispatcher = TestBed . inject ( Dispatcher ) ;
269
+ const store = TestBed . inject ( Store ) ;
270
+
271
+ dispatcher . dispatch ( first ( ) ) ;
272
+ expect ( store . savedEvents ( ) ) . toEqual ( [ 'first' , 'second' ] ) ;
273
+ expect ( handledEventsLog ) . toEqual ( [
274
+ 'reducer: first' ,
275
+ 'emitSecond$ effect: first' ,
276
+ 'reducer: second' ,
277
+ 'save$ effect: first' ,
278
+ 'reducer: savefirst' ,
279
+ 'save$ effect: second' ,
280
+ 'reducer: savesecond' ,
281
+ ] ) ;
282
+ } ) ;
233
283
} ) ;
234
284
235
285
describe ( 'custom withReducer and withEffects' , ( ) => {
0 commit comments