-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(store): show error when state initialization order is invalid (#2066
) This commit adds a `console.error` when the `UpdateState` is dispatched before the `InitState`. This typically indicates that the state initialization order is invalid and must be updated to ensure that states are initialized in the correct order. The incorrect order may prevent `ngxsOnInit` from being called on feature states or lead to other unpredictable errors.
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/store/tests/issues/state-initialization-order.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { NgxsModule, State, Store } from '@ngxs/store'; | ||
|
||
describe('State stream order of updates', () => { | ||
@Injectable() | ||
@State({ name: 'counter', defaults: 0 }) | ||
class CounterState {} | ||
|
||
it('should log an error into the console when the state initialization order is invalid', () => { | ||
// Arrange | ||
const errorSpy = jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [NgxsModule.forFeature([CounterState]), NgxsModule.forRoot()] | ||
}); | ||
|
||
// Act | ||
TestBed.inject(Store); | ||
|
||
try { | ||
// Assert | ||
expect(errorSpy).toHaveBeenCalledWith( | ||
expect.stringMatching(/invalid state initialization order/) | ||
); | ||
} finally { | ||
errorSpy.mockRestore(); | ||
} | ||
}); | ||
}); |