Skip to content

Commit 241816e

Browse files
committed
use location.replace to ensure the browser can track the redirect in its history
1 parent a2f8a5c commit 241816e

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/app/core/services/browser-hard-redirect.service.spec.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ import { TestBed } from '@angular/core/testing';
22
import { BrowserHardRedirectService } from './browser-hard-redirect.service';
33

44
describe('BrowserHardRedirectService', () => {
5-
const origin = 'https://test-host.com:4000';
6-
const mockLocation = {
7-
href: undefined,
8-
pathname: '/pathname',
9-
search: '/search',
10-
origin
11-
} as Location;
12-
13-
const service: BrowserHardRedirectService = new BrowserHardRedirectService(mockLocation);
5+
let origin: string;
6+
let mockLocation: Location;
7+
let service: BrowserHardRedirectService;
148

159
beforeEach(() => {
10+
origin = 'https://test-host.com:4000';
11+
mockLocation = {
12+
href: undefined,
13+
pathname: '/pathname',
14+
search: '/search',
15+
origin,
16+
replace: (url: string) => {
17+
mockLocation.href = url;
18+
}
19+
} as Location;
20+
spyOn(mockLocation, 'replace');
21+
22+
service = new BrowserHardRedirectService(mockLocation);
23+
1624
TestBed.configureTestingModule({});
1725
});
1826

@@ -28,8 +36,8 @@ describe('BrowserHardRedirectService', () => {
2836
service.redirect(redirect);
2937
});
3038

31-
it('should update the location', () => {
32-
expect(mockLocation.href).toEqual(redirect);
39+
it('should call location.replace with the new url', () => {
40+
expect(mockLocation.replace).toHaveBeenCalledWith(redirect);
3341
});
3442
});
3543

src/app/core/services/browser-hard-redirect.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class BrowserHardRedirectService extends HardRedirectService {
2424
* @param url
2525
*/
2626
redirect(url: string) {
27-
this.location.href = url;
27+
this.location.replace(url);
2828
}
2929

3030
/**

0 commit comments

Comments
 (0)