From 175c79d1d831b6c1278914250da016bde184b735 Mon Sep 17 00:00:00 2001 From: Sonu Kapoor Date: Mon, 10 Aug 2020 19:02:47 -0400 Subject: [PATCH] test(docs-infra): remove deprecated `ReflectiveInjector` (#38408) This commit replaces the old and slow `ReflectiveInjector` that was deprecated in v5 with the new `Injector`. Note: This change was only done in the spec files inside the `aio` folder. While changing this, it was not possible to directly use `Injector.get` to get the correct typing for the mocked classes. For example: ```typescript locationService = injector.get(LocationService); ``` Fails with: > Argument of type 'typeof LocationService' is not assignable to parameter of type 'Type | InjectionToken | AbstractType'. Type 'typeof LocationService' is not assignable to type 'Type'. Property 'searchResult' is missing in type 'LocationService' but required in type 'TestLocationService'. Therefore, it was necessary to first convert to `unknown` and then to `TestLocationService`. ```typescript locationService = injector.get(LocationService) as unknown as TestLocationService; ``` PR Close #38408 --- .../contributor-list.component.spec.ts | 20 ++++++----- .../resource/resource-list.component.spec.ts | 20 ++++++----- aio/src/app/search/search.service.spec.ts | 15 ++++---- aio/src/app/shared/deployment.service.spec.ts | 12 +++---- aio/src/app/shared/ga.service.spec.ts | 11 ++++-- aio/src/app/shared/location.service.spec.ts | 34 ++++++++++--------- aio/src/app/shared/logger.service.spec.ts | 10 +++--- .../shared/reporting-error-handler.spec.ts | 13 +++---- aio/src/app/shared/scroll-spy.service.spec.ts | 8 ++--- aio/src/app/shared/scroll.service.spec.ts | 34 +++++++++++-------- aio/src/app/shared/toc.service.spec.ts | 19 ++++++----- .../app/sw-updates/sw-updates.service.spec.ts | 20 +++++------ 12 files changed, 118 insertions(+), 98 deletions(-) diff --git a/aio/src/app/custom-elements/contributor/contributor-list.component.spec.ts b/aio/src/app/custom-elements/contributor/contributor-list.component.spec.ts index 48d9bd2080..72f110ce29 100644 --- a/aio/src/app/custom-elements/contributor/contributor-list.component.spec.ts +++ b/aio/src/app/custom-elements/contributor/contributor-list.component.spec.ts @@ -1,4 +1,4 @@ -import { ReflectiveInjector } from '@angular/core'; +import { Injector } from '@angular/core'; import { of } from 'rxjs'; @@ -12,20 +12,22 @@ import { LocationService } from 'app/shared/location.service'; describe('ContributorListComponent', () => { let component: ContributorListComponent; - let injector: ReflectiveInjector; + let injector: Injector; let contributorService: TestContributorService; let locationService: TestLocationService; let contributorGroups: ContributorGroup[]; beforeEach(() => { - injector = ReflectiveInjector.resolveAndCreate([ - ContributorListComponent, - {provide: ContributorService, useClass: TestContributorService }, - {provide: LocationService, useClass: TestLocationService } - ]); + injector = Injector.create({ + providers: [ + {provide: ContributorListComponent, deps: [ContributorService, LocationService] }, + {provide: ContributorService, useClass: TestContributorService, deps: [] }, + {provide: LocationService, useClass: TestLocationService, deps: [] } + ] + }); - locationService = injector.get(LocationService); - contributorService = injector.get(ContributorService); + locationService = injector.get(LocationService) as unknown as TestLocationService; + contributorService = injector.get(ContributorService) as unknown as TestContributorService; contributorGroups = contributorService.testContributors; }); diff --git a/aio/src/app/custom-elements/resource/resource-list.component.spec.ts b/aio/src/app/custom-elements/resource/resource-list.component.spec.ts index 60a9374252..cfc2bbf554 100644 --- a/aio/src/app/custom-elements/resource/resource-list.component.spec.ts +++ b/aio/src/app/custom-elements/resource/resource-list.component.spec.ts @@ -1,4 +1,4 @@ -import { ReflectiveInjector } from '@angular/core'; +import { Injector } from '@angular/core'; import { of } from 'rxjs'; @@ -12,20 +12,22 @@ import { Category } from './resource.model'; describe('ResourceListComponent', () => { let component: ResourceListComponent; - let injector: ReflectiveInjector; + let injector: Injector; let resourceService: TestResourceService; let locationService: TestLocationService; let categories: Category[]; beforeEach(() => { - injector = ReflectiveInjector.resolveAndCreate([ - ResourceListComponent, - {provide: ResourceService, useClass: TestResourceService }, - {provide: LocationService, useClass: TestLocationService } - ]); + injector = Injector.create({ + providers: [ + {provide: ResourceListComponent, deps: [ResourceService, LocationService] }, + {provide: ResourceService, useClass: TestResourceService, deps: [] }, + {provide: LocationService, useClass: TestLocationService, deps: [] } + ] + }); - locationService = injector.get(LocationService); - resourceService = injector.get(ResourceService); + locationService = injector.get(LocationService) as unknown as TestLocationService; + resourceService = injector.get(ResourceService) as unknown as TestResourceService; categories = resourceService.testCategories; }); diff --git a/aio/src/app/search/search.service.spec.ts b/aio/src/app/search/search.service.spec.ts index 4fad506ac0..906236f161 100644 --- a/aio/src/app/search/search.service.spec.ts +++ b/aio/src/app/search/search.service.spec.ts @@ -1,4 +1,4 @@ -import { ReflectiveInjector, NgZone } from '@angular/core'; +import { Injector, NgZone } from '@angular/core'; import { fakeAsync, tick } from '@angular/core/testing'; import { of } from 'rxjs'; import { SearchService } from './search.service'; @@ -6,7 +6,7 @@ import { WebWorkerClient } from 'app/shared/web-worker'; describe('SearchService', () => { - let injector: ReflectiveInjector; + let injector: Injector; let service: SearchService; let sendMessageSpy: jasmine.Spy; let mockWorker: WebWorkerClient; @@ -16,10 +16,13 @@ describe('SearchService', () => { mockWorker = { sendMessage: sendMessageSpy } as any; spyOn(WebWorkerClient, 'create').and.returnValue(mockWorker); - injector = ReflectiveInjector.resolveAndCreate([ - SearchService, - { provide: NgZone, useFactory: () => new NgZone({ enableLongStackTrace: false }) } - ]); + injector = Injector.create({ + providers: [ + { provide: SearchService, deps: [NgZone]}, + { provide: NgZone, useFactory: () => new NgZone({ enableLongStackTrace: false }), deps: [] } + ] + }); + service = injector.get(SearchService); }); diff --git a/aio/src/app/shared/deployment.service.spec.ts b/aio/src/app/shared/deployment.service.spec.ts index 34df4ec92e..5c19ee2016 100644 --- a/aio/src/app/shared/deployment.service.spec.ts +++ b/aio/src/app/shared/deployment.service.spec.ts @@ -1,4 +1,4 @@ -import { ReflectiveInjector } from '@angular/core'; +import { Injector } from '@angular/core'; import { environment } from 'environments/environment'; import { LocationService } from 'app/shared/location.service'; import { MockLocationService } from 'testing/location.service'; @@ -15,7 +15,7 @@ describe('Deployment service', () => { it('should get the mode from the `mode` query parameter if available', () => { const injector = getInjector(); - const locationService: MockLocationService = injector.get(LocationService); + const locationService = injector.get(LocationService) as unknown as MockLocationService; locationService.search.and.returnValue({ mode: 'bar' }); const deployment = injector.get(Deployment); @@ -25,8 +25,8 @@ describe('Deployment service', () => { }); function getInjector() { - return ReflectiveInjector.resolveAndCreate([ - Deployment, - { provide: LocationService, useFactory: () => new MockLocationService('') } - ]); + return Injector.create({providers: [ + { provide: Deployment, deps: [LocationService] }, + { provide: LocationService, useFactory: () => new MockLocationService(''), deps: [] } + ]}); } diff --git a/aio/src/app/shared/ga.service.spec.ts b/aio/src/app/shared/ga.service.spec.ts index ffd95fbc45..507b23e713 100644 --- a/aio/src/app/shared/ga.service.spec.ts +++ b/aio/src/app/shared/ga.service.spec.ts @@ -1,18 +1,23 @@ -import { ReflectiveInjector } from '@angular/core'; +import { Injector } from '@angular/core'; import { GaService } from 'app/shared/ga.service'; import { WindowToken } from 'app/shared/window'; describe('GaService', () => { let gaService: GaService; - let injector: ReflectiveInjector; + let injector: Injector; let gaSpy: jasmine.Spy; let mockWindow: any; beforeEach(() => { gaSpy = jasmine.createSpy('ga'); mockWindow = { ga: gaSpy }; - injector = ReflectiveInjector.resolveAndCreate([GaService, { provide: WindowToken, useFactory: () => mockWindow }]); + injector = Injector.create({ + providers: [ + { provide: GaService, deps: [WindowToken] }, + { provide: WindowToken, useFactory: () => mockWindow, deps: [] } + ]}); + gaService = injector.get(GaService); }); diff --git a/aio/src/app/shared/location.service.spec.ts b/aio/src/app/shared/location.service.spec.ts index a098d9bf31..9661758493 100644 --- a/aio/src/app/shared/location.service.spec.ts +++ b/aio/src/app/shared/location.service.spec.ts @@ -1,4 +1,4 @@ -import { ReflectiveInjector } from '@angular/core'; +import { Injector } from '@angular/core'; import { Location, LocationStrategy, PlatformLocation } from '@angular/common'; import { MockLocationStrategy } from '@angular/common/testing'; import { Subject } from 'rxjs'; @@ -9,26 +9,28 @@ import { LocationService } from './location.service'; import { ScrollService } from './scroll.service'; describe('LocationService', () => { - let injector: ReflectiveInjector; + let injector: Injector; let location: MockLocationStrategy; let service: LocationService; let swUpdates: MockSwUpdatesService; let scrollService: MockScrollService; beforeEach(() => { - injector = ReflectiveInjector.resolveAndCreate([ - LocationService, - Location, - { provide: GaService, useClass: TestGaService }, - { provide: LocationStrategy, useClass: MockLocationStrategy }, - { provide: PlatformLocation, useClass: MockPlatformLocation }, - { provide: SwUpdatesService, useClass: MockSwUpdatesService }, - { provide: ScrollService, useClass: MockScrollService } - ]); + injector = Injector.create({ + providers: [ + { provide: LocationService, deps: [GaService, Location, ScrollService, PlatformLocation, SwUpdatesService] }, + { provide: Location, deps: [LocationStrategy, PlatformLocation] }, + { provide: GaService, useClass: TestGaService, deps: [] }, + { provide: LocationStrategy, useClass: MockLocationStrategy, deps: [] }, + { provide: PlatformLocation, useClass: MockPlatformLocation, deps: [] }, + { provide: SwUpdatesService, useClass: MockSwUpdatesService, deps: [] }, + { provide: ScrollService, useClass: MockScrollService, deps: [] } + ] + }); - location = injector.get(LocationStrategy); - service = injector.get(LocationService); - swUpdates = injector.get(SwUpdatesService); + location = injector.get(LocationStrategy) as unknown as MockLocationStrategy; + service = injector.get(LocationService); + swUpdates = injector.get(SwUpdatesService) as unknown as MockSwUpdatesService; scrollService = injector.get(ScrollService); }); @@ -380,7 +382,7 @@ describe('LocationService', () => { let platformLocation: MockPlatformLocation; beforeEach(() => { - platformLocation = injector.get(PlatformLocation); + platformLocation = injector.get(PlatformLocation) as unknown as MockPlatformLocation; }); it('should call replaceState on PlatformLocation', () => { @@ -577,7 +579,7 @@ describe('LocationService', () => { let gaLocationChanged: jasmine.Spy; beforeEach(() => { - const gaService = injector.get(GaService); + const gaService = injector.get(GaService) as unknown as TestGaService; gaLocationChanged = gaService.locationChanged; // execute currentPath observable so that gaLocationChanged is called service.currentPath.subscribe(); diff --git a/aio/src/app/shared/logger.service.spec.ts b/aio/src/app/shared/logger.service.spec.ts index d676cc4e37..2dedb6e228 100644 --- a/aio/src/app/shared/logger.service.spec.ts +++ b/aio/src/app/shared/logger.service.spec.ts @@ -1,4 +1,4 @@ -import { ErrorHandler, ReflectiveInjector } from '@angular/core'; +import { ErrorHandler, Injector } from '@angular/core'; import { Logger } from './logger.service'; describe('logger service', () => { @@ -10,10 +10,10 @@ describe('logger service', () => { beforeEach(() => { logSpy = spyOn(console, 'log'); warnSpy = spyOn(console, 'warn'); - const injector = ReflectiveInjector.resolveAndCreate([ - Logger, - { provide: ErrorHandler, useClass: MockErrorHandler } - ]); + const injector = Injector.create({providers: [ + { provide: Logger, deps: [ErrorHandler] }, + { provide: ErrorHandler, useClass: MockErrorHandler, deps: [] } + ]}); logger = injector.get(Logger); errorHandler = injector.get(ErrorHandler); }); diff --git a/aio/src/app/shared/reporting-error-handler.spec.ts b/aio/src/app/shared/reporting-error-handler.spec.ts index a7389824d4..39fb790e30 100644 --- a/aio/src/app/shared/reporting-error-handler.spec.ts +++ b/aio/src/app/shared/reporting-error-handler.spec.ts @@ -1,4 +1,4 @@ -import { ErrorHandler, ReflectiveInjector } from '@angular/core'; +import { ErrorHandler, Injector } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { WindowToken } from 'app/shared/window'; import { AppModule } from 'app/app.module'; @@ -14,11 +14,12 @@ describe('ReportingErrorHandler service', () => { onerrorSpy = jasmine.createSpy('onerror'); superHandler = spyOn(ErrorHandler.prototype, 'handleError'); - const injector = ReflectiveInjector.resolveAndCreate([ - { provide: ErrorHandler, useClass: ReportingErrorHandler }, - { provide: WindowToken, useFactory: () => ({ onerror: onerrorSpy }) } - ]); - handler = injector.get(ErrorHandler); + const injector = Injector.create({providers: [ + { provide: ErrorHandler, useClass: ReportingErrorHandler, deps: [WindowToken] }, + { provide: WindowToken, useFactory: () => ({ onerror: onerrorSpy }), deps: [] } + ]}); + + handler = injector.get(ErrorHandler) as unknown as ReportingErrorHandler; }); it('should be registered on the AppModule', () => { diff --git a/aio/src/app/shared/scroll-spy.service.spec.ts b/aio/src/app/shared/scroll-spy.service.spec.ts index 4563683c72..9184839fa5 100644 --- a/aio/src/app/shared/scroll-spy.service.spec.ts +++ b/aio/src/app/shared/scroll-spy.service.spec.ts @@ -1,4 +1,4 @@ -import { Injector, ReflectiveInjector } from '@angular/core'; +import { Injector } from '@angular/core'; import { fakeAsync, tick } from '@angular/core/testing'; import { DOCUMENT } from '@angular/common'; @@ -151,11 +151,11 @@ describe('ScrollSpyService', () => { let scrollSpyService: ScrollSpyService; beforeEach(() => { - injector = ReflectiveInjector.resolveAndCreate([ + injector = Injector.create({providers: [ { provide: DOCUMENT, useValue: { body: {} } }, { provide: ScrollService, useValue: { topOffset: 50 } }, - ScrollSpyService - ]); + { provide: ScrollSpyService, deps: [DOCUMENT, ScrollService] } + ]}); scrollSpyService = injector.get(ScrollSpyService); }); diff --git a/aio/src/app/shared/scroll.service.spec.ts b/aio/src/app/shared/scroll.service.spec.ts index bd3ffd71f1..51cbfb490c 100644 --- a/aio/src/app/shared/scroll.service.spec.ts +++ b/aio/src/app/shared/scroll.service.spec.ts @@ -1,7 +1,7 @@ import {Location, LocationStrategy, PlatformLocation, ViewportScroller} from '@angular/common'; import {DOCUMENT} from '@angular/common'; import {MockLocationStrategy, SpyLocation} from '@angular/common/testing'; -import {ReflectiveInjector} from '@angular/core'; +import {Injector} from '@angular/core'; import {fakeAsync, tick} from '@angular/core/testing'; import {ScrollService, topMargin} from './scroll.service'; @@ -15,7 +15,7 @@ describe('ScrollService', () => { }; const topOfPageElem = {} as Element; - let injector: ReflectiveInjector; + let injector: Injector; let document: MockDocument; let platformLocation: MockPlatformLocation; let scrollService: ScrollService; @@ -41,21 +41,25 @@ describe('ScrollService', () => { jasmine.createSpyObj('viewportScroller', ['getScrollPosition', 'scrollToPosition']); beforeEach(() => { - injector = ReflectiveInjector.resolveAndCreate([ - { - provide: ScrollService, - useFactory: createScrollService, - deps: [DOCUMENT, PlatformLocation, ViewportScroller, Location], - }, - {provide: Location, useClass: SpyLocation}, {provide: DOCUMENT, useClass: MockDocument}, - {provide: PlatformLocation, useClass: MockPlatformLocation}, - {provide: ViewportScroller, useValue: viewportScrollerStub}, - {provide: LocationStrategy, useClass: MockLocationStrategy} - ]); + injector = Injector.create( { + providers: [ + { + provide: ScrollService, + useFactory: createScrollService, + deps: [DOCUMENT, PlatformLocation, ViewportScroller, Location], + }, + {provide: Location, useClass: SpyLocation, deps: [] }, + {provide: DOCUMENT, useClass: MockDocument, deps: []}, + {provide: PlatformLocation, useClass: MockPlatformLocation, deps: []}, + {provide: ViewportScroller, useValue: viewportScrollerStub}, + {provide: LocationStrategy, useClass: MockLocationStrategy, deps: []} + ] + }); + platformLocation = injector.get(PlatformLocation); - document = injector.get(DOCUMENT); + document = injector.get(DOCUMENT) as unknown as MockDocument; scrollService = injector.get(ScrollService); - location = injector.get(Location); + location = injector.get(Location) as unknown as SpyLocation; spyOn(window, 'scrollBy'); }); diff --git a/aio/src/app/shared/toc.service.spec.ts b/aio/src/app/shared/toc.service.spec.ts index ffe765e820..be5cd36fe5 100644 --- a/aio/src/app/shared/toc.service.spec.ts +++ b/aio/src/app/shared/toc.service.spec.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { ReflectiveInjector } from '@angular/core'; +import { Injector } from '@angular/core'; import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; import { Subject } from 'rxjs'; @@ -7,7 +7,7 @@ import { ScrollItem, ScrollSpyInfo, ScrollSpyService } from 'app/shared/scroll-s import { TocItem, TocService } from './toc.service'; describe('TocService', () => { - let injector: ReflectiveInjector; + let injector: Injector; let scrollSpyService: MockScrollSpyService; let tocService: TocService; let lastTocList: TocItem[]; @@ -21,13 +21,14 @@ describe('TocService', () => { } beforeEach(() => { - injector = ReflectiveInjector.resolveAndCreate([ - { provide: DomSanitizer, useClass: TestDomSanitizer }, + injector = Injector.create({providers: [ + { provide: DomSanitizer, useClass: TestDomSanitizer, deps: [] }, { provide: DOCUMENT, useValue: document }, - { provide: ScrollSpyService, useClass: MockScrollSpyService }, - TocService, - ]); - scrollSpyService = injector.get(ScrollSpyService); + { provide: ScrollSpyService, useClass: MockScrollSpyService, deps: [] }, + { provide: TocService, deps: [DOCUMENT, DomSanitizer, ScrollSpyService] }, + ]}); + + scrollSpyService = injector.get(ScrollSpyService) as unknown as MockScrollSpyService; tocService = injector.get(TocService); tocService.tocList.subscribe(tocList => lastTocList = tocList); }); @@ -330,7 +331,7 @@ describe('TocService', () => { }); it('should have bypassed HTML sanitizing of heading\'s innerHTML ', () => { - const domSanitizer: TestDomSanitizer = injector.get(DomSanitizer); + const domSanitizer: TestDomSanitizer = injector.get(DomSanitizer) as unknown as TestDomSanitizer; expect(domSanitizer.bypassSecurityTrustHtml) .toHaveBeenCalledWith('Setup to develop locally.'); }); diff --git a/aio/src/app/sw-updates/sw-updates.service.spec.ts b/aio/src/app/sw-updates/sw-updates.service.spec.ts index e99330d663..fa35b19931 100644 --- a/aio/src/app/sw-updates/sw-updates.service.spec.ts +++ b/aio/src/app/sw-updates/sw-updates.service.spec.ts @@ -1,4 +1,4 @@ -import { ApplicationRef, ReflectiveInjector } from '@angular/core'; +import { ApplicationRef, Injector } from '@angular/core'; import { discardPeriodicTasks, fakeAsync, tick } from '@angular/core/testing'; import { SwUpdate } from '@angular/service-worker'; import { Subject } from 'rxjs'; @@ -8,7 +8,7 @@ import { SwUpdatesService } from './sw-updates.service'; describe('SwUpdatesService', () => { - let injector: ReflectiveInjector; + let injector: Injector; let appRef: MockApplicationRef; let service: SwUpdatesService; let swu: MockSwUpdate; @@ -21,16 +21,16 @@ describe('SwUpdatesService', () => { // run `setup()`/`tearDown()` in `beforeEach()`/`afterEach()` blocks. We use the `run()` helper // to call them inside each test's zone. const setup = (isSwUpdateEnabled: boolean) => { - injector = ReflectiveInjector.resolveAndCreate([ - { provide: ApplicationRef, useClass: MockApplicationRef }, - { provide: Logger, useClass: MockLogger }, - { provide: SwUpdate, useFactory: () => new MockSwUpdate(isSwUpdateEnabled) }, - SwUpdatesService - ]); + injector = Injector.create({providers: [ + { provide: ApplicationRef, useClass: MockApplicationRef, deps: [] }, + { provide: Logger, useClass: MockLogger, deps: [] }, + { provide: SwUpdate, useFactory: () => new MockSwUpdate(isSwUpdateEnabled), deps: [] }, + { provide: SwUpdatesService, deps: [ApplicationRef, Logger, SwUpdate] } + ]}); - appRef = injector.get(ApplicationRef); + appRef = injector.get(ApplicationRef) as unknown as MockApplicationRef; service = injector.get(SwUpdatesService); - swu = injector.get(SwUpdate); + swu = injector.get(SwUpdate) as unknown as MockSwUpdate; checkInterval = (service as any).checkInterval; }; const tearDown = () => service.ngOnDestroy();