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