refs #14161 Merge branch 'origin/14161-inputs-focus-enter-action'
[arvados-workbench2.git] / src / store / auth / auth-actions.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { authReducer, AuthState } from "./auth-reducer";
6 import { AuthAction, initAuth } from "./auth-action";
7 import {
8     API_TOKEN_KEY,
9     USER_EMAIL_KEY,
10     USER_FIRST_NAME_KEY,
11     USER_LAST_NAME_KEY,
12     USER_OWNER_UUID_KEY,
13     USER_UUID_KEY
14 } from "~/services/auth-service/auth-service";
15
16 import 'jest-localstorage-mock';
17 import { createServices } from "~/services/services";
18 import { configureStore, RootStore } from "../store";
19 import createBrowserHistory from "history/createBrowserHistory";
20 import { mockConfig } from '~/common/config';
21 import { ApiActions } from "~/services/api/api-actions";
22
23 describe('auth-actions', () => {
24     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
25     let store: RootStore;
26     const actions: ApiActions = {
27         progressFn: (id: string, working: boolean) => {},
28         errorFn: (id: string, message: string) => {}
29     };
30
31     beforeEach(() => {
32         store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
33         localStorage.clear();
34         reducer = authReducer(createServices(mockConfig({}), actions));
35     });
36
37     it('should initialise state with user and api token from local storage', () => {
38
39         localStorage.setItem(API_TOKEN_KEY, "token");
40         localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
41         localStorage.setItem(USER_FIRST_NAME_KEY, "John");
42         localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
43         localStorage.setItem(USER_UUID_KEY, "uuid");
44         localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
45
46         store.dispatch(initAuth());
47
48         expect(store.getState().auth).toEqual({
49             apiToken: "token",
50             user: {
51                 email: "test@test.com",
52                 firstName: "John",
53                 lastName: "Doe",
54                 uuid: "uuid",
55                 ownerUuid: "ownerUuid"
56             }
57         });
58     });
59
60     // TODO: Add remaining action tests
61     /*
62     it('should fire external url to login', () => {
63         const initialState = undefined;
64         window.location.assign = jest.fn();
65         reducer(initialState, authActions.LOGIN());
66         expect(window.location.assign).toBeCalledWith(
67             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
68         );
69     });
70
71     it('should fire external url to logout', () => {
72         const initialState = undefined;
73         window.location.assign = jest.fn();
74         reducer(initialState, authActions.LOGOUT());
75         expect(window.location.assign).toBeCalledWith(
76             `/logout?return_to=${location.protocol}//${location.host}`
77         );
78     });
79     */
80 });
81
82