2023-03-29 18:17:30 +03:00
|
|
|
// Copyright (c) 2023 Tudor Stanciu
|
|
|
|
|
|
|
|
import React, { useReducer, useMemo } from "react";
|
|
|
|
import { renderHook } from "@testing-library/react-hooks";
|
|
|
|
import { TuitioContext, TuitioDispatchContext } from "../../src/contexts";
|
|
|
|
import { useTuitioUserInfo } from "../../src/hooks";
|
|
|
|
import { reducer, dispatchActions } from "../../src/reducer";
|
|
|
|
import { TuitioDispatchActions } from "../../src/types";
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
|
|
jest.mock("axios");
|
|
|
|
|
|
|
|
const userInfoMock = {
|
|
|
|
userId: 1,
|
|
|
|
userName: "tuitio.test",
|
|
|
|
firstName: "Tuitio",
|
|
|
|
lastName: "Test",
|
|
|
|
email: "tuitio.test@lab.com",
|
|
|
|
profilePictureUrl: "https://domain/cdn/resources/test.jpg",
|
|
|
|
securityStamp: "mock",
|
|
|
|
creationDate: "2023-03-08T11:46:31.453",
|
|
|
|
failedLoginAttempts: 0,
|
2023-04-03 19:14:35 +03:00
|
|
|
lastLoginDate: "2023-03-27T16:09:26.283",
|
|
|
|
userRoles: [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
code: "SYSTEM_ADMINISTRATOR",
|
|
|
|
name: "System administrator"
|
|
|
|
},
|
|
|
|
{ id: 2, code: "FULLSTACK_DEVELOPER", name: "Fullstack developer" }
|
|
|
|
],
|
|
|
|
userGroups: [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
code: "ADMINISTRATORS",
|
|
|
|
name: "Administrators"
|
|
|
|
}
|
|
|
|
]
|
2023-03-29 18:17:30 +03:00
|
|
|
};
|
|
|
|
|
2023-04-03 19:14:35 +03:00
|
|
|
describe("useTuitioUserInfo: dispatchActions", () => {
|
2023-03-29 18:17:30 +03:00
|
|
|
const tuitioContextState = {
|
|
|
|
auth: {
|
|
|
|
userName: null,
|
|
|
|
token: null,
|
|
|
|
validUntil: null
|
|
|
|
},
|
|
|
|
configuration: { tuitioUrl: "http://localhost:5063/" }
|
|
|
|
};
|
|
|
|
|
|
|
|
let dispatchedActions: TuitioDispatchActions;
|
|
|
|
|
|
|
|
const Wrapper = ({ children }: { children: React.ReactNode }) => {
|
|
|
|
const [state, dispatch] = useReducer(reducer, tuitioContextState);
|
|
|
|
dispatchedActions = useMemo(() => dispatchActions(dispatch), [dispatch]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TuitioContext.Provider value={state}>
|
|
|
|
<TuitioDispatchContext.Provider value={dispatchedActions}>{children}</TuitioDispatchContext.Provider>
|
|
|
|
</TuitioContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
it("useTuitioUserInfo should return the user info obtained from Tuitio's API", async () => {
|
|
|
|
(axios.request as jest.Mock).mockResolvedValue({
|
|
|
|
data: { ...userInfoMock }
|
|
|
|
});
|
|
|
|
|
|
|
|
const { result, waitForNextUpdate } = renderHook(useTuitioUserInfo, { wrapper: Wrapper });
|
|
|
|
expect(result.current.userInfo).toBeUndefined();
|
|
|
|
|
|
|
|
const onUserInfoLoadedSpy = jest.spyOn(dispatchedActions, "onUserInfoLoaded");
|
|
|
|
await waitForNextUpdate();
|
|
|
|
|
|
|
|
expect(onUserInfoLoadedSpy).toHaveBeenCalled();
|
|
|
|
expect(onUserInfoLoadedSpy).toHaveBeenCalledWith(userInfoMock);
|
|
|
|
|
2023-04-03 19:14:35 +03:00
|
|
|
const { userInfo, isSysAdmin, isDeveloper, isGuest } = result.current;
|
2023-03-29 18:17:30 +03:00
|
|
|
expect(userInfo).toBeDefined();
|
|
|
|
expect(userInfo?.userId).toBe(1);
|
|
|
|
expect(userInfo?.userName).toBe("tuitio.test");
|
2023-04-03 19:14:35 +03:00
|
|
|
expect(userInfo?.userRoles).toHaveLength(2);
|
|
|
|
expect(userInfo?.userGroups).toHaveLength(1);
|
|
|
|
expect(isSysAdmin).toBe(true);
|
|
|
|
expect(isDeveloper).toBe(true);
|
|
|
|
expect(isGuest).toBe(false);
|
2023-03-29 18:17:30 +03:00
|
|
|
});
|
|
|
|
});
|