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';
22 describe('auth-actions', () => {
23 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
27 store = configureStore(createBrowserHistory(), createServices(mockConfig({})));
29 reducer = authReducer(createServices(mockConfig({})));
32 it('should initialise state with user and api token from local storage', () => {
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 localStorage.setItem(USER_UUID_KEY, "uuid");
39 localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
41 store.dispatch(initAuth());
43 expect(store.getState().auth).toEqual({
46 email: "test@test.com",
50 ownerUuid: "ownerUuid"
55 // TODO: Add remaining action tests
57 it('should fire external url to login', () => {
58 const initialState = undefined;
59 window.location.assign = jest.fn();
60 reducer(initialState, authActions.LOGIN());
61 expect(window.location.assign).toBeCalledWith(
62 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
66 it('should fire external url to logout', () => {
67 const initialState = undefined;
68 window.location.assign = jest.fn();
69 reducer(initialState, authActions.LOGOUT());
70 expect(window.location.assign).toBeCalledWith(
71 `/logout?return_to=${location.protocol}//${location.host}`