tuitio-client-react/tests/hooks/useTuitioClient.test.ts

174 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-03-18 15:54:29 +02:00
// Copyright (c) 2023 Tudor Stanciu
2023-02-12 00:43:08 +02:00
import { renderHook, act } from "@testing-library/react-hooks";
import { TuitioClient } from "@flare/tuitio-client";
2023-02-12 01:04:19 +02:00
import { useTuitioClient } from "../../src/hooks";
2023-02-12 00:43:08 +02:00
const userName = "tuitio.user";
const password = "password";
const token = "mocked-token";
const expiresIn = 600000;
const validUntil = new Date();
2023-02-12 00:43:08 +02:00
jest.mock("@flare/tuitio-client", () => ({
TuitioClient: jest.fn().mockImplementation(() => ({
login: jest.fn().mockResolvedValue({
result: {
token,
expiresIn,
validUntil
},
error: null
}),
logout: jest.fn().mockResolvedValue({
result: {
userId: 0,
userName,
logoutDate: new Date()
2023-02-12 00:43:08 +02:00
},
error: null
2023-02-12 00:43:08 +02:00
})
})),
fetch: jest.fn().mockResolvedValue({
token,
validUntil,
userName
2023-02-12 00:43:08 +02:00
})
}));
describe("useTuitioClient", () => {
it("should call onLoginSuccess when login is successful and should call onLogoutSuccess when logout is called", async () => {
2023-02-12 00:43:08 +02:00
const onLoginSuccess = jest.fn();
const onLogoutSuccess = jest.fn();
2023-02-12 00:43:08 +02:00
const { result } = renderHook(() =>
useTuitioClient({
onLoginSuccess,
onLoginFailed: jest.fn(),
onLoginError: jest.fn(),
onLogoutSuccess
2023-02-12 00:43:08 +02:00
})
);
await act(async () => {
await result.current.login(userName, password);
await result.current.logout();
2023-02-12 00:43:08 +02:00
});
expect(onLoginSuccess).toHaveBeenCalledWith(userName, token, expiresIn, validUntil);
expect(onLogoutSuccess).toHaveBeenCalled();
2023-02-12 00:43:08 +02:00
});
it("should call onLoginFailed when login is unsuccessful", async () => {
const error = "error";
2023-02-12 00:43:08 +02:00
const onLoginFailed = jest.fn();
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
login: jest.fn().mockResolvedValue({
result: null,
error
2023-02-12 00:43:08 +02:00
})
}));
const { result } = renderHook(() =>
useTuitioClient({
onLoginSuccess: jest.fn(),
onLoginFailed,
onLoginError: jest.fn()
})
);
await act(async () => {
await result.current.login(userName, password);
2023-02-12 00:43:08 +02:00
});
expect(onLoginFailed).toHaveBeenCalledWith(error);
2023-02-12 00:43:08 +02:00
});
it("should call onLoginError with error if authentication fails", async () => {
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
login: jest.fn().mockRejectedValue(new Error("Login failed"))
}));
const onLoginError = jest.fn();
const { result } = renderHook(() =>
useTuitioClient({ onLoginSuccess: jest.fn(), onLoginFailed: jest.fn(), onLoginError })
);
await act(async () => {
try {
await result.current.login(userName, password);
} catch (error) {
expect(onLoginError).toHaveBeenCalledWith(error);
}
});
});
it("should return error if logout returns error", async () => {
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
logout: jest.fn().mockResolvedValue({
result: null,
error: "error"
})
}));
const onLogoutFailed = jest.fn();
const { result } = renderHook(() =>
useTuitioClient({
onLogoutFailed
})
);
const response = await result.current.logout();
expect(response.result).toBeNull();
expect(response.error).toBe("error");
expect(onLogoutFailed).toHaveBeenCalledWith(response.error);
});
it("should call onLogoutError with error if logout fails", async () => {
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
logout: jest.fn().mockRejectedValue(new Error("Login failed"))
}));
const onLogoutError = jest.fn();
const { result } = renderHook(() => useTuitioClient({ onLogoutError }));
await act(async () => {
try {
await result.current.logout();
} catch (error) {
expect(onLogoutError).toHaveBeenCalledWith(error);
}
});
});
2023-03-18 14:49:01 +02:00
it("should throw exception if both result and error are null", async () => {
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
login: jest.fn().mockResolvedValue({
result: null,
error: null
}),
logout: jest.fn().mockResolvedValue({
result: null,
error: null
})
}));
const { result } = renderHook(() => useTuitioClient());
await act(async () => {
try {
await result.current.login(userName, password);
} catch (error) {
expect(error).toBeDefined();
}
try {
await result.current.logout();
} catch (error) {
expect(error).toBeDefined();
}
});
});
2023-02-12 00:43:08 +02:00
});