Make login/logout tests compatible with jsdom
[arvados-workbench2.git] / src / store / auth / auth-reducer.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import authReducer from "./auth-reducer";
6 import actions from "./auth-action";
7 import {
8     API_TOKEN_KEY,
9     USER_EMAIL_KEY,
10     USER_FIRST_NAME_KEY,
11     USER_LAST_NAME_KEY
12 } from "../../services/auth-service/auth-service";
13 import { API_HOST } from "../../common/server-api";
14
15 import 'jest-localstorage-mock';
16
17 describe('auth-reducer', () => {
18     beforeAll(() => {
19         localStorage.clear();
20     });
21
22     it('should return default state on initialisation', () => {
23         const initialState = undefined;
24         const state = authReducer(initialState, actions.INIT());
25         expect(state).toEqual({
26             apiToken: undefined,
27             user: undefined
28         });
29     });
30
31     it('should read user and api token from local storage on init if they are there', () => {
32         const initialState = undefined;
33
34         localStorage.setItem(API_TOKEN_KEY, "token");
35         localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
36         localStorage.setItem(USER_FIRST_NAME_KEY, "John");
37         localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
38
39         const state = authReducer(initialState, actions.INIT());
40         expect(state).toEqual({
41             apiToken: "token",
42             user: {
43                 email: "test@test.com",
44                 firstName: "John",
45                 lastName: "Doe"
46             }
47         });
48     });
49
50     it('should store token in local storage', () => {
51         const initialState = undefined;
52
53         const state = authReducer(initialState, actions.SAVE_API_TOKEN("token"));
54         expect(state).toEqual({
55             apiToken: "token",
56             user: undefined
57         });
58
59         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
60     });
61
62     it('should set user details on success fetch', () => {
63         const initialState = undefined;
64
65         const userDetails = {
66             email: "test@test.com",
67             first_name: "John",
68             last_name: "Doe",
69             is_admin: true
70         };
71
72         const state = authReducer(initialState, actions.USER_DETAILS_SUCCESS(userDetails));
73         expect(state).toEqual({
74             apiToken: undefined,
75             user: {
76                 email: "test@test.com",
77                 firstName: "John",
78                 lastName: "Doe"
79             }
80         });
81
82         expect(localStorage.getItem(API_TOKEN_KEY)).toBe("token");
83     });
84
85     it('should fire external url to login', () => {
86         const initialState = undefined;
87         window.location.assign = jest.fn();
88         authReducer(initialState, actions.LOGIN());
89         expect(window.location.assign).toBeCalledWith(
90             `${API_HOST}/login?return_to=${window.location.protocol}//${window.location.host}/token`
91         );
92     });
93
94     it('should fire external url to logout', () => {
95         const initialState = undefined;
96         window.location.assign = jest.fn();
97         authReducer(initialState, actions.LOGOUT());
98         expect(window.location.assign).toBeCalledWith(
99             `${API_HOST}/logout?return_to=${location.protocol}//${location.host}`
100         );
101     });
102 });