use location.replace to ensure the browser can track the redirect in its history

This commit is contained in:
Art Lowel
2022-09-22 12:39:58 +02:00
parent a2f8a5ccfa
commit 241816e836
2 changed files with 20 additions and 12 deletions

View File

@@ -2,17 +2,25 @@ import { TestBed } from '@angular/core/testing';
import { BrowserHardRedirectService } from './browser-hard-redirect.service';
describe('BrowserHardRedirectService', () => {
const origin = 'https://test-host.com:4000';
const mockLocation = {
href: undefined,
pathname: '/pathname',
search: '/search',
origin
} as Location;
const service: BrowserHardRedirectService = new BrowserHardRedirectService(mockLocation);
let origin: string;
let mockLocation: Location;
let service: BrowserHardRedirectService;
beforeEach(() => {
origin = 'https://test-host.com:4000';
mockLocation = {
href: undefined,
pathname: '/pathname',
search: '/search',
origin,
replace: (url: string) => {
mockLocation.href = url;
}
} as Location;
spyOn(mockLocation, 'replace');
service = new BrowserHardRedirectService(mockLocation);
TestBed.configureTestingModule({});
});
@@ -28,8 +36,8 @@ describe('BrowserHardRedirectService', () => {
service.redirect(redirect);
});
it('should update the location', () => {
expect(mockLocation.href).toEqual(redirect);
it('should call location.replace with the new url', () => {
expect(mockLocation.replace).toHaveBeenCalledWith(redirect);
});
});

View File

@@ -24,7 +24,7 @@ export class BrowserHardRedirectService extends HardRedirectService {
* @param url
*/
redirect(url: string) {
this.location.href = url;
this.location.replace(url);
}
/**