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

View File

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