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(router-plugin): cleanup subscriptions when the root view is destr…
…oyed (ngxs#1754)
- Loading branch information
Showing
4 changed files
with
79 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { APP_BASE_HREF } from '@angular/common'; | ||
import { Component, NgModule } from '@angular/core'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { Router, RouterEvent, RouterModule } from '@angular/router'; | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
|
||
import { Subject } from 'rxjs'; | ||
import { NgxsModule } from '@ngxs/store'; | ||
import { freshPlatform } from '@ngxs/store/internals/testing'; | ||
|
||
import { NgxsRouterPluginModule, RouterState } from '../'; | ||
|
||
describe('RouterState cleanup', () => { | ||
@Component({ | ||
selector: 'app-root', | ||
template: '' | ||
}) | ||
class TestComponent {} | ||
|
||
@NgModule({ | ||
imports: [ | ||
BrowserModule, | ||
RouterModule.forRoot([], { initialNavigation: false }), | ||
NgxsModule.forRoot([]), | ||
NgxsRouterPluginModule.forRoot() | ||
], | ||
declarations: [TestComponent], | ||
providers: [{ provide: APP_BASE_HREF, useValue: '/' }], | ||
bootstrap: [TestComponent] | ||
}) | ||
class TestModule {} | ||
|
||
it( | ||
'should cleanup subscriptions when the root view is destroyed', | ||
freshPlatform(async () => { | ||
// Arrange & act | ||
const ngModuleRef = await platformBrowserDynamic().bootstrapModule(TestModule); | ||
|
||
const router = ngModuleRef.injector.get(Router); | ||
const events = router.events as Subject<RouterEvent>; | ||
const routerState = ngModuleRef.injector.get(RouterState); | ||
const spy = jest.spyOn(routerState, 'ngOnDestroy'); | ||
|
||
try { | ||
// Assert | ||
expect(events.observers.length).toBeGreaterThan(0); | ||
ngModuleRef.destroy(); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(events.observers.length).toEqual(0); | ||
} finally { | ||
spy.mockRestore(); | ||
} | ||
}) | ||
); | ||
}); |