Fix passing auth reducer tests
[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, AuthState } from "./auth-reducer";
6 import { AuthAction, authActions } from "./auth-action";
7
8 import 'jest-localstorage-mock';
9 import { createServices } from "~/services/services";
10 import { mockConfig } from '~/common/config';
11 import { ApiActions } from "~/services/api/api-actions";
12
13 describe('auth-reducer', () => {
14     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
15     const actions: ApiActions = {
16         progressFn: (id: string, working: boolean) => {},
17         errorFn: (id: string, message: string) => {}
18     };
19
20     beforeAll(() => {
21         localStorage.clear();
22         reducer = authReducer(createServices(mockConfig({}), actions));
23     });
24
25     it('should correctly initialise state', () => {
26         const initialState = undefined;
27         const user = {
28             email: "test@test.com",
29             firstName: "John",
30             lastName: "Doe",
31             uuid: "uuid",
32             ownerUuid: "ownerUuid",
33             identityUrl: "identityUrl",
34             prefs: {},
35             isAdmin: false
36         };
37         const state = reducer(initialState, authActions.INIT({ user, token: "token" }));
38         expect(state).toEqual({
39             apiToken: "token",
40             user,
41             sshKeys: [],
42             sessions: []
43         });
44     });
45
46     it('should save api token', () => {
47         const initialState = undefined;
48
49         const state = reducer(initialState, authActions.SAVE_API_TOKEN("token"));
50         expect(state).toEqual({
51             apiToken: "token",
52             user: undefined,
53             sshKeys: [],
54             sessions: []
55         });
56     });
57
58     it('should set user details on success fetch', () => {
59         const initialState = undefined;
60
61         const user = {
62             email: "test@test.com",
63             firstName: "John",
64             lastName: "Doe",
65             uuid: "uuid",
66             ownerUuid: "ownerUuid",
67             identityUrl: "identityUrl",
68             prefs: {},
69             isAdmin: false
70         };
71
72         const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
73         expect(state).toEqual({
74             apiToken: undefined,
75             sshKeys: [],
76             sessions: [],
77             user: {
78                 email: "test@test.com",
79                 firstName: "John",
80                 lastName: "Doe",
81                 uuid: "uuid",
82                 ownerUuid: "ownerUuid",
83                 identityUrl: "identityUrl",
84                 prefs: {},
85                 isAdmin: false
86             }
87         });
88     });
89 });