refs #master Fix test and hide details panel from ssh-keys panel
[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         };
34         const state = reducer(initialState, authActions.INIT({ user, token: "token" }));
35         expect(state).toEqual({
36             apiToken: "token",
37             user
38         });
39     });
40
41     it('should save api token', () => {
42         const initialState = undefined;
43
44         const state = reducer(initialState, authActions.SAVE_API_TOKEN("token"));
45         expect(state).toEqual({
46             apiToken: "token",
47             user: undefined,
48             sshKeys: []
49         });
50     });
51
52     it('should set user details on success fetch', () => {
53         const initialState = undefined;
54
55         const user = {
56             email: "test@test.com",
57             firstName: "John",
58             lastName: "Doe",
59             uuid: "uuid",
60             ownerUuid: "ownerUuid"
61         };
62
63         const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
64         expect(state).toEqual({
65             apiToken: undefined,
66             sshKeys: [],
67             user: {
68                 email: "test@test.com",
69                 firstName: "John",
70                 lastName: "Doe",
71                 uuid: "uuid",
72                 ownerUuid: "ownerUuid",
73             }
74         });
75     });
76 });