Skip to content

Commit

Permalink
Add more tests for useApp
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Nov 1, 2024
1 parent 6274100 commit 267cc8d
Showing 1 changed file with 144 additions and 9 deletions.
153 changes: 144 additions & 9 deletions src/hooks/app/useApp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("useApp", () => {
id: "1",
isLoggedIn: vi.fn().mockResolvedValue(true),
reset: vi.fn(),
login: vi.fn(() => console.log("7777")),
login: vi.fn(),
logout: vi.fn(),
}),
getCertificatesByProviderId: vi.fn().mockResolvedValue(certificatesMock),
Expand Down Expand Up @@ -133,6 +133,22 @@ describe("useApp", () => {
expect(result.current.fetching.connectionApprove).toEqual("resolved");
});

it("Should handle connectionApprove is rejected", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() =>
({
...mockFortifyAPIInstance,
challenge: vi.fn().mockRejectedValue(new Error("Error")),
}) as unknown as FortifyAPI
);
const { result } = renderHook(() => useApp());

await waitFor(() => {
expect(result.current.fetching.connectionApprove).toEqual("rejected");
});
expect(result.current.certificates).toEqual([]);
});

it("Should handle provider change", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
Expand All @@ -156,10 +172,6 @@ describe("useApp", () => {
);
const { result } = renderHook(() => useApp());

await waitFor(() => {
expect(result.current.currentProvider).toEqual(providersMock[0]);
});

await act(async () => {
await result.current.handleCertificatesDataReload(providersMock[0].id);
});
Expand All @@ -173,10 +185,6 @@ describe("useApp", () => {
);
const { result } = renderHook(() => useApp());

await waitFor(() => {
expect(result.current.currentProvider).toEqual(providersMock[0]);
});

await act(async () => {
await result.current.handleProviderResetAndRefreshList();
});
Expand Down Expand Up @@ -233,4 +241,131 @@ describe("useApp", () => {
).toHaveBeenCalled();
expect(result.current.isCurrentProviderLogedin).toEqual(true);
});

it("Should set login status to false when error occured during login", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() =>
({
...mockFortifyAPIInstance,
getProviderById: vi.fn().mockResolvedValue(new Error("Error")),
}) as unknown as FortifyAPI
);
const { result } = renderHook(() => useApp());

await waitFor(() => {
expect(result.current.currentProvider).toEqual(providersMock[0]);
});

await act(async () => {
await result.current.handleProviderLoginLogout(false);
});

expect(result.current.isCurrentProviderLogedin).toEqual(false);
});

it("Should show error message if provider doesn't support signing in", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() =>
({
...mockFortifyAPIInstance,
getProviderById: vi.fn().mockResolvedValue({
id: "1",
isLoggedIn: vi.fn().mockResolvedValue(true),
logout: vi.fn(),
}),
}) as unknown as FortifyAPI
);
const { result } = renderHook(() => useApp());

await waitFor(() => {
expect(result.current.currentProvider).toEqual(providersMock[0]);
});

await act(async () => {
await result.current.handleProviderLoginLogout(true);
});

expect(addToastMock).toHaveBeenCalledWith(
expect.objectContaining({
message: "This provider doesn’t support signing in.",
variant: "attention",
})
);
addToastMock.mockClear();
});

it("Should return early if no fortifyClient is present", async () => {
const { result } = renderHook(() => useApp());

result.current.fortifyClient = null;

waitFor(async () => {
expect(
await result.current.handleCertificatesDataReload("1")
).toReturnWith(undefined);
});

waitFor(async () => {
expect(await result.current.handleProviderLoginLogout(true)).toReturnWith(
undefined
);
});

waitFor(async () => {
expect(
await result.current.handleProviderResetAndRefreshList()
).toReturnWith(undefined);
});

waitFor(async () => {
expect(await result.current.handleProviderChange("1")).toReturnWith(
undefined
);
});
});

it("Should reload app on handleRetryConection", () => {
const originalLocation = global.location;

global.location = {
...originalLocation,
reload: vi.fn(),
};

const { result } = renderHook(() => useApp());

act(() => {
result.current.handleRetryConection();
});

expect(global.location.reload).toHaveBeenCalled();

global.location = originalLocation;
});

it("Should clear certificates when error occured during first load & reload", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() =>
({
...mockFortifyAPIInstance,
getCertificatesByProviderId: vi
.fn()
.mockRejectedValue(new Error("Error")),
}) as unknown as FortifyAPI
);

const { result } = renderHook(() => useApp());

await waitFor(() => {
expect(result.current.currentProvider).toEqual(providersMock[0]);
});

expect(result.current.certificates).toEqual([]);

await act(async () => {
await result.current.handleCertificatesDataReload(providersMock[0].id);
});

expect(result.current.certificates).toEqual([]);
});
});

0 comments on commit 267cc8d

Please sign in to comment.