added auth object in state

master
Tudor Stanciu 2023-03-18 02:29:06 +02:00
parent 8e200a1d14
commit cc1dd6a4c2
9 changed files with 28 additions and 27 deletions

View File

@ -3,10 +3,10 @@ import { TuitioContext } from "../contexts";
const useTuitioToken = () => {
const state = useContext(TuitioContext);
const token = state.token;
const token = state.auth.token;
const validate = (): boolean => {
const validUntil = state.validUntil;
const validUntil = state.auth.validUntil;
if (!validUntil) {
return false;
}

View File

@ -5,7 +5,7 @@ import { TuitioContext } from "../contexts";
const useTuitioUser = () => {
const state = useContext(TuitioContext);
const userName = state.userName;
const userName = state.auth.userName;
return { userName };
};

View File

@ -4,10 +4,11 @@ import type { TuitioReactState, TuitioDispatchActions } from "./types";
const { token, validUntil, userName } = fetch();
export const initialState: TuitioReactState = {
// pune intr-un obiect auth
userName,
token,
validUntil,
auth: {
userName,
token,
validUntil
},
configuration: { tuitioUrl: null }
};

View File

@ -8,9 +8,11 @@ export const reducer = (state: TuitioReactState = initialState, action: any): Tu
const { token, validUntil, userName } = action;
return {
...state,
token,
validUntil,
userName
auth: {
token,
validUntil,
userName
}
};
}
case "onLogoutSuccess": {

View File

@ -1,8 +1,10 @@
export type TuitioConfiguration = { tuitioUrl: string | null };
export type TuitioReactState = {
userName: string | null;
token: string | null;
validUntil: Date | null;
auth: {
userName: string | null;
token: string | null;
validUntil: Date | null;
};
configuration: TuitioConfiguration;
};
export type TuitioDispatchActions = {

View File

@ -40,9 +40,11 @@ describe("useTuitioClient: dispatchActions", () => {
});
const tuitioContextState = {
userName: "",
token: "mocked-token",
validUntil: new Date(),
auth: {
userName: "",
token: "mocked-token",
validUntil: new Date()
},
configuration: { tuitioUrl: null }
};

View File

@ -5,15 +5,13 @@ import { useTuitioToken } from "../../src/hooks";
describe("useTuitioToken: positive flow", () => {
const testState = {
userName: "test-user",
token: "mocked-token",
validUntil: new Date(),
auth: { userName: "test-user", token: "mocked-token", validUntil: new Date() },
configuration: {
tuitioUrl: null
}
};
testState.validUntil.setHours(testState.validUntil.getHours() + 1);
testState.auth.validUntil.setHours(testState.auth.validUntil.getHours() + 1);
const wrapper = ({ children }: { children?: React.ReactNode }) => (
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
@ -38,9 +36,7 @@ describe("useTuitioToken: positive flow", () => {
describe("useTuitioToken: negative flow", () => {
const testState = {
userName: "test-user",
token: null,
validUntil: null,
auth: { userName: "test-user", token: null, validUntil: null },
configuration: {
tuitioUrl: null
}

View File

@ -6,9 +6,7 @@ import { useTuitioUser } from "../../src/hooks";
describe("useTuitioUser: positive flow", () => {
it("should return the userName from the TuitioContext", () => {
const testState = {
userName: "test-user",
token: "mocked-token",
validUntil: new Date(2023, 12, 31),
auth: { userName: "test-user", token: "mocked-token", validUntil: new Date(2023, 12, 31) },
configuration: {
tuitioUrl: null
}

View File

@ -15,7 +15,7 @@ describe("reducer", () => {
it('should handle the "onLoginSuccess" action', () => {
const result = reducer(initialState, { type: "onLoginSuccess", token, validUntil, userName });
const expected = { ...initialState, token, validUntil, userName };
const expected = { ...initialState, auth: { token, validUntil, userName } };
expect(result).toEqual(expected);
});