diff --git a/src/hooks/useTuitioToken.ts b/src/hooks/useTuitioToken.ts index 3987241..2bb25e5 100644 --- a/src/hooks/useTuitioToken.ts +++ b/src/hooks/useTuitioToken.ts @@ -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; } diff --git a/src/hooks/useTuitioUser.ts b/src/hooks/useTuitioUser.ts index 863a143..3aa9093 100644 --- a/src/hooks/useTuitioUser.ts +++ b/src/hooks/useTuitioUser.ts @@ -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 }; }; diff --git a/src/initialState.ts b/src/initialState.ts index 2d4f151..f133749 100644 --- a/src/initialState.ts +++ b/src/initialState.ts @@ -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 } }; diff --git a/src/reducer.ts b/src/reducer.ts index c6e3dfe..344d794 100644 --- a/src/reducer.ts +++ b/src/reducer.ts @@ -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": { diff --git a/src/types.ts b/src/types.ts index c6a72c7..dac2b18 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 = { diff --git a/tests/hooks/useTuitioClient.test.tsx b/tests/hooks/useTuitioClient.test.tsx index dcf1ce5..871a3c9 100644 --- a/tests/hooks/useTuitioClient.test.tsx +++ b/tests/hooks/useTuitioClient.test.tsx @@ -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 } }; diff --git a/tests/hooks/useTuitioToken.test.tsx b/tests/hooks/useTuitioToken.test.tsx index bec4df2..58ae9a5 100644 --- a/tests/hooks/useTuitioToken.test.tsx +++ b/tests/hooks/useTuitioToken.test.tsx @@ -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 }) => ( {children} @@ -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 } diff --git a/tests/hooks/useTuitioUser.test.tsx b/tests/hooks/useTuitioUser.test.tsx index f3bd643..fac03d2 100644 --- a/tests/hooks/useTuitioUser.test.tsx +++ b/tests/hooks/useTuitioUser.test.tsx @@ -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 } diff --git a/tests/reducer.test.ts b/tests/reducer.test.ts index c11c2f1..f139cac 100644 --- a/tests/reducer.test.ts +++ b/tests/reducer.test.ts @@ -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); });