1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { authReducer, AuthState } from "./auth-reducer";
6 import { AuthAction, initAuth } from "./auth-action";
14 } from "~/services/auth-service/auth-service";
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";
23 describe('auth-actions', () => {
24 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
26 const actions: ApiActions = {
27 progressFn: (id: string, working: boolean) => {},
28 errorFn: (id: string, message: string) => {}
32 store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
34 reducer = authReducer(createServices(mockConfig({}), actions));
37 it('should initialise state with user and api token from local storage', () => {
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");
46 store.dispatch(initAuth());
48 expect(store.getState().auth).toEqual({
52 email: "test@test.com",
56 ownerUuid: "ownerUuid"
61 // TODO: Add remaining action tests
63 it('should fire external url to login', () => {
64 const initialState = undefined;
65 window.location.assign = jest.fn();
66 reducer(initialState, authActions.LOGIN());
67 expect(window.location.assign).toBeCalledWith(
68 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
72 it('should fire external url to logout', () => {
73 const initialState = undefined;
74 window.location.assign = jest.fn();
75 reducer(initialState, authActions.LOGOUT());
76 expect(window.location.assign).toBeCalledWith(
77 `/logout?return_to=${location.protocol}//${location.host}`