Copyright (c) 2023 Tudor Stanciu

master
Tudor Stanciu 2023-03-18 15:54:29 +02:00
parent 6a9f3d7e27
commit 21db221f8a
18 changed files with 99 additions and 3 deletions

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import React, { useReducer, useMemo, ReactNode } from "react"; import React, { useReducer, useMemo, ReactNode } from "react";
import { TuitioContext, TuitioDispatchContext } from "./contexts"; import { TuitioContext, TuitioDispatchContext } from "./contexts";
import { initialState } from "./initialState"; import { initialState } from "./initialState";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import React from "react"; import React from "react";
import { initialState, initialDispatchActions } from "./initialState"; import { initialState, initialDispatchActions } from "./initialState";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
export * from "./useTuitioClient"; export * from "./useTuitioClient";
export * from "./useTuitioToken"; export * from "./useTuitioToken";
export * from "./useTuitioUser"; export * from "./useTuitioUser";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { useContext } from "react"; import { useContext } from "react";
import { TuitioContext, TuitioDispatchContext } from "../contexts"; import { TuitioContext, TuitioDispatchContext } from "../contexts";
import { TuitioClient } from "@flare/tuitio-client"; import { TuitioClient } from "@flare/tuitio-client";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { useContext } from "react"; import { useContext } from "react";
import { TuitioContext } from "../contexts"; import { TuitioContext } from "../contexts";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { useContext } from "react"; import { useContext } from "react";
import { TuitioContext } from "../contexts"; import { TuitioContext } from "../contexts";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import TuitioProvider from "./TuitioProvider"; import TuitioProvider from "./TuitioProvider";
export * from "./hooks"; export * from "./hooks";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { fetch } from "@flare/tuitio-client"; import { fetch } from "@flare/tuitio-client";
import type { TuitioReactState, TuitioDispatchActions } from "./types"; import type { TuitioReactState, TuitioDispatchActions } from "./types";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { initialState } from "./initialState"; import { initialState } from "./initialState";
import type { TuitioReactState, TuitioDispatchActions } from "./types"; import type { TuitioReactState, TuitioDispatchActions } from "./types";
import { Dispatch } from "react"; import { Dispatch } from "react";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
export type TuitioConfiguration = { tuitioUrl: string | null }; export type TuitioConfiguration = { tuitioUrl: string | null };
export type TuitioReactState = { export type TuitioReactState = {
auth: { auth: {

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import * as React from "react"; import * as React from "react";
import { render } from "@testing-library/react"; import { render } from "@testing-library/react";
import "jest-canvas-mock"; import "jest-canvas-mock";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { renderHook, act } from "@testing-library/react-hooks"; import { renderHook, act } from "@testing-library/react-hooks";
import { TuitioClient } from "@flare/tuitio-client"; import { TuitioClient } from "@flare/tuitio-client";
import { useTuitioClient } from "../../src/hooks"; import { useTuitioClient } from "../../src/hooks";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import React from "react"; import React from "react";
import { renderHook } from "@testing-library/react-hooks"; import { renderHook } from "@testing-library/react-hooks";
import { TuitioContext, TuitioDispatchContext } from "../../src/contexts"; import { TuitioContext, TuitioDispatchContext } from "../../src/contexts";
@ -62,14 +64,14 @@ describe("useTuitioClient: dispatchActions", () => {
</TuitioContext.Provider> </TuitioContext.Provider>
); );
const optionMock = { const optionsMock = {
onLoginSuccess: jest.fn(), onLoginSuccess: jest.fn(),
onLoginFailed: jest.fn(), onLoginFailed: jest.fn(),
onLoginError: jest.fn() onLoginError: jest.fn()
}; };
it("onLoginSuccess should be called when user logs in", async () => { 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 userName = "user";
const password = "password"; const password = "password";
@ -79,7 +81,7 @@ describe("useTuitioClient: dispatchActions", () => {
}); });
it("onLogoutSuccess should be called when user logs out", async () => { 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(); await result.current.logout();
expect(spyOnLogoutSuccess).toHaveBeenCalled(); expect(spyOnLogoutSuccess).toHaveBeenCalled();
}); });

View File

@ -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");
});
});

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import React from "react"; import React from "react";
import { renderHook } from "@testing-library/react-hooks"; import { renderHook } from "@testing-library/react-hooks";
import { TuitioContext } from "../../src/contexts"; import { TuitioContext } from "../../src/contexts";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import React from "react"; import React from "react";
import { renderHook } from "@testing-library/react-hooks"; import { renderHook } from "@testing-library/react-hooks";
import { TuitioContext } from "../../src/contexts"; import { TuitioContext } from "../../src/contexts";

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { initialDispatchActions } from "../src/initialState"; import { initialDispatchActions } from "../src/initialState";
describe("Initial dispatch actions", () => { describe("Initial dispatch actions", () => {

View File

@ -1,3 +1,5 @@
// Copyright (c) 2023 Tudor Stanciu
import { reducer, dispatchActions } from "../src/reducer"; import { reducer, dispatchActions } from "../src/reducer";
import { TuitioReactState, TuitioDispatchActions } from "../src/types"; import { TuitioReactState, TuitioDispatchActions } from "../src/types";
import { initialState } from "../src/initialState"; import { initialState } from "../src/initialState";