Skip to content

Commit

Permalink
Rough in tests for new hooks and react components
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianpd committed Jul 4, 2023
1 parent 1f9309b commit ae2c10c
Show file tree
Hide file tree
Showing 31 changed files with 510 additions and 149 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"devDependencies": {
"@gadget-client/bulk-actions-test": "^1.102.0",
"@gadget-client/related-products-example": "^1.848.0",
"@gadget-client/super-auth": "^1.13738.0",
"@gadgetinc/eslint-config": "^0.6.1",
"@gadgetinc/prettier-config": "^0.4.0",
"@swc/core": "^1.3.42",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"urql": "^4.0.4"
},
"devDependencies": {
"@gadget-client/super-auth": "^1.13139.0",
"@gadgetinc/api-client-core": "workspace:*",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@types/deep-equal": "^1.0.1",
"@types/jest": "^29.5.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/react/spec/apis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client as BulkClient } from "@gadget-client/bulk-actions-test";
import { Client } from "@gadget-client/related-products-example";
import { Client as ClientWithAuth } from "@gadget-client/super-auth";
import { Client as AuthClient } from "@gadget-client/super-auth";

export const relatedProductsApi = new Client();
export const superAuthApi = new ClientWithAuth();
export const bulkExampleApi = new BulkClient();
export const superAuthApi = new AuthClient();
11 changes: 6 additions & 5 deletions packages/react/spec/auth/useIsSignedIn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { renderHook } from "@testing-library/react";
import { TestWrapperWithAuth, mockUrqlClient } from "../testWrapper";
import { superAuthApi } from "../../spec/apis";
import { useIsSignedIn } from "../../src/auth/useIsSignedIn";
import { TestWrapper, mockUrqlClient } from "../testWrapper";

describe("useIsSignedIn", () => {
test("returns true if the user is signed in", async () => {
const { result, rerender } = renderHook(() => useIsSignedIn(), { wrapper: TestWrapperWithAuth });
const { result, rerender } = renderHook(() => useIsSignedIn(), { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
Expand All @@ -16,7 +17,7 @@ describe("useIsSignedIn", () => {
id: "321",
firstName: "Jane",
lastName: "Doe",
}
},
},
},
stale: false,
Expand All @@ -28,15 +29,15 @@ describe("useIsSignedIn", () => {
});

test("returns false if the user is signed out", async () => {
const { result, rerender } = renderHook(() => useIsSignedIn(), { wrapper: TestWrapperWithAuth });
const { result, rerender } = renderHook(() => useIsSignedIn(), { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: null,
user: null
user: null,
},
},
stale: false,
Expand Down
11 changes: 6 additions & 5 deletions packages/react/spec/auth/useSession.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { renderHook } from "@testing-library/react";
import { TestWrapperWithAuth, mockUrqlClient } from "../testWrapper";
import { superAuthApi } from "../../spec/apis";
import { useSession } from "../../src/auth/useSession";
import { TestWrapper, mockUrqlClient } from "../testWrapper";

describe("useSession", () => {
test("it returns the current session when the user is logged in", async () => {
const { result, rerender } = renderHook(() => useSession(), { wrapper: TestWrapperWithAuth });
const { result, rerender } = renderHook(() => useSession(), { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
Expand All @@ -16,7 +17,7 @@ describe("useSession", () => {
id: "321",
firstName: "Jane",
lastName: "Doe",
}
},
},
},
stale: false,
Expand All @@ -33,15 +34,15 @@ describe("useSession", () => {
});

test("it returns the current session when the user is logged out", async () => {
const { result } = renderHook(() => useSession(), { wrapper: TestWrapperWithAuth });
const { result } = renderHook(() => useSession(), { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: null,
user: null
user: null,
},
},
stale: false,
Expand Down
11 changes: 6 additions & 5 deletions packages/react/spec/auth/useUser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { renderHook } from "@testing-library/react";
import { TestWrapperWithAuth, mockUrqlClient } from "../testWrapper";
import { superAuthApi } from "../../spec/apis";
import { useUser } from "../../src/auth/useUser";
import { TestWrapper, mockUrqlClient } from "../testWrapper";

describe("useUser", () => {
test("it returns the current user when the user is logged in", async () => {
const { result, rerender } = renderHook(() => useUser(), { wrapper: TestWrapperWithAuth });
const { result, rerender } = renderHook(() => useUser(), { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
Expand All @@ -16,7 +17,7 @@ describe("useUser", () => {
id: "321",
firstName: "Jane",
lastName: "Doe",
}
},
},
},
stale: false,
Expand All @@ -30,15 +31,15 @@ describe("useUser", () => {
});

test("it returns null when the user is logged out", async () => {
const { result, rerender } = renderHook(() => useUser(), { wrapper: TestWrapperWithAuth });
const { result, rerender } = renderHook(() => useUser(), { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: null,
user: null
user: null,
},
},
stale: false,
Expand Down
64 changes: 62 additions & 2 deletions packages/react/spec/components/SignedIn.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import React from "react";
import { superAuthApi } from "../../spec/apis";
import { SignedIn } from "../../src/components/SignedIn";
import { TestWrapper, mockUrqlClient } from "../testWrapper";

describe("SignedIn", () => {
test("renders without crashing", () => {
expect(true).toBe(false);
test("renders children when signed in", () => {
const component = (
<h1>
Hello<SignedIn>, Jane!</SignedIn>
</h1>
);

const { container, rerender } = render(component, { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: "321",
user: {
id: "321",
firstName: "Jane",
lastName: "Doe",
},
},
},
stale: false,
hasNext: false,
});

rerender(component);

// return session
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello, Jane!</h1></div>"`);
});

test("renders nothing when signed out", () => {
const component = (
<h1>
Hello<SignedIn>, Jane!</SignedIn>
</h1>
);

const { container, rerender } = render(component, { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: null,
user: null,
},
},
stale: false,
hasNext: false,
});
rerender(component);
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello</h1></div>"`);
});
});
84 changes: 81 additions & 3 deletions packages/react/spec/components/SignedInOrRedirect.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
describe("SignedInOrRedirect", () => {
test("renders without crashing", () => {
expect(true).toBe(false);
import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import React from "react";
import { superAuthApi } from "../../spec/apis";
import { TestWrapper, mockUrqlClient } from "../../spec/testWrapper";
import { SignedInOrRedirect } from "../../src/components/SignedInOrRedirect";

describe("SignedInOrRedirectOrRedirect", () => {
const { location } = window;
const mockAssign = jest.fn();

beforeAll(() => {
delete window.location;
window.location = { assign: mockAssign };
});

afterEach(() => {
mockAssign.mockClear();
});

afterAll(() => {
window.location = location;
});

test("redirects when signed out", () => {
const component = (
<h1>
<SignedInOrRedirect>Hello, Jane!</SignedInOrRedirect>
</h1>
);

const { rerender } = render(component, { wrapper: TestWrapper(superAuthApi) });

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

rerender(component);
expect(mockAssign).toHaveBeenCalledTimes(1);
expect(mockAssign).toHaveBeenCalledWith("/auth/signin");
});

test("renders when signed in", () => {
const component = (
<h1>
<SignedInOrRedirect>Hello, Jane!</SignedInOrRedirect>
</h1>
);

const { container, rerender } = render(component, { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: "321",
user: {
id: "321",
firstName: "Jane",
lastName: "Doe",
},
},
},
stale: false,
hasNext: false,
});

rerender(component);

expect(mockAssign).not.toBeCalled();
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello, Jane!</h1></div>"`);
});
});
63 changes: 61 additions & 2 deletions packages/react/spec/components/SignedOut.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import React from "react";
import { superAuthApi } from "../../spec/apis";
import { TestWrapper, mockUrqlClient } from "../../spec/testWrapper";
import { SignedOut } from "../../src/components/SignedOut";

describe("SignedOut", () => {
test("renders without crashing", () => {
expect(true).toBe(false);
test("renders children when signed out", () => {
const component = (
<h1>
Hello<SignedOut>, Jane!</SignedOut>
</h1>
);

const { container, rerender } = render(component, { wrapper: TestWrapper(superAuthApi) });

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

rerender(component);
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello, Jane!</h1></div>"`);
});

test("renders nothing when signed in", () => {
const component = (
<h1>
Hello<SignedOut>, Jane!</SignedOut>
</h1>
);

const { container, rerender } = render(component, { wrapper: TestWrapper(superAuthApi) });

expect(mockUrqlClient.executeQuery).toBeCalledTimes(1);
mockUrqlClient.executeQuery.pushResponse("currentSession", {
data: {
currentSession: {
id: "123",
userId: "321",
user: {
id: "321",
firstName: "Jane",
lastName: "Doe",
},
},
},
stale: false,
hasNext: false,
});

rerender(component);
expect(container.outerHTML).toMatchInlineSnapshot(`"<div><h1>Hello</h1></div>"`);
});
});
Loading

0 comments on commit ae2c10c

Please sign in to comment.