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 useTuitioToken = () => {
const state = useContext(TuitioContext); const state = useContext(TuitioContext);
const token = state.token; const token = state.auth.token;
const validate = (): boolean => { const validate = (): boolean => {
const validUntil = state.validUntil; const validUntil = state.auth.validUntil;
if (!validUntil) { if (!validUntil) {
return false; return false;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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