Skip to content

Commit 825efa8

Browse files
jasonadenbenlesh
authored andcommittedApr 24, 2019
fix(common): adjust MockPlatformLocation to set state to new object (#30055)
When using the `history` API, setting a new `state` and retrieving it does not pass a `===` test to the object used to set the state. In other words, `history.state` is always a copy. This change makes the `MockPlatformLocation` behave in the same way. PR Close #30055
1 parent 3a9cf3f commit 825efa8

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed
 

‎packages/common/testing/src/mock_platform_location.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {Subject} from 'rxjs';
1212

1313
function parseUrl(urlStr: string, baseHref: string) {
1414
const verifyProtocol = /^((http[s]?|ftp):\/\/)/;
15-
let serverBase = '';
15+
let serverBase;
1616

17-
// URL class requires full URL. If the URL string doesn't start with protocol, we need to add an
18-
// arbitrary base URL which can be removed afterward.
17+
// URL class requires full URL. If the URL string doesn't start with protocol, we need to add
18+
// an arbitrary base URL which can be removed afterward.
1919
if (!verifyProtocol.test(urlStr)) {
2020
serverBase = 'http://empty.com/';
2121
}
@@ -62,7 +62,7 @@ export class MockPlatformLocation implements PlatformLocation {
6262
this.baseHref = config.appBaseHref || '';
6363

6464
const parsedChanges =
65-
this.parseChanges(null, config.startUrl || 'http://<empty>', this.baseHref);
65+
this.parseChanges(null, config.startUrl || 'http://<empty>/', this.baseHref);
6666
this.urlChanges[0] = {...parsedChanges};
6767
}
6868
}
@@ -86,7 +86,9 @@ export class MockPlatformLocation implements PlatformLocation {
8686
onHashChange(fn: LocationChangeListener): void { this.hashUpdate.subscribe(fn); }
8787

8888
get href(): string {
89-
return `${this.protocol}//${this.hostname}${this.baseHref}${this.pathname === '/' ? '' : this.pathname}${this.search}${this.hash}`;
89+
let url = `${this.protocol}//${this.hostname}${this.port ? ':' + this.port : ''}`;
90+
url += `${this.pathname === '/' ? '' : this.pathname}${this.search}${this.hash}`
91+
return url;
9092
}
9193

9294
get url(): string { return `${this.pathname}${this.search}${this.hash}`; }
@@ -104,6 +106,8 @@ export class MockPlatformLocation implements PlatformLocation {
104106
}
105107

106108
private parseChanges(state: unknown, url: string, baseHref: string = '') {
109+
// When the `history.state` value is stored, it is always copied.
110+
state = JSON.parse(JSON.stringify(state));
107111
return {...parseUrl(url, baseHref), state};
108112
}
109113

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
{
22
"extends": "../tsconfig-build.json",
3-
43
"compilerOptions": {
54
"baseUrl": ".",
65
"rootDir": "../",
76
"paths": {
8-
"@angular/core": ["../../../dist/packages/core"],
9-
"@angular/common": ["../../../dist/packages/common"]
7+
"@angular/core": [
8+
"../../../dist/packages/core"
9+
],
10+
"@angular/common": [
11+
"../../../dist/packages/common"
12+
]
1013
},
1114
"outDir": "../../../dist/packages/common"
1215
},
13-
1416
"files": [
1517
"public_api.ts",
1618
"../../../node_modules/zone.js/dist/zone.js.d.ts"
1719
],
18-
1920
"angularCompilerOptions": {
2021
"annotateForClosureCompiler": true,
2122
"strictMetadataEmit": false,
2223
"skipTemplateCodegen": true,
2324
"flatModuleOutFile": "testing.js",
2425
"flatModuleId": "@angular/common/testing"
2526
}
26-
}
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.