Add progress indicator for services
[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
22 describe('auth-actions', () => {
23     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
24     let store: RootStore;
25     const progressFn = (id: string, working: boolean) => {};
26
27     beforeEach(() => {
28         store = configureStore(createBrowserHistory(), createServices(mockConfig({}), progressFn));
29         localStorage.clear();
30         reducer = authReducer(createServices(mockConfig({}), progressFn));
31     });
32
33     it('should initialise state with user and api token from local storage', () => {
34
35         localStorage.setItem(API_TOKEN_KEY, "token");
36         localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
37         localStorage.setItem(USER_FIRST_NAME_KEY, "John");
38         localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
39         localStorage.setItem(USER_UUID_KEY, "uuid");
40         localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
41
42         store.dispatch(initAuth());
43
44         expect(store.getState().auth).toEqual({
45             apiToken: "token",
46             user: {
47                 email: "test@test.com",
48                 firstName: "John",
49                 lastName: "Doe",
50                 uuid: "uuid",
51                 ownerUuid: "ownerUuid"
52             }
53         });
54     });
55
56     // TODO: Add remaining action tests
57     /*
58     it('should fire external url to login', () => {
59         const initialState = undefined;
60         window.location.assign = jest.fn();
61         reducer(initialState, authActions.LOGIN());
62         expect(window.location.assign).toBeCalledWith(
63             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
64         );
65     });
66
67     it('should fire external url to logout', () => {
68         const initialState = undefined;
69         window.location.assign = jest.fn();
70         reducer(initialState, authActions.LOGOUT());
71         expect(window.location.assign).toBeCalledWith(
72             `/logout?return_to=${location.protocol}//${location.host}`
73         );
74     });
75     */
76 });
77
78