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,31 +57,36 @@ describe('Location Class', () => {
{provide: Location, useClass: Location, deps: [LocationStrategy, PlatformLocation]},
]
});
location = TestBed.inject(Location);
});
it('should get the state object', inject([Location], (location: Location) => {
expect(location.getState()).toBe(null);
it('should get the state object', () => {
expect(location.getState()).toBe(null);
location.go('/test', '', {foo: 'bar'});
location.go('/test', '', {foo: 'bar'});
expect(location.getState()).toEqual({foo: 'bar'});
}));
expect(location.getState()).toEqual({foo: 'bar'});
});
it('should work after using back button', inject([Location], (location: Location) => {
expect(location.getState()).toBe(null);
it('should work after using back button', () => {
expect(location.getState()).toBe(null);
location.go('/test1', '', {url: 'test1'});
location.go('/test2', '', {url: 'test2'});
location.go('/test1', '', {url: 'test1'});
location.go('/test2', '', {url: 'test2'});
expect(location.getState()).toEqual({url: 'test2'});
expect(location.getState()).toEqual({url: 'test2'});
location.back();
location.back();
expect(location.getState()).toEqual({url: 'test1'});
}));
expect(location.getState()).toEqual({url: 'test1'});
});
});
describe('location.onUrlChange()', () => {
let location: Location;
let locationStrategy: MockLocationStrategy;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CommonModule],
@ -94,29 +101,29 @@ 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) => {
expect(typeof location.onUrlChange).toBe('function');
}));
it('should have onUrlChange method', () => {
expect(typeof location.onUrlChange).toBe('function');
});
it('should add registered functions to urlChangeListeners',
inject([Location], (location: Location) => {
function changeListener(url: string, state: unknown) {
return undefined;
}
it('should add registered functions to urlChangeListeners', () => {
function changeListener(url: string, state: unknown) {
return undefined;
}
expect((location as any)._urlChangeListeners.length).toBe(0);
expect((location as any)._urlChangeListeners.length).toBe(0);
location.onUrlChange(changeListener);
location.onUrlChange(changeListener);
expect((location as any)._urlChangeListeners.length).toBe(1);
expect((location as any)._urlChangeListeners[0]).toEqual(changeListener);
}));
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) {