~~~ fix some tests

This commit is contained in:
Jason Choi
2016-08-16 12:03:45 -07:00
parent 23a942ddec
commit 884cf2df33
2 changed files with 250 additions and 244 deletions

View File

@ -41,7 +41,8 @@ export function main() {
platformDynamicServer().bootstrapModule(ExampleModule).then(() => { platformDynamicServer().bootstrapModule(ExampleModule).then(() => {
expect(getDOM().getText(body)).toEqual('Works!'); expect(getDOM().getText(body)).toEqual('Works!');
}); });
})); }),
500);
}); });
} }

View File

@ -11,6 +11,7 @@ import 'rxjs/add/operator/map';
import {CommonModule, Location} from '@angular/common'; import {CommonModule, Location} from '@angular/common';
import {Component, NgModule, NgModuleFactoryLoader} from '@angular/core'; import {Component, NgModule, NgModuleFactoryLoader} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing'; import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {expect} from '@angular/platform-browser/testing/matchers'; import {expect} from '@angular/platform-browser/testing/matchers';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {of } from 'rxjs/observable/of'; import {of } from 'rxjs/observable/of';
@ -632,202 +633,204 @@ describe('Integration', () => {
}))); })));
}); });
describe('router links', () => { if (getDOM().supportsDOMEvents()) { // Browser only
it('should support string router links', fakeAsync(inject([Router], (router: Router) => { describe('router links', () => {
const fixture = createRoot(router, RootCmp); it('should support string router links', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [ children: [
{path: 'link', component: StringLinkCmp}, {path: 'simple', component: SimpleCmp} {path: 'link', component: StringLinkCmp}, {path: 'simple', component: SimpleCmp}
] ]
}]); }]);
router.navigateByUrl('/team/22/link'); router.navigateByUrl('/team/22/link');
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]');
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
expect(native.getAttribute('href')).toEqual('/team/33/simple'); expect(native.getAttribute('href')).toEqual('/team/33/simple');
native.click(); native.click();
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 33 [ simple, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 33 [ simple, right: ]');
}))); })));
it('should not preserve query params and fragment by default', fakeAsync(() => { it('should not preserve query params and fragment by default', fakeAsync(() => {
@Component({ @Component({
selector: 'someRoot', selector: 'someRoot',
template: `<router-outlet></router-outlet><a routerLink="/home">Link</a>` template: `<router-outlet></router-outlet><a routerLink="/home">Link</a>`
}) })
class RootCmpWithLink { class RootCmpWithLink {
} }
TestBed.configureTestingModule({declarations: [RootCmpWithLink]}); TestBed.configureTestingModule({declarations: [RootCmpWithLink]});
const router: Router = TestBed.get(Router); const router: Router = TestBed.get(Router);
const fixture = createRoot(router, RootCmpWithLink); const fixture = createRoot(router, RootCmpWithLink);
router.resetConfig([{path: 'home', component: SimpleCmp}]); router.resetConfig([{path: 'home', component: SimpleCmp}]);
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
router.navigateByUrl('/home?q=123#fragment'); router.navigateByUrl('/home?q=123#fragment');
advance(fixture); advance(fixture);
expect(native.getAttribute('href')).toEqual('/home'); expect(native.getAttribute('href')).toEqual('/home');
})); }));
it('should update hrefs when query params or fragment change', fakeAsync(() => { it('should update hrefs when query params or fragment change', fakeAsync(() => {
@Component({ @Component({
selector: 'someRoot', selector: 'someRoot',
template: template:
`<router-outlet></router-outlet><a routerLink="/home" preserveQueryParams preserveFragment>Link</a>` `<router-outlet></router-outlet><a routerLink="/home" preserveQueryParams preserveFragment>Link</a>`
}) })
class RootCmpWithLink { class RootCmpWithLink {
} }
TestBed.configureTestingModule({declarations: [RootCmpWithLink]}); TestBed.configureTestingModule({declarations: [RootCmpWithLink]});
const router: Router = TestBed.get(Router); const router: Router = TestBed.get(Router);
const fixture = createRoot(router, RootCmpWithLink); const fixture = createRoot(router, RootCmpWithLink);
router.resetConfig([{path: 'home', component: SimpleCmp}]); router.resetConfig([{path: 'home', component: SimpleCmp}]);
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
router.navigateByUrl('/home?q=123'); router.navigateByUrl('/home?q=123');
advance(fixture); advance(fixture);
expect(native.getAttribute('href')).toEqual('/home?q=123'); expect(native.getAttribute('href')).toEqual('/home?q=123');
router.navigateByUrl('/home?q=456'); router.navigateByUrl('/home?q=456');
advance(fixture); advance(fixture);
expect(native.getAttribute('href')).toEqual('/home?q=456'); expect(native.getAttribute('href')).toEqual('/home?q=456');
router.navigateByUrl('/home?q=456#1'); router.navigateByUrl('/home?q=456#1');
advance(fixture); advance(fixture);
expect(native.getAttribute('href')).toEqual('/home?q=456#1'); expect(native.getAttribute('href')).toEqual('/home?q=456#1');
})); }));
it('should support using links on non-a tags', fakeAsync(inject([Router], (router: Router) => { it('should support using links on non-a tags', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp); const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [ children: [
{path: 'link', component: StringLinkButtonCmp}, {path: 'link', component: StringLinkButtonCmp},
{path: 'simple', component: SimpleCmp} {path: 'simple', component: SimpleCmp}
] ]
}]); }]);
router.navigateByUrl('/team/22/link'); router.navigateByUrl('/team/22/link');
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]');
const native = fixture.debugElement.nativeElement.querySelector('button'); const native = fixture.debugElement.nativeElement.querySelector('button');
native.click(); native.click();
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 33 [ simple, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 33 [ simple, right: ]');
}))); })));
it('should support absolute router links', fakeAsync(inject([Router], (router: Router) => { it('should support absolute router links', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp); const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [ children: [
{path: 'link', component: AbsoluteLinkCmp}, {path: 'simple', component: SimpleCmp} {path: 'link', component: AbsoluteLinkCmp}, {path: 'simple', component: SimpleCmp}
] ]
}]); }]);
router.navigateByUrl('/team/22/link'); router.navigateByUrl('/team/22/link');
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]');
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
expect(native.getAttribute('href')).toEqual('/team/33/simple'); expect(native.getAttribute('href')).toEqual('/team/33/simple');
native.click(); native.click();
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 33 [ simple, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 33 [ simple, right: ]');
}))); })));
it('should support relative router links', fakeAsync(inject([Router], (router: Router) => { it('should support relative router links', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp); const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [ children: [
{path: 'link', component: RelativeLinkCmp}, {path: 'simple', component: SimpleCmp} {path: 'link', component: RelativeLinkCmp}, {path: 'simple', component: SimpleCmp}
] ]
}]); }]);
router.navigateByUrl('/team/22/link'); router.navigateByUrl('/team/22/link');
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ link, right: ]');
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
expect(native.getAttribute('href')).toEqual('/team/22/simple'); expect(native.getAttribute('href')).toEqual('/team/22/simple');
native.click(); native.click();
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ simple, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ simple, right: ]');
}))); })));
it('should support top-level link', fakeAsync(inject([Router], (router: Router) => { it('should support top-level link', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RelativeLinkInIfCmp); const fixture = createRoot(router, RelativeLinkInIfCmp);
advance(fixture); advance(fixture);
router.resetConfig( router.resetConfig(
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]); [{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]);
router.navigateByUrl('/'); router.navigateByUrl('/');
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText(' '); expect(fixture.debugElement.nativeElement).toHaveText(' ');
const cmp = fixture.debugElement.componentInstance; const cmp = fixture.debugElement.componentInstance;
cmp.show = true; cmp.show = true;
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('link '); expect(fixture.debugElement.nativeElement).toHaveText('link ');
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
expect(native.getAttribute('href')).toEqual('/simple'); expect(native.getAttribute('href')).toEqual('/simple');
native.click(); native.click();
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('link simple'); expect(fixture.debugElement.nativeElement).toHaveText('link simple');
}))); })));
it('should support query params and fragments', it('should support query params and fragments',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => { fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp); const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [ children: [
{path: 'link', component: LinkWithQueryParamsAndFragment}, {path: 'link', component: LinkWithQueryParamsAndFragment},
{path: 'simple', component: SimpleCmp} {path: 'simple', component: SimpleCmp}
] ]
}]); }]);
router.navigateByUrl('/team/22/link'); router.navigateByUrl('/team/22/link');
advance(fixture); advance(fixture);
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
expect(native.getAttribute('href')).toEqual('/team/22/simple?q=1#f'); expect(native.getAttribute('href')).toEqual('/team/22/simple?q=1#f');
native.click(); native.click();
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ simple, right: ]'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ simple, right: ]');
expect(location.path()).toEqual('/team/22/simple?q=1#f'); expect(location.path()).toEqual('/team/22/simple?q=1#f');
}))); })));
}); });
}
describe('redirects', () => { describe('redirects', () => {
it('should work', fakeAsync(inject([Router, Location], (router: Router, location: Location) => { it('should work', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
@ -1350,119 +1353,121 @@ describe('Integration', () => {
}); });
}); });
describe('routerActiveLink', () => { if (getDOM().supportsDOMEvents()) { // Browser only
it('should set the class when the link is active (a tag)', describe('routerActiveLink', () => {
fakeAsync(inject([Router, Location], (router: Router, location: Location) => { it('should set the class when the link is active (a tag)',
const fixture = createRoot(router, RootCmp); fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [{ children: [{
path: 'link', path: 'link',
component: DummyLinkCmp, component: DummyLinkCmp,
children: children:
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}] [{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]
}] }]
}]); }]);
router.navigateByUrl('/team/22/link;exact=true'); router.navigateByUrl('/team/22/link;exact=true');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link;exact=true'); expect(location.path()).toEqual('/team/22/link;exact=true');
const nativeLink = fixture.debugElement.nativeElement.querySelector('a'); const nativeLink = fixture.debugElement.nativeElement.querySelector('a');
const nativeButton = fixture.debugElement.nativeElement.querySelector('button'); const nativeButton = fixture.debugElement.nativeElement.querySelector('button');
expect(nativeLink.className).toEqual('active'); expect(nativeLink.className).toEqual('active');
expect(nativeButton.className).toEqual('active'); expect(nativeButton.className).toEqual('active');
router.navigateByUrl('/team/22/link/simple'); router.navigateByUrl('/team/22/link/simple');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link/simple'); expect(location.path()).toEqual('/team/22/link/simple');
expect(nativeLink.className).toEqual(''); expect(nativeLink.className).toEqual('');
expect(nativeButton.className).toEqual(''); expect(nativeButton.className).toEqual('');
}))); })));
it('should not set the class until the first navigation succeeds', fakeAsync(() => { it('should not set the class until the first navigation succeeds', fakeAsync(() => {
@Component({ @Component({
template: template:
'<router-outlet></router-outlet><a routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}" >' '<router-outlet></router-outlet><a routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}" >'
}) })
class RootCmpWithLink { class RootCmpWithLink {
} }
TestBed.configureTestingModule({declarations: [RootCmpWithLink]}); TestBed.configureTestingModule({declarations: [RootCmpWithLink]});
const router: Router = TestBed.get(Router); const router: Router = TestBed.get(Router);
const f = TestBed.createComponent(RootCmpWithLink); const f = TestBed.createComponent(RootCmpWithLink);
advance(f); advance(f);
const link = f.debugElement.nativeElement.querySelector('a'); const link = f.debugElement.nativeElement.querySelector('a');
expect(link.className).toEqual(''); expect(link.className).toEqual('');
router.initialNavigation(); router.initialNavigation();
advance(f); advance(f);
expect(link.className).toEqual('active'); expect(link.className).toEqual('active');
})); }));
it('should set the class on a parent element when the link is active', it('should set the class on a parent element when the link is active',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => { fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp); const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [{ children: [{
path: 'link', path: 'link',
component: DummyLinkWithParentCmp, component: DummyLinkWithParentCmp,
children: children:
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}] [{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]
}] }]
}]); }]);
router.navigateByUrl('/team/22/link;exact=true'); router.navigateByUrl('/team/22/link;exact=true');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link;exact=true'); expect(location.path()).toEqual('/team/22/link;exact=true');
const native = fixture.debugElement.nativeElement.querySelector('link-parent'); const native = fixture.debugElement.nativeElement.querySelector('link-parent');
expect(native.className).toEqual('active'); expect(native.className).toEqual('active');
router.navigateByUrl('/team/22/link/simple'); router.navigateByUrl('/team/22/link/simple');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link/simple'); expect(location.path()).toEqual('/team/22/link/simple');
expect(native.className).toEqual(''); expect(native.className).toEqual('');
}))); })));
it('should set the class when the link is active', it('should set the class when the link is active',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => { fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp); const fixture = createRoot(router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [{ children: [{
path: 'link', path: 'link',
component: DummyLinkCmp, component: DummyLinkCmp,
children: children:
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}] [{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]
}] }]
}]); }]);
router.navigateByUrl('/team/22/link'); router.navigateByUrl('/team/22/link');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link'); expect(location.path()).toEqual('/team/22/link');
const native = fixture.debugElement.nativeElement.querySelector('a'); const native = fixture.debugElement.nativeElement.querySelector('a');
expect(native.className).toEqual('active'); expect(native.className).toEqual('active');
router.navigateByUrl('/team/22/link/simple'); router.navigateByUrl('/team/22/link/simple');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link/simple'); expect(location.path()).toEqual('/team/22/link/simple');
expect(native.className).toEqual('active'); expect(native.className).toEqual('active');
}))); })));
}); });
}
describe('lazy loading', () => { describe('lazy loading', () => {
it('works', fakeAsync(inject( it('works', fakeAsync(inject(
@ -1936,4 +1941,4 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
] ]
}) })
class TestModule { class TestModule {
} }