Copyright (c) 2023 Tudor Stanciu
parent
6a9f3d7e27
commit
21db221f8a
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import React, { useReducer, useMemo, ReactNode } from "react";
|
||||
import { TuitioContext, TuitioDispatchContext } from "./contexts";
|
||||
import { initialState } from "./initialState";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import React from "react";
|
||||
import { initialState, initialDispatchActions } from "./initialState";
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
export * from "./useTuitioClient";
|
||||
export * from "./useTuitioToken";
|
||||
export * from "./useTuitioUser";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { useContext } from "react";
|
||||
import { TuitioContext, TuitioDispatchContext } from "../contexts";
|
||||
import { TuitioClient } from "@flare/tuitio-client";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { useContext } from "react";
|
||||
import { TuitioContext } from "../contexts";
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { useContext } from "react";
|
||||
import { TuitioContext } from "../contexts";
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import TuitioProvider from "./TuitioProvider";
|
||||
|
||||
export * from "./hooks";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { fetch } from "@flare/tuitio-client";
|
||||
import type { TuitioReactState, TuitioDispatchActions } from "./types";
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { initialState } from "./initialState";
|
||||
import type { TuitioReactState, TuitioDispatchActions } from "./types";
|
||||
import { Dispatch } from "react";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
export type TuitioConfiguration = { tuitioUrl: string | null };
|
||||
export type TuitioReactState = {
|
||||
auth: {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import * as React from "react";
|
||||
import { render } from "@testing-library/react";
|
||||
import "jest-canvas-mock";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { renderHook, act } from "@testing-library/react-hooks";
|
||||
import { TuitioClient } from "@flare/tuitio-client";
|
||||
import { useTuitioClient } from "../../src/hooks";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import React from "react";
|
||||
import { renderHook } from "@testing-library/react-hooks";
|
||||
import { TuitioContext, TuitioDispatchContext } from "../../src/contexts";
|
||||
|
@ -62,14 +64,14 @@ describe("useTuitioClient: dispatchActions", () => {
|
|||
</TuitioContext.Provider>
|
||||
);
|
||||
|
||||
const optionMock = {
|
||||
const optionsMock = {
|
||||
onLoginSuccess: jest.fn(),
|
||||
onLoginFailed: jest.fn(),
|
||||
onLoginError: jest.fn()
|
||||
};
|
||||
|
||||
it("onLoginSuccess should be called when user logs in", async () => {
|
||||
const { result } = renderHook(() => useTuitioClient(optionMock), { wrapper });
|
||||
const { result } = renderHook(() => useTuitioClient(optionsMock), { wrapper });
|
||||
|
||||
const userName = "user";
|
||||
const password = "password";
|
||||
|
@ -79,7 +81,7 @@ describe("useTuitioClient: dispatchActions", () => {
|
|||
});
|
||||
|
||||
it("onLogoutSuccess should be called when user logs out", async () => {
|
||||
const { result } = renderHook(() => useTuitioClient(optionMock), { wrapper });
|
||||
const { result } = renderHook(() => useTuitioClient(optionsMock), { wrapper });
|
||||
await result.current.logout();
|
||||
expect(spyOnLogoutSuccess).toHaveBeenCalled();
|
||||
});
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import React from "react";
|
||||
import { renderHook } from "@testing-library/react-hooks";
|
||||
import { TuitioContext, TuitioDispatchContext } from "../../src/contexts";
|
||||
import { useTuitioClient } from "../../src/hooks";
|
||||
import axios from "axios";
|
||||
|
||||
jest.mock("axios");
|
||||
|
||||
describe("useTuitioClient: dispatchActions", () => {
|
||||
let spyOnLoginSuccess: (token: string, validUntil: Date, userName: string) => void;
|
||||
let spyOnLogoutSuccess: () => void;
|
||||
|
||||
beforeEach(() => {
|
||||
spyOnLoginSuccess = jest.fn();
|
||||
spyOnLogoutSuccess = jest.fn();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
(spyOnLoginSuccess as jest.Mock).mockReset();
|
||||
(spyOnLogoutSuccess as jest.Mock).mockReset();
|
||||
});
|
||||
|
||||
const tuitioContextState = {
|
||||
auth: {
|
||||
userName: null,
|
||||
token: null,
|
||||
validUntil: null
|
||||
},
|
||||
configuration: { tuitioUrl: "http://localhost:5063/" }
|
||||
};
|
||||
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<TuitioContext.Provider value={tuitioContextState}>
|
||||
<TuitioDispatchContext.Provider
|
||||
value={{ onLoginSuccess: spyOnLoginSuccess, onLogoutSuccess: spyOnLogoutSuccess }}
|
||||
>
|
||||
{children}
|
||||
</TuitioDispatchContext.Provider>
|
||||
</TuitioContext.Provider>
|
||||
);
|
||||
|
||||
const optionsMock = {
|
||||
onLogoutFailed: jest.fn()
|
||||
};
|
||||
|
||||
it("onLogoutFailed should be called if logout is called before login", async () => {
|
||||
(axios.request as jest.Mock).mockResolvedValue({
|
||||
data: {
|
||||
result: { userId: 0, userName: "tuitio.user", logoutDate: new Date() },
|
||||
error: null
|
||||
}
|
||||
});
|
||||
|
||||
const { result } = renderHook(() => useTuitioClient(optionsMock), { wrapper });
|
||||
const response = await result.current.logout();
|
||||
expect(response.result).toBeNull();
|
||||
expect(response.error).toBe("UNAUTHENTICATED");
|
||||
expect(optionsMock.onLogoutFailed).toHaveBeenCalledWith("UNAUTHENTICATED");
|
||||
});
|
||||
});
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import React from "react";
|
||||
import { renderHook } from "@testing-library/react-hooks";
|
||||
import { TuitioContext } from "../../src/contexts";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import React from "react";
|
||||
import { renderHook } from "@testing-library/react-hooks";
|
||||
import { TuitioContext } from "../../src/contexts";
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { initialDispatchActions } from "../src/initialState";
|
||||
|
||||
describe("Initial dispatch actions", () => {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// Copyright (c) 2023 Tudor Stanciu
|
||||
|
||||
import { reducer, dispatchActions } from "../src/reducer";
|
||||
import { TuitioReactState, TuitioDispatchActions } from "../src/types";
|
||||
import { initialState } from "../src/initialState";
|
||||
|
|
Loading…
Reference in New Issue