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 Jun 30, 2023
1 parent 2a60a93 commit 2786e79
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"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
25 changes: 23 additions & 2 deletions packages/react/spec/components/SignedIn.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { render } from "@testing-library/react";
import React from "react";
import { SignedIn } from "../../src/components/SignedIn";
import { TestWrapperWithAuth } from "../testWrapper";
import '@testing-library/jest-dom'

describe("SignedIn", () => {
test("renders without crashing", () => {
expect(true).toBe(false);
test("renders children when signed in", () => {
// TODO: mock useSession hook
const { getByText} = render((
<h1>
Hello<SignedIn>, Jane!</SignedIn>
</h1>
), { wrapper: TestWrapperWithAuth });
expect(getByText('Hello, Jane!')).toBeInTheDocument()
});

test("renders nothing when signed out", () => {
const { getByText} = render((
<h1>
Hello<SignedIn>, Jane!</SignedIn>
</h1>
), { wrapper: TestWrapperWithAuth });
expect(getByText(', Jane!')).not.toBeInTheDocument()
});
});
43 changes: 40 additions & 3 deletions packages/react/spec/components/SignedInOrRedirect.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
describe("SignedInOrRedirect", () => {
test("renders without crashing", () => {
expect(true).toBe(false);
import { render } from "@testing-library/react";
import React from "react";
import { SignedInOrRedirect } from "../../src/components/SignedInOrRedirect";
import { TestWrapperWithAuth } from "../testWrapper";
import '@testing-library/jest-dom'

describe("SignedInOrRedirectOrRedirect", () => {
test("redirects when signed out", () => {
render((
<SignedInOrRedirect>
Hello, Jane!
</SignedInOrRedirect>
), { wrapper: TestWrapperWithAuth });

const mockAssign = jest.fn();
Object.defineProperty(window, 'location', {
value: {
assign: mockAssign,
}
});

expect(mockAssign).toHaveBeenCalledWith('/auth/signin');
});

test("renders when signed in", () => {
const { getByText} = render((
<h1>
Hello<SignedInOrRedirect>, Jane!</SignedInOrRedirect>
</h1>
), { wrapper: TestWrapperWithAuth });

const mockAssign = jest.fn();
Object.defineProperty(window, 'location', {
value: {
assign: mockAssign,
}
});

expect(mockAssign).not.toBeCalled();
expect(getByText(', Jane!')).not.toBeInTheDocument();
});
});
25 changes: 23 additions & 2 deletions packages/react/spec/components/SignedOut.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { render } from "@testing-library/react";
import React from "react";
import { SignedOut } from "../../src/components/SignedOut";
import { TestWrapperWithAuth } from "../testWrapper";
import '@testing-library/jest-dom'

describe("SignedOut", () => {
test("renders without crashing", () => {
expect(true).toBe(false);
test("renders children when signed out", () => {
const { getByText} = render((
<h1>
Hello<SignedOut>, Jane!</SignedOut>
</h1>
), { wrapper: TestWrapperWithAuth });
expect(getByText('Hello, Jane!')).toBeInTheDocument()
});

test("renders nothing when signed in", () => {
// TODO: mock useSession hook
const { getByText} = render((
<h1>
Hello<SignedOut>, Jane!</SignedOut>
</h1>
), { wrapper: TestWrapperWithAuth });
expect(getByText(', Jane!')).not.toBeInTheDocument()
});
});
6 changes: 3 additions & 3 deletions packages/react/spec/testWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ export const TestWrapperWithAuth = (props: { children: ReactNode }) => {
// mimic a barebones api client with auth that will allow us to make mocked requests
jest.spyOn(GadgetConnection.prototype, "currentClient", "get").mockReturnValue(mockUrqlClient);
return (
<Provider api={superAuthApi}>
{/* <Suspense fallback={<div>Loading...</div>}> */}
<Provider api={superAuthApi as unknown as AnyClient}>
<Suspense fallback={<div>Loading...</div>}>
{props.children}
{/* </Suspense> */}
</Suspense>
</Provider>
);
};
8 changes: 1 addition & 7 deletions packages/react/src/auth/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,4 @@ export const useSession = (): GadgetSession | undefined => {
return session;
}

export const isSessionSignedOut = (session: GadgetSession) => {
return session?.userId == undefined;
}

export const isSessionSignedIn = (session: GadgetSession) => {
return !isSessionSignedOut(session);
}
export default useSession;
9 changes: 9 additions & 0 deletions packages/react/src/auth/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { GadgetSession } from "./useSession";

export const isSessionSignedOut = (session: GadgetSession) => {
return session?.userId == undefined;
}

export const isSessionSignedIn = (session: GadgetSession) => {
return !isSessionSignedOut(session);
}
3 changes: 2 additions & 1 deletion packages/react/src/components/SignedIn.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReactNode } from "react";
import { isSessionSignedIn, useSession } from "../../src/auth/useSession";
import { useSession } from "../../src/auth/useSession";
import { isSessionSignedIn } from "../../src/auth/utils";

export const SignedIn = (props: { children: ReactNode}) => {
const session = useSession();
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/components/SignedInOrRedirect.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ReactNode} from "react";
import { useContext, useEffect } from "react";
import { GadgetClientContext } from "src/GadgetProvider";
import { useSession, isSessionSignedIn } from "../../src/auth/useSession";
import { GadgetClientContext } from "../../src/GadgetProvider";
import { useSession } from "../../src/auth/useSession";
import { isSessionSignedIn } from "../../src/auth/utils";

export const SignedInOrRedirect = (props: { children: ReactNode }) => {
const session = useSession();
Expand All @@ -10,8 +11,7 @@ export const SignedInOrRedirect = (props: { children: ReactNode }) => {

useEffect(() => {
if (context?.signInPath && !isSignedIn) {
// TODO: change to provider value
window.location.href = context.signInPath;
window.location.assign(context.signInPath);
}
}, [isSignedIn, context?.signInPath]);

Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/components/SignedOut.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ReactNode } from "react";
import { isSessionSignedOut, useSession } from "../../src/auth/useSession";
import { useSession } from "../../src/auth/useSession";
import { isSessionSignedOut } from "../../src/auth/utils";

export const SignedOUt = (props: { children: ReactNode}) => {
export const SignedOut = (props: { children: ReactNode}) => {
const session = useSession();
if (!session || isSessionSignedOut(session)) {
return props.children;
Expand Down
Loading

0 comments on commit 2786e79

Please sign in to comment.