Skip to content

Commit

Permalink
feat(platform-browser): include Location mocks into testing environme…
Browse files Browse the repository at this point in the history
…nt by default

Currently, the `Location` and `LocationStrategy` mocks are added by the Router's testing module (the `RouterTestingModule`). However, the `Location` and `LocationStrategy` abstractions are parts of the `@angular/common` package and can be used outside of the Router logic to interact with the location.

This PR adds the `Location` and `LocationStrategy` mocks into the `BrowserTestingModule` NgModule, which is used for environment setup in TestBed. Adding the mocks there has 2 main advantages:

- `Location` and `LocationStrategy` mocks will always be setup when you use TestBed (note: if you provide location mocks in the TestBed.configureTestingModule, custom mocks would still be used), which would cover cases when the `Location` is used in apps code directly.
- The Router would not need to provide the mocks for abstractions that belong to the different package (`@angular/common`), which should help simplify the standalone Router APIs for testing.
  • Loading branch information
AndrewKushnir committed Oct 5, 2022
1 parent ab4ef26 commit 18ef292
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/platform-browser/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ ng_module(
name = "testing",
srcs = glob(["**/*.ts"]),
deps = [
"//packages/common",
"//packages/common/testing",
"//packages/core",
"//packages/core/testing",
"//packages/platform-browser",
Expand Down
3 changes: 3 additions & 0 deletions packages/platform-browser/testing/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 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 {PlatformLocation} from '@angular/common';
import {MockPlatformLocation} from '@angular/common/testing';
import {APP_ID, createPlatformFactory, NgModule, NgZone, PLATFORM_INITIALIZER, platformCore, StaticProvider} from '@angular/core';
import {BrowserModule, ɵBrowserDomAdapter as BrowserDomAdapter} from '@angular/platform-browser';

Expand Down Expand Up @@ -36,6 +38,7 @@ export const platformBrowserTesting =
providers: [
{provide: APP_ID, useValue: 'a'},
{provide: NgZone, useFactory: createNgZone},
{provide: PlatformLocation, useClass: MockPlatformLocation},
]
})
export class BrowserTestingModule {
Expand Down

0 comments on commit 18ef292

Please sign in to comment.