18881: Fixes process state indicator, with tests.
[arvados-workbench2.git] / src / common / redirect-to.test.ts
index 94000a98c489beb49f990987bd52b31efb318600..e25d7be9530826ff09f8c7d8845b44ea9e756fe1 100644 (file)
@@ -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
+});