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