Merge branch 'master' into 14452-my-account
[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         });
43     });
44
45     it('should save api token', () => {
46         const initialState = undefined;
47
48         const state = reducer(initialState, authActions.SAVE_API_TOKEN("token"));
49         expect(state).toEqual({
50             apiToken: "token",
51             user: undefined,
52             sshKeys: []
53         });
54     });
55
56     it('should set user details on success fetch', () => {
57         const initialState = undefined;
58
59         const user = {
60             email: "test@test.com",
61             firstName: "John",
62             lastName: "Doe",
63             uuid: "uuid",
64             ownerUuid: "ownerUuid",
65             identityUrl: "identityUrl",
66             prefs: {},
67             isAdmin: false
68         };
69
70         const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user));
71         expect(state).toEqual({
72             apiToken: undefined,
73             sshKeys: [],
74             user: {
75                 email: "test@test.com",
76                 firstName: "John",
77                 lastName: "Doe",
78                 uuid: "uuid",
79                 ownerUuid: "ownerUuid",
80                 isAdmin: false
81             }
82         });
83     });
84 });