2023-03-18 15:54:29 +02:00
|
|
|
// Copyright (c) 2023 Tudor Stanciu
|
|
|
|
|
2023-02-12 03:27:22 +02:00
|
|
|
import React from "react";
|
|
|
|
import { renderHook } from "@testing-library/react-hooks";
|
|
|
|
import { TuitioContext } from "../../src/contexts";
|
|
|
|
import { useTuitioToken } from "../../src/hooks";
|
|
|
|
|
2023-03-30 10:03:07 +03:00
|
|
|
const tokenMock =
|
|
|
|
"eyJUb2tlbklkIjoiZDkzZjEwMzQtNjI4ZS00MWQ4LWI5OGUtNGM3MDhiMmRlODgzIiwiVXNlcklkIjoxLCJVc2VyTmFtZSI6InR1aXRpby51c2VyIiwiRmlyc3ROYW1lIjoiVHVpdGlvIiwiTGFzdE5hbWUiOiJVc2VyIiwiRW1haWwiOiJ0dWl0aW8udXNlckBsYWIuY29tIiwiU2VjdXJpdHlTdGFtcCI6InRlc3Qtc2VjdXJpdHktc3RhbXAiLCJMb2NrU3RhbXAiOiJ0ZXN0LWxvY2stc3RhbXAiLCJDcmVhdGVkQXQiOiIyMDIwLTAxLTI5VDE1OjIyOjQ5LjYyMzk5MzFaIiwiRXhwaXJlc0luIjoxMDYyfQ==";
|
|
|
|
|
2023-02-12 03:27:22 +02:00
|
|
|
describe("useTuitioToken: positive flow", () => {
|
|
|
|
const testState = {
|
2023-03-30 10:03:07 +03:00
|
|
|
auth: { userName: "test-user", token: tokenMock, validUntil: new Date() },
|
2023-02-12 03:27:22 +02:00
|
|
|
configuration: {
|
|
|
|
tuitioUrl: null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-18 02:29:06 +02:00
|
|
|
testState.auth.validUntil.setHours(testState.auth.validUntil.getHours() + 1);
|
2023-02-12 03:27:22 +02:00
|
|
|
|
|
|
|
const wrapper = ({ children }: { children?: React.ReactNode }) => (
|
|
|
|
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
|
|
|
|
);
|
|
|
|
|
2023-03-18 02:24:30 +02:00
|
|
|
it("should return the correct token", () => {
|
2023-02-12 03:27:22 +02:00
|
|
|
const { result } = renderHook(() => useTuitioToken(), { wrapper });
|
2023-03-30 10:03:07 +03:00
|
|
|
expect(result.current.token).toEqual(tokenMock);
|
2023-02-12 03:27:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false value for valid", () => {
|
|
|
|
const { result } = renderHook(() => useTuitioToken(), { wrapper });
|
|
|
|
expect(result.current.valid).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("validate should return true value", () => {
|
|
|
|
const { result } = renderHook(() => useTuitioToken(), { wrapper });
|
|
|
|
const valid = result.current.validate();
|
|
|
|
expect(valid).toBe(true);
|
|
|
|
});
|
2023-03-30 10:03:07 +03:00
|
|
|
|
|
|
|
it("should successfully decode token", () => {
|
|
|
|
const { result } = renderHook(() => useTuitioToken(), { wrapper });
|
|
|
|
const { decodedToken } = result.current;
|
|
|
|
expect(decodedToken?.userId).toEqual(1);
|
|
|
|
expect(decodedToken?.userName).toBe("tuitio.user");
|
|
|
|
expect(decodedToken?.firstName).toBe("Tuitio");
|
|
|
|
expect(decodedToken?.securityStamp).toBeDefined();
|
|
|
|
expect(decodedToken?.lockStamp).toBeDefined();
|
|
|
|
expect(decodedToken?.createdAt).toBeDefined();
|
|
|
|
});
|
2023-02-12 03:27:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("useTuitioToken: negative flow", () => {
|
|
|
|
const testState = {
|
2023-03-18 02:29:06 +02:00
|
|
|
auth: { userName: "test-user", token: null, validUntil: null },
|
2023-02-12 03:27:22 +02:00
|
|
|
configuration: {
|
|
|
|
tuitioUrl: null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = ({ children }: { children?: React.ReactNode }) => (
|
|
|
|
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
it("should return null for token", () => {
|
|
|
|
const { result } = renderHook(() => useTuitioToken(), { wrapper });
|
|
|
|
expect(result.current.token).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return false value for valid", () => {
|
|
|
|
const { result } = renderHook(() => useTuitioToken(), { wrapper });
|
|
|
|
expect(result.current.valid).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("validate should return false value", () => {
|
|
|
|
const { result } = renderHook(() => useTuitioToken(), { wrapper });
|
|
|
|
const valid = result.current.validate();
|
|
|
|
expect(valid).toBe(false);
|
|
|
|
});
|
|
|
|
});
|