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