Merge branch '21834-stdout-timestamps'
[arvados.git] / services / workbench2 / src / common / redirect-to.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { storeRedirects, handleRedirects } from './redirect-to';
6
7 describe('redirect-to', () => {
8     const mockWindow: { location?: any, localStorage?: any} = window
9
10     const { location } = mockWindow;
11     const config: any = {
12         keepWebServiceUrl: 'http://localhost',
13         keepWebServiceInlineUrl: 'http://localhost-inline'
14     };
15     const redirectTo = 'c=acbd18db4cc2f85cedef654fccc4a4d8%2B3/foo';
16     const locationTemplate = {
17         hash: '',
18         hostname: '',
19         origin: '',
20         host: '',
21         pathname: '',
22         port: '80',
23         protocol: 'http',
24         search: '',
25         reload: () => { },
26         replace: () => { },
27         assign: () => { },
28         ancestorOrigins: [],
29         href: '',
30     };
31
32     afterAll((): void => {
33         mockWindow.location = location;
34     });
35
36     describe('storeRedirects', () => {
37         beforeEach(() => {
38             delete mockWindow.location;
39             mockWindow.location = {
40                 ...locationTemplate,
41                 href: `${location.href}?redirectToDownload=${redirectTo}`,
42             } as any;
43             Object.defineProperty(mockWindow, 'localStorage', {
44                 value: {
45                     setItem: jest.fn(),
46                 },
47                 writable: true
48             });
49         });
50
51         it('should store redirectTo in the session storage', () => {
52             // when
53             storeRedirects();
54
55             // then
56             expect(mockWindow.localStorage.setItem).toHaveBeenCalledWith('redirectToDownload', decodeURIComponent(redirectTo));
57         });
58     });
59
60     describe('handleRedirects', () => {
61         beforeEach(() => {
62             delete mockWindow.location;
63             mockWindow.location = {
64                 ...locationTemplate,
65                 href: `${location.href}?redirectToDownload=${redirectTo}`,
66             } as any;;
67             Object.defineProperty(mockWindow, 'localStorage', {
68                 value: {
69                     getItem: () => redirectTo,
70                     removeItem: jest.fn(),
71                 },
72                 writable: true
73             });
74         });
75
76         it('should redirect to page when it is present in session storage', () => {
77             // when
78             handleRedirects("abcxyz", config);
79
80             // then
81             expect(mockWindow.location.href).toBe(`${config.keepWebServiceUrl}${redirectTo}?api_token=abcxyz`);
82         });
83     });
84 });