forked from ngxs/store
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(storage-plugin): only restore state if key matches addedStates (n…
…gxs#1746) * fix(storage-plugin): only restore state if key matches addedStates * test(storage-plugin): add test that ensures that state is not restored again * chore: update CHANGELOG.md Co-authored-by: arturovt <[email protected]>
- Loading branch information
1 parent
bbf2968
commit 2f9542d
Showing
6 changed files
with
136 additions
and
7 deletions.
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
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
119 changes: 119 additions & 0 deletions
119
packages/storage-plugin/tests/issues/issue-restore-state-only-if-key-matches.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,119 @@ | ||
import { APP_BASE_HREF } from '@angular/common'; | ||
import { Router, RouterModule } from '@angular/router'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { Component, NgModule, NgZone } from '@angular/core'; | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
import { freshPlatform } from '@ngxs/store/internals/testing'; | ||
import { State, NgxsModule, Store, StateToken } from '@ngxs/store'; | ||
|
||
import { NgxsStoragePluginModule } from '../../'; | ||
|
||
describe('Restore state only if key matches', () => { | ||
beforeEach(() => { | ||
// Caretaker note: it somehow sets `/@angular-cli-builders` as a default URL, thus when running `initialNavigation()` | ||
// it errors that there's no route definition for the `/@angular-cli-builders`. | ||
spyOn(Router.prototype, 'initialNavigation').and.returnValue(undefined); | ||
}); | ||
|
||
afterEach(() => { | ||
localStorage.clear(); | ||
}); | ||
|
||
it( | ||
'should not deserialize the state twice if the state should not be handled by the storage plugin', | ||
freshPlatform(async () => { | ||
// Arrange | ||
interface AuthStateModel { | ||
token: string; | ||
} | ||
|
||
const AUTH_STATE_TOKEN = new StateToken<AuthStateModel | null>('auth'); | ||
|
||
localStorage.setItem( | ||
AUTH_STATE_TOKEN.getName(), | ||
JSON.stringify({ | ||
token: 'initialTokenFromLocalStorage' | ||
}) | ||
); | ||
|
||
@State<AuthStateModel | null>({ | ||
name: AUTH_STATE_TOKEN, | ||
defaults: null | ||
}) | ||
class AuthState {} | ||
|
||
@State({ | ||
name: 'user', | ||
defaults: {} | ||
}) | ||
class UserState {} | ||
|
||
@Component({ template: '' }) | ||
class UserComponent {} | ||
|
||
@NgModule({ | ||
imports: [ | ||
RouterModule.forChild([ | ||
{ | ||
path: '', | ||
component: UserComponent | ||
} | ||
]), | ||
NgxsModule.forFeature([UserState]) | ||
], | ||
declarations: [UserComponent] | ||
}) | ||
class UserModule {} | ||
|
||
@Component({ selector: 'app-root', template: '<router-outlet></router-outlet>' }) | ||
class TestComponent {} | ||
|
||
@NgModule({ | ||
imports: [ | ||
BrowserModule, | ||
RouterModule.forRoot([ | ||
{ | ||
path: 'user', | ||
loadChildren: () => UserModule | ||
} | ||
]), | ||
NgxsModule.forRoot([AuthState]), | ||
NgxsStoragePluginModule.forRoot({ | ||
key: [AuthState] | ||
}) | ||
], | ||
declarations: [TestComponent], | ||
bootstrap: [TestComponent], | ||
providers: [{ provide: APP_BASE_HREF, useValue: '/' }] | ||
}) | ||
class TestModule {} | ||
|
||
// Act | ||
const { injector } = await platformBrowserDynamic().bootstrapModule(TestModule); | ||
const ngZone = injector.get(NgZone); | ||
const router = injector.get(Router); | ||
const store = injector.get(Store); | ||
const subscribeSpy = jest.fn(); | ||
const subscription = store.select(AuthState).subscribe(subscribeSpy); | ||
|
||
// Well, we're explicitly setting another value for the auth state before going to the `/user` route. | ||
// Previously, it would've retrieved the auth state value from the local storage each time the `UpdateState` | ||
// action is dispatched. See `storage.plugin.ts` and `!event.addedStates.hasOwnProperty(key)` expression which | ||
// runs `continue` if the state, which has been added, shouldn't be handled by the storage plugin. | ||
localStorage.setItem( | ||
AUTH_STATE_TOKEN.getName(), | ||
JSON.stringify({ token: 'manuallySetTokenToEnsureItIsNotRetrievedAgain' }) | ||
); | ||
|
||
await ngZone.run(() => router.navigateByUrl('/user')); | ||
|
||
// Assert | ||
expect(subscribeSpy).toHaveBeenCalledTimes(1); | ||
expect(subscribeSpy).toHaveBeenCalledWith({ | ||
token: 'initialTokenFromLocalStorage' | ||
}); | ||
|
||
subscription.unsubscribe(); | ||
}) | ||
); | ||
}); |