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