X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/4361d82d939fb3d8d6009b6bdfb66922634ba005..43551086cc04bb37a2b1dc6c8ec24af44f2acf8d:/src/common/redirect-to.test.ts diff --git a/src/common/redirect-to.test.ts b/src/common/redirect-to.test.ts index 94000a98..e25d7be9 100644 --- a/src/common/redirect-to.test.ts +++ b/src/common/redirect-to.test.ts @@ -5,17 +5,40 @@ import { storeRedirects, handleRedirects } from './redirect-to'; describe('redirect-to', () => { - const redirectTo = 'http://localhost/test123'; + const { location } = window; + const config: any = { + keepWebServiceUrl: 'http://localhost', + keepWebServiceInlineUrl: 'http://localhost' + }; + const redirectTo = '/test123'; + const locationTemplate = { + hash: '', + hostname: '', + origin: '', + host: '', + pathname: '', + port: '80', + protocol: 'http', + search: '', + reload: () => { }, + replace: () => { }, + assign: () => { }, + ancestorOrigins: [], + href: '', + }; + + afterAll((): void => { + window.location = location; + }); describe('storeRedirects', () => { beforeEach(() => { - Object.defineProperty(window, 'location', { - value: { - href: `${window.location.href}?redirectTo=${redirectTo}` - }, - writable: true - }); - Object.defineProperty(window, 'sessionStorage', { + delete window.location; + window.location = { + ...locationTemplate, + href: `${location.href}?redirectTo=${redirectTo}`, + } as any; + Object.defineProperty(window, 'localStorage', { value: { setItem: jest.fn(), }, @@ -28,19 +51,18 @@ describe('redirect-to', () => { storeRedirects(); // then - expect(window.sessionStorage.setItem).toHaveBeenCalledWith('redirectTo', redirectTo); + expect(window.localStorage.setItem).toHaveBeenCalledWith('redirectTo', redirectTo); }); }); describe('handleRedirects', () => { beforeEach(() => { - Object.defineProperty(window, 'location', { - value: { - href: '' - }, - writable: true - }); - Object.defineProperty(window, 'sessionStorage', { + delete window.location; + window.location = { + ...locationTemplate, + href: `${location.href}?redirectTo=${redirectTo}`, + } as any;; + Object.defineProperty(window, 'localStorage', { value: { getItem: () => redirectTo, removeItem: jest.fn(), @@ -50,14 +72,11 @@ describe('redirect-to', () => { }); it('should redirect to page when it is present in session storage', () => { - // given - const token = 'testToken'; - // when - handleRedirects(token); + handleRedirects("abcxyz", config); // then - expect(window.location.href).toBe(`${redirectTo}?api_token=${token}`); + expect(window.location.href).toBe(`${config.keepWebServiceUrl}${redirectTo}?api_token=abcxyz`); }); }); -}); \ No newline at end of file +});