test(common): prefer TestBed.inject over inject (#37459)

Use the strongly typed TestBed.inject rather than the weakly typed inject test utility function. Reuse injected dependency variables between sibling test cases.

PR Close #37459
This commit is contained in:
Lars Gyrup Brink Nielsen 2020-06-05 23:24:16 +02:00 committed by atscott
parent 55979fe0ae
commit 0b7845964b
1 changed files with 36 additions and 29 deletions

View File

@ -8,7 +8,7 @@
import {CommonModule, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
import {MockLocationStrategy, MockPlatformLocation} from '@angular/common/testing';
import {inject, TestBed} from '@angular/core/testing';
import {TestBed} from '@angular/core/testing';
const baseUrl = '/base';
@ -41,6 +41,8 @@ describe('Location Class', () => {
});
describe('location.getState()', () => {
let location: Location;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
@ -55,17 +57,19 @@ describe('Location Class', () => {
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
]
});
location = TestBed.inject(Location);
});
it('should get the state object', inject([Location], (location: Location) => {
it('should get the state object', () => {
expect(location.getState()).toBe(null);
location.go('/test', '', {foo: 'bar'});
expect(location.getState()).toEqual({foo: 'bar'});
}));
});
it('should work after using back button', inject([Location], (location: Location) => {
it('should work after using back button', () => {
expect(location.getState()).toBe(null);
location.go('/test1', '', {url: 'test1'});
@ -76,10 +80,13 @@ describe('Location Class', () => {
location.back();
expect(location.getState()).toEqual({url: 'test1'});
}));
});
});
describe('location.onUrlChange()', () => {
let location: Location;
let locationStrategy: MockLocationStrategy;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
@ -94,14 +101,16 @@ describe('Location Class', () => {
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
]
});
location = TestBed.inject(Location);
locationStrategy = TestBed.inject(LocationStrategy) as MockLocationStrategy;
});
it('should have onUrlChange method', inject([Location], (location: Location) => {
it('should have onUrlChange method', () => {
expect(typeof location.onUrlChange).toBe('function');
}));
});
it('should add registered functions to urlChangeListeners',
inject([Location], (location: Location) => {
it('should add registered functions to urlChangeListeners', () => {
function changeListener(url: string, state: unknown) {
return undefined;
}
@ -112,11 +121,9 @@ describe('Location Class', () => {
expect((location as any)._urlChangeListeners.length).toBe(1);
expect((location as any)._urlChangeListeners[0]).toEqual(changeListener);
}));
});
it('should only notify listeners once when multiple listeners are registered', () => {
const location = TestBed.inject(Location);
const locationStrategy = TestBed.inject(LocationStrategy) as MockLocationStrategy;
let notificationCount = 0;
function incrementChangeListener(url: string, state: unknown) {