Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(upgrade): update router upgrade tests to use fewer mocks (#43441)
This updates the router upgrade tests to use less mocked behavior. The test upgrade location module is copied from the one that's used in the common package. This update to the tests verifies more real behavior of the upgrade module. PR Close #43441
- Loading branch information
1 parent
64adfc9
commit 123f42c
Showing
3 changed files
with
177 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/router/upgrade/test/upgrade_location_test_module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {APP_BASE_HREF, CommonModule, Location, LocationStrategy, PlatformLocation} from '@angular/common'; | ||
import {MockPlatformLocation} from '@angular/common/testing'; | ||
import {$locationShim, $locationShimProvider, LocationUpgradeModule, UrlCodec} from '@angular/common/upgrade'; | ||
import {Inject, InjectionToken, ModuleWithProviders, NgModule, Optional} from '@angular/core'; | ||
import {UpgradeModule} from '@angular/upgrade/static'; | ||
|
||
export interface LocationUpgradeTestingConfig { | ||
useHash?: boolean; | ||
hashPrefix?: string; | ||
urlCodec?: typeof UrlCodec; | ||
startUrl?: string; | ||
appBaseHref?: string; | ||
} | ||
|
||
/** | ||
* @description | ||
* | ||
* Is used in DI to configure the router. | ||
*/ | ||
export const LOC_UPGRADE_TEST_CONFIG = | ||
new InjectionToken<LocationUpgradeTestingConfig>('LOC_UPGRADE_TEST_CONFIG'); | ||
|
||
|
||
export const APP_BASE_HREF_RESOLVED = new InjectionToken<string>('APP_BASE_HREF_RESOLVED'); | ||
|
||
/** | ||
* Module used for configuring Angular's LocationUpgradeService. | ||
*/ | ||
@NgModule({imports: [CommonModule]}) | ||
export class LocationUpgradeTestModule { | ||
static config(config?: LocationUpgradeTestingConfig): | ||
ModuleWithProviders<LocationUpgradeTestModule> { | ||
return { | ||
ngModule: LocationUpgradeTestModule, | ||
providers: [ | ||
{provide: LOC_UPGRADE_TEST_CONFIG, useValue: config || {}}, { | ||
provide: PlatformLocation, | ||
useFactory: (appBaseHref?: string) => { | ||
if (config && config.appBaseHref != null) { | ||
appBaseHref = config.appBaseHref; | ||
} else if (appBaseHref == null) { | ||
appBaseHref = ''; | ||
} | ||
return new MockPlatformLocation( | ||
{startUrl: config && config.startUrl, appBaseHref: appBaseHref}); | ||
}, | ||
deps: [[new Inject(APP_BASE_HREF), new Optional()]] | ||
}, | ||
{ | ||
provide: $locationShim, | ||
useFactory: provide$location, | ||
deps: [ | ||
UpgradeModule, Location, PlatformLocation, UrlCodec, LocationStrategy, | ||
LOC_UPGRADE_TEST_CONFIG | ||
] | ||
}, | ||
LocationUpgradeModule | ||
.config({ | ||
appBaseHref: config && config.appBaseHref, | ||
useHash: config && config.useHash || false | ||
}) | ||
.providers! | ||
], | ||
}; | ||
} | ||
} | ||
|
||
export function provide$location( | ||
ngUpgrade: UpgradeModule, location: Location, platformLocation: PlatformLocation, | ||
urlCodec: UrlCodec, locationStrategy: LocationStrategy, config?: LocationUpgradeTestingConfig) { | ||
const $locationProvider = | ||
new $locationShimProvider(ngUpgrade, location, platformLocation, urlCodec, locationStrategy); | ||
|
||
$locationProvider.hashPrefix(config && config.hashPrefix); | ||
$locationProvider.html5Mode(config && !config.useHash); | ||
|
||
return $locationProvider.$get(); | ||
} |