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