94000a98c489beb49f990987bd52b31efb318600
[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 redirectTo = 'http://localhost/test123';
9
10     describe('storeRedirects', () => {
11         beforeEach(() => {
12             Object.defineProperty(window, 'location', {
13                 value: {
14                     href: `${window.location.href}?redirectTo=${redirectTo}`
15                 },
16                 writable: true
17             });
18             Object.defineProperty(window, 'sessionStorage', {
19                 value: {
20                     setItem: jest.fn(),
21                 },
22                 writable: true
23             });
24         });
25
26         it('should store redirectTo in the session storage', () => {
27             // when
28             storeRedirects();
29
30             // then
31             expect(window.sessionStorage.setItem).toHaveBeenCalledWith('redirectTo', redirectTo);
32         });
33     });
34
35     describe('handleRedirects', () => {
36         beforeEach(() => {
37             Object.defineProperty(window, 'location', {
38                 value: {
39                     href: ''
40                 },
41                 writable: true
42             });
43             Object.defineProperty(window, 'sessionStorage', {
44                 value: {
45                     getItem: () => redirectTo,
46                     removeItem: jest.fn(),
47                 },
48                 writable: true
49             });
50         });
51
52         it('should redirect to page when it is present in session storage', () => {
53             // given
54             const token = 'testToken';
55
56             // when
57             handleRedirects(token);
58
59             // then
60             expect(window.location.href).toBe(`${redirectTo}?api_token=${token}`);
61         });
62     });
63 });