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