Skip to content

Commit

Permalink
Add error test cases for auth hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianpd committed Jul 5, 2023
1 parent 2ce0485 commit 870366d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 21 deletions.
25 changes: 24 additions & 1 deletion packages/react/spec/auth/useIsSignedIn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderHook } from "@testing-library/react";
import { superAuthApi } from "../../spec/apis";
import { expectMockSignedInUser, expectMockSignedOutUser } from "../../spec/utils";
import { expectMockSignedInUser, expectMockSignedOutUser, mockInternalServerError, mockNetworkError } from "../../spec/utils";
import { useIsSignedIn } from "../../src/auth/useIsSignedIn";
import { TestWrapper } from "../testWrapper";

Expand All @@ -22,4 +22,27 @@ describe("useIsSignedIn", () => {
rerender();
expect(result.current).toBe(false);
});

test("it throws when the server responds with an error", async () => {
expect(() => {
const { rerender } = renderHook(() => useIsSignedIn(), { wrapper: TestWrapper(superAuthApi) });

mockInternalServerError();

rerender();
}).toThrowErrorMatchingInlineSnapshot(`
"[GraphQL] GGT_INTERNAL_SERVET_ERROR
[GraphQL] An error occurred"
`);
});

test("it throws when request fails to complete", async () => {
expect(() => {
const { rerender } = renderHook(() => useIsSignedIn(), { wrapper: TestWrapper(superAuthApi) });

mockNetworkError();

rerender();
}).toThrowErrorMatchingInlineSnapshot(`"[Network] Network error"`);
});
});
25 changes: 24 additions & 1 deletion packages/react/spec/auth/useSession.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderHook } from "@testing-library/react";
import { superAuthApi } from "../../spec/apis";
import { expectMockSignedInUser, expectMockSignedOutUser } from "../../spec/utils";
import { expectMockSignedInUser, expectMockSignedOutUser, mockInternalServerError, mockNetworkError } from "../../spec/utils";
import { useSession } from "../../src/auth/useSession";
import { TestWrapper } from "../testWrapper";

Expand Down Expand Up @@ -29,4 +29,27 @@ describe("useSession", () => {
expect(result.current!.userId).toBe(null);
expect(result.current!.user).toBe(null);
});

test("it throws when the server responds with an error", async () => {
expect(() => {
const { rerender } = renderHook(() => useSession(), { wrapper: TestWrapper(superAuthApi) });

mockInternalServerError();

rerender();
}).toThrowErrorMatchingInlineSnapshot(`
"[GraphQL] GGT_INTERNAL_SERVET_ERROR
[GraphQL] An error occurred"
`);
});

test("it throws when request fails to complete", async () => {
expect(() => {
const { rerender } = renderHook(() => useSession(), { wrapper: TestWrapper(superAuthApi) });

mockNetworkError();

rerender();
}).toThrowErrorMatchingInlineSnapshot(`"[Network] Network error"`);
});
});
39 changes: 21 additions & 18 deletions packages/react/spec/auth/useUser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { renderHook } from "@testing-library/react";
import { superAuthApi } from "../../spec/apis";
import { expectMockSignedInUser, expectMockSignedOutUser } from "../../spec/utils";
import { expectMockSignedInUser, expectMockSignedOutUser, mockInternalServerError, mockNetworkError } from "../../spec/utils";
import { useUser } from "../../src/auth/useUser";
import { TestWrapper, mockUrqlClient } from "../testWrapper";
import { TestWrapper } from "../testWrapper";

describe("useUser", () => {
test("it returns the current user when the user is logged in", async () => {
Expand All @@ -25,23 +25,26 @@ describe("useUser", () => {
expect(result.current).toBe(null);
});

test("it does something when the user fails to fetch", async () => {
const { result, rerender } = renderHook(() => useUser(), { wrapper: TestWrapper(superAuthApi) });
test("it throws when the server responds with an error", async () => {
expect(() => {
const { rerender } = renderHook(() => useUser(), { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: null,
user: null,
},
},
stale: false,
hasNext: false,
});
mockInternalServerError();

rerender();
expect(result.current).toBe(null);
rerender();
}).toThrowErrorMatchingInlineSnapshot(`
"[GraphQL] GGT_INTERNAL_SERVET_ERROR
[GraphQL] An error occurred"
`);
});

test("it throws when request fails to complete", async () => {
expect(() => {
const { rerender } = renderHook(() => useUser(), { wrapper: TestWrapper(superAuthApi) });

mockNetworkError();

rerender();
}).toThrowErrorMatchingInlineSnapshot(`"[Network] Network error"`);
});
});
21 changes: 21 additions & 0 deletions packages/react/spec/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CombinedError } from "urql";
import { mockUrqlClient } from "./testWrapper";

export const expectMockSignedInUser = () => {
Expand Down Expand Up @@ -33,3 +34,23 @@ export const expectMockSignedOutUser = () => {
hasNext: false,
});
};

export const mockInternalServerError = () => {
expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: null,
error: new CombinedError({ graphQLErrors: [new Error("GGT_INTERNAL_SERVET_ERROR"), "An error occurred"] }),
stale: false,
hasNext: false,
});
};

export const mockNetworkError = () => {
expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: null,
error: new CombinedError({ networkError: new Error("Network error") }),
stale: false,
hasNext: false,
});
};
3 changes: 2 additions & 1 deletion packages/react/src/auth/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const useGetSessionAndUser = () => {
* @returns The current session
*/
export const useSession = (): GadgetSession | undefined => {
const [{ data: session }] = useGetSessionAndUser();
const [{ data: session, error }] = useGetSessionAndUser();
if (error) throw error;
return session;
};

0 comments on commit 870366d

Please sign in to comment.