2023-02-12 00:43:08 +02:00
|
|
|
import { renderHook, act } from "@testing-library/react-hooks";
|
2023-03-18 02:24:30 +02:00
|
|
|
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
|
|
|
|
2023-03-18 02:24:30 +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(() => ({
|
2023-03-18 02:24:30 +02:00
|
|
|
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
|
|
|
},
|
2023-03-18 02:24:30 +02:00
|
|
|
error: null
|
2023-02-12 00:43:08 +02:00
|
|
|
})
|
|
|
|
})),
|
|
|
|
fetch: jest.fn().mockResolvedValue({
|
2023-03-18 02:24:30 +02:00
|
|
|
token,
|
|
|
|
validUntil,
|
|
|
|
userName
|
2023-02-12 00:43:08 +02:00
|
|
|
})
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("useTuitioClient", () => {
|
2023-03-18 02:24:30 +02:00
|
|
|
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();
|
2023-03-18 02:24:30 +02:00
|
|
|
const onLogoutSuccess = jest.fn();
|
2023-02-12 00:43:08 +02:00
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useTuitioClient({
|
|
|
|
onLoginSuccess,
|
|
|
|
onLoginFailed: jest.fn(),
|
2023-03-18 02:24:30 +02:00
|
|
|
onLoginError: jest.fn(),
|
|
|
|
onLogoutSuccess
|
2023-02-12 00:43:08 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
await act(async () => {
|
2023-03-18 02:24:30 +02:00
|
|
|
await result.current.login(userName, password);
|
|
|
|
await result.current.logout();
|
2023-02-12 00:43:08 +02:00
|
|
|
});
|
|
|
|
|
2023-03-18 02:24:30 +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 () => {
|
2023-03-18 02:24:30 +02:00
|
|
|
const error = "error";
|
|
|
|
|
2023-02-12 00:43:08 +02:00
|
|
|
const onLoginFailed = jest.fn();
|
|
|
|
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
|
2023-03-18 02:24:30 +02:00
|
|
|
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 () => {
|
2023-03-18 02:24:30 +02:00
|
|
|
await result.current.login(userName, password);
|
2023-02-12 00:43:08 +02:00
|
|
|
});
|
|
|
|
|
2023-03-18 02:24:30 +02:00
|
|
|
expect(onLoginFailed).toHaveBeenCalledWith(error);
|
2023-02-12 00:43:08 +02:00
|
|
|
});
|
2023-02-12 01:06:58 +02:00
|
|
|
|
2023-02-12 01:13:26 +02:00
|
|
|
it("should call onLoginError with error if authentication fails", async () => {
|
|
|
|
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
|
2023-03-18 02:24:30 +02:00
|
|
|
login: jest.fn().mockRejectedValue(new Error("Login failed"))
|
2023-02-12 01:13:26 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
const onLoginError = jest.fn();
|
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useTuitioClient({ onLoginSuccess: jest.fn(), onLoginFailed: jest.fn(), onLoginError })
|
|
|
|
);
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
try {
|
2023-03-18 02:24:30 +02:00
|
|
|
await result.current.login(userName, password);
|
2023-02-12 01:13:26 +02:00
|
|
|
} catch (error) {
|
|
|
|
expect(onLoginError).toHaveBeenCalledWith(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-18 02:24:30 +02:00
|
|
|
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 }));
|
2023-02-12 01:06:58 +02:00
|
|
|
|
2023-03-18 02:24:30 +02:00
|
|
|
await act(async () => {
|
|
|
|
try {
|
|
|
|
await result.current.logout();
|
|
|
|
} catch (error) {
|
|
|
|
expect(onLogoutError).toHaveBeenCalledWith(error);
|
|
|
|
}
|
2023-02-12 01:06:58 +02:00
|
|
|
});
|
2023-03-18 02:24:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw exception if bots 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
|
|
|
|
})
|
|
|
|
}));
|
2023-02-12 01:06:58 +02:00
|
|
|
|
2023-03-18 02:24:30 +02:00
|
|
|
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 01:06:58 +02:00
|
|
|
});
|
2023-02-12 00:43:08 +02:00
|
|
|
});
|