tuitio-client-react/tests/hooks/useTuitioToken.test.tsx

76 lines
2.3 KiB
TypeScript

import React from "react";
import { renderHook } from "@testing-library/react-hooks";
import { TuitioContext } from "../../src/contexts";
import { useTuitioToken } from "../../src/hooks";
describe("useTuitioToken: positive flow", () => {
const testState = {
userName: "test-user",
token: {
raw: "mocked-token",
validFrom: new Date(),
validUntil: new Date()
},
configuration: {
tuitioUrl: null
}
};
testState.token.validFrom.setHours(testState.token.validFrom.getHours() - 1);
testState.token.validUntil.setHours(testState.token.validUntil.getHours() + 1);
const wrapper = ({ children }: { children?: React.ReactNode }) => (
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
);
it("should return the correct token object", () => {
const { result } = renderHook(() => useTuitioToken(), { wrapper });
expect(result.current.token).toEqual({
raw: "mocked-token",
validFrom: expect.any(Date),
validUntil: expect.any(Date)
});
});
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);
});
});
describe("useTuitioToken: negative flow", () => {
const testState = {
userName: "test-user",
token: null,
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);
});
});