Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevewithington/8f04b7abaa285a1a2e8c831cb0049a44 to your computer and use it in GitHub Desktop.
Save stevewithington/8f04b7abaa285a1a2e8c831cb0049a44 to your computer and use it in GitHub Desktop.
Lazy loading testing
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