Merge branch '21900-groups-panel-toolbar'
[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     // PDH is immaterial; for explanation: md5+sizehint of manifest (replace
16     // <LF> with linefeed char)
17     // `. d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo\040-\040%27?a=b<LF>`
18     // ie. empty file with filename 'foo - %27?a=b'
19     const underlyingPath = '/c=6b1c735de6ae0f2e60cd75d7de36476f+61/foo - %27?a=b';
20     const redirectToParamInput = '/c=6b1c735de6ae0f2e60cd75d7de36476f%2B61/foo%20-%20%2527?a=b';
21     const locationTemplate = {
22         hash: '',
23         hostname: '',
24         origin: '',
25         host: '',
26         pathname: '',
27         port: '80',
28         protocol: 'http',
29         search: '',
30         reload: () => { },
31         replace: () => { },
32         assign: () => { },
33         ancestorOrigins: [],
34         href: '',
35     };
36
37     afterAll((): void => {
38         mockWindow.location = location;
39     });
40
41     describe('storeRedirects', () => {
42         beforeEach(() => {
43             delete mockWindow.location;
44             mockWindow.location = {
45                 ...locationTemplate,
46                 href: `${location.href}?redirectToDownload=${redirectToParamInput}`,
47             } as any;
48             Object.defineProperty(mockWindow, 'localStorage', {
49                 value: {
50                     setItem: jest.fn(),
51                 },
52                 writable: true
53             });
54         });
55
56         it('should store decoded target path in the local storage', () => {
57             // when
58             storeRedirects();
59
60             // then
61             expect(mockWindow.localStorage.setItem).toHaveBeenCalledWith('redirectToDownload', underlyingPath);
62         });
63     });
64
65     describe('handleRedirects', () => {
66         beforeEach(() => {
67             delete mockWindow.location;
68             mockWindow.location = {
69                 ...locationTemplate,
70                 href: `${location.href}?redirectToDownload=${redirectToParamInput}`,
71             } as any;
72             Object.defineProperty(mockWindow, 'localStorage', {
73                 value: {
74                     getItem: () => underlyingPath,
75                     removeItem: jest.fn(),
76                 },
77                 writable: true
78             });
79         });
80
81         it('should redirect to page when it is present in local storage', () => {
82             // when
83             handleRedirects("abcxyz", config);
84
85             // then
86             let navTarget = new URL(mockWindow.location.href);
87             expect(navTarget.origin).toBe(config.keepWebServiceUrl);
88             expect(decodeURIComponent(navTarget.pathname)).toBe(underlyingPath);
89             expect(navTarget.search).toBe('?api_token=abcxyz');
90         });
91     });
92 });