Merge branch '16719-collection-version-basic-ui'
[arvados-workbench2.git] / 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 { location } = window;
9     const config: any = {
10         keepWebServiceUrl: 'http://localhost',
11         keepWebServiceInlineUrl: 'http://localhost'
12     };
13     const redirectTo = '/test123';
14     const locationTemplate = {
15         hash: '',
16         hostname: '',
17         origin: '',
18         host: '',
19         pathname: '',
20         port: '80',
21         protocol: 'http',
22         search: '',
23         reload: () => { },
24         replace: () => { },
25         assign: () => { },
26         ancestorOrigins: [],
27         href: '',
28     };
29
30     afterAll((): void => {
31         window.location = location;
32     });
33
34     describe('storeRedirects', () => {
35         beforeEach(() => {
36             delete window.location;
37             window.location = {
38                 ...locationTemplate,
39                 href: `${location.href}?redirectTo=${redirectTo}`,
40             } as any;
41             Object.defineProperty(window, 'localStorage', {
42                 value: {
43                     setItem: jest.fn(),
44                 },
45                 writable: true
46             });
47         });
48
49         it('should store redirectTo in the session storage', () => {
50             // when
51             storeRedirects();
52
53             // then
54             expect(window.localStorage.setItem).toHaveBeenCalledWith('redirectTo', redirectTo);
55         });
56     });
57
58     describe('handleRedirects', () => {
59         beforeEach(() => {
60             delete window.location;
61             window.location = {
62                 ...locationTemplate,
63                 href: `${location.href}?redirectTo=${redirectTo}`,
64             } as any;;
65             Object.defineProperty(window, 'localStorage', {
66                 value: {
67                     getItem: () => redirectTo,
68                     removeItem: jest.fn(),
69                 },
70                 writable: true
71             });
72         });
73
74         it('should redirect to page when it is present in session storage', () => {
75             // when
76             handleRedirects("abcxyz", config);
77
78             // then
79             expect(window.location.href).toBe(`${config.keepWebServiceUrl}${redirectTo}?api_token=abcxyz`);
80         });
81     });
82 });