-
-
Save stevewithington/8f04b7abaa285a1a2e8c831cb0049a44 to your computer and use it in GitHub Desktop.
Lazy loading testing
This file contains hidden or 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
import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; | |
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | |
import { Router, RouterModule } from '@angular/router'; | |
import { NgModuleFactoryLoader, Component, NgModule } from '@angular/core'; | |
import { RouterTestingModule } from '@angular/router/testing'; | |
import { routes } from './app-routing.module' | |
import { Location } from '@angular/common'; | |
describe('PageNotfoundComponent', () => { | |
let component: AppComponent; | |
let fixture: ComponentFixture<AppComponent>; | |
let router: Router; | |
let location: Location; | |
beforeEach(() => { | |
fixture = TestBed.createComponent(AppComponent); | |
component = fixture.componentInstance; | |
fixture.detectChanges(); | |
router = TestBed.get(Router); | |
}); | |
beforeEach(async(() => { | |
TestBed.configureTestingModule({ | |
declarations: [ AppComponent ], | |
imports: [ | |
RouterTestingModule.withRoutes(routes), | |
BrowserAnimationsModule, | |
PageNotfoundModule, | |
LoginModule | |
] | |
}).compileComponents(); | |
})); | |
@Component({ | |
template: '' | |
}) | |
class LazyLoadedComponent { } | |
@NgModule({ | |
declarations: [LazyLoadedComponent] | |
}) | |
class LazyModule { } | |
it('should navigate to 404 child path', fakeAsync(() => { | |
router = TestBed.get(Router); | |
router.initialNavigation(); | |
// Used to load ng module factories. | |
const loader = TestBed.get(NgModuleFactoryLoader); | |
location = TestBed.get(Location); | |
// sets up stubbedModules | |
loader.stubbedModules = { | |
'./page-notfound/page-notfound.module#PageNotfoundModule': LazyModule, | |
}; | |
router.resetConfig([ | |
{ path: '404', loadChildren: './page-notfound/page-notfound.module#PageNotfoundModule' }, | |
]); | |
router.navigateByUrl('/404'); | |
tick(); | |
fixture.detectChanges(); | |
expect(location.path()).toBe('/404'); | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment