Skip to content

Instantly share code, notes, and snippets.

@joeeames
Created September 20, 2016 19:46
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 joeeames/0ddcfe8f0576bc7651ae99e6b59e81ea to your computer and use it in GitHub Desktop.
Save joeeames/0ddcfe8f0576bc7651ae99e6b59e81ea to your computer and use it in GitHub Desktop.
import {Location} from '@angular/common';
import {Component, } from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import {Router} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing';
@Component({selector: 'root-cmp', template: `
<a routerLink="/simple">click me</a>
<router-outlet></router-outlet>
`})
class RootComponent {
}
@Component({selector: 'blank-cmp', template: ``})
class DefaultComponent {
}
@Component({selector: 'simple-cmp', template: `simple`})
class FirstPageComponent {
}
describe('Integration', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes(
[{path: '', component: DefaultComponent}, {path: 'simple', component: FirstPageComponent}]),
],
declarations: [
RootComponent,
DefaultComponent,
FirstPageComponent
]
});
});
// This isn't working
it('should navigate when the link is clicked',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootComponent);
let element = fixture.debugElement.query(By.css('a'))
router.navigateByUrl('/');
advance(fixture);
expect(location.path()).toEqual('/');
element.triggerEventHandler('click', null);
advance(fixture);
expect(location.path()).toEqual('/simple');
})));
});
function advance(fixture: ComponentFixture<any>): void {
tick();
fixture.detectChanges();
}
function createRoot(router: Router, type: any): ComponentFixture<any> {
const f = TestBed.createComponent(type);
advance(f);
router.initialNavigation();
advance(f);
return f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment