Skip to content

Commit

Permalink
Add test for new GenerativeAIToggle component
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjarling committed Mar 5, 2024
1 parent df31078 commit 37baf21
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 2 deletions.
117 changes: 117 additions & 0 deletions components/Search/GenerativeAIToggle.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import {
SearchProvider,
defaultState as defaultSearchState,
} from "@/context/search-context";
import { render, screen } from "@testing-library/react";

import GenerativeAIToggle from "./GenerativeAIToggle";
import React from "react";
import { UserContext } from "@/context/user-context";
import { UserContext as UserContextType } from "@/types/context/user";
import mockRouter from "next-router-mock";
import userEvent from "@testing-library/user-event";

const defaultUser = {
user: {
email: "[email protected]",
isLoggedIn: true,
isReadingRoom: false,
name: "Ace Frehley",
sub: "xyz123",
},
};

const withUserProvider = (
Component: React.ReactNode,
user: UserContextType = defaultUser
) => {
return <UserContext.Provider value={user}>{Component}</UserContext.Provider>;
};

const withSearchProvider = (
Component: React.ReactNode,
initialState = defaultSearchState
) => {
return (
<SearchProvider initialState={initialState}>{Component}</SearchProvider>
);
};

describe("GenerativeAIToggle", () => {
it("renders the generative AI toggle UI and toggles state for a logged in user", async () => {
const user = userEvent.setup();
render(
withUserProvider(
withSearchProvider(<GenerativeAIToggle isSearchActive={true} />)
)
);

const label = screen.getByLabelText("Use Generative AI");
const checkbox = screen.getByRole("checkbox");

expect(label).toBeInTheDocument();
expect(checkbox).toHaveAttribute("data-state", "unchecked");

await user.click(checkbox);
expect(checkbox).toHaveAttribute("data-state", "checked");
});

it("renders the generative AI tooltip", () => {
render(withSearchProvider(<GenerativeAIToggle isSearchActive={true} />));
// Target the svg icon itself
const tooltip = screen.getByText("Information Circle");

expect(tooltip).toBeInTheDocument();
});

it("renders the generative AI dialog for a non-logged in user", async () => {
const user = userEvent.setup();
const nonLoggedInUser = {
user: {
...defaultUser.user,
isLoggedIn: false,
},
};

render(
withUserProvider(
withSearchProvider(<GenerativeAIToggle isSearchActive={true} />),
nonLoggedInUser
)
);

const checkbox = screen.getByRole("checkbox");
await user.click(checkbox);

const generativeAIDialog = screen.queryByText(
"You must be logged in with a Northwestern NetID to use the Generative AI search feature."
);
const cancelButton = screen.getByText("Cancel");

expect(generativeAIDialog).toBeInTheDocument();
expect(screen.getByText("Login")).toBeInTheDocument();
expect(cancelButton).toBeInTheDocument();

await user.click(cancelButton);

expect(generativeAIDialog).not.toBeInTheDocument();
});

it("renders a toggled generative ai state when a query param is set", () => {
const activeSearchState = {
...defaultSearchState,
isGenerativeAI: true,
};

mockRouter.setCurrentUrl("/search?ai=true");
render(
withSearchProvider(
<GenerativeAIToggle isSearchActive={true} />,
activeSearchState
)
);

const checkbox = screen.getByRole("checkbox");
expect(checkbox).toHaveAttribute("data-state", "checked");
});
});
2 changes: 1 addition & 1 deletion components/Search/GenerativeAIToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const TooltipContent = styled(Tooltip.Content, {
function GenerativeAITooltip() {
return (
<Tooltip.Provider delayDuration={20}>
<Tooltip.Root>
<Tooltip.Root data-testid="tooltip">
<TooltipTrigger>
<IconInfo />
</TooltipTrigger>
Expand Down
73 changes: 73 additions & 0 deletions components/Search/newFile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
defaultUser,
withSearchProvider,
withUserProvider,
} from "./GenerativeAIToggle.test";
import { render, screen } from "@testing-library/react";

import GenerativeAIToggle from "./GenerativeAIToggle";
import React from "react";
import userEvent from "@testing-library/user-event";

describe("GenerativeAIToggle", () => {
it("renders the generative AI toggle UI and toggles state for a logged in user", async () => {
const user = userEvent.setup();
render(
withUserProvider(
withSearchProvider(<GenerativeAIToggle isSearchActive={true} />)
)
);

const label = screen.getByLabelText("Use Generative AI");
const checkbox = screen.getByRole("checkbox");

expect(label).toBeInTheDocument();
expect(checkbox).toHaveAttribute("data-state", "unchecked");

await user.click(checkbox);
expect(checkbox).toHaveAttribute("data-state", "checked");
});

it("renders the generative AI tooltip", () => {
render(withSearchProvider(<GenerativeAIToggle isSearchActive={true} />));
// Target the svg icon itself
const tooltip = screen.getByText("Information Circle");

expect(tooltip).toBeInTheDocument();
});

it("renders the generative AI dialog for a non-logged in user", async () => {
const user = userEvent.setup();
const nonLoggedInUser = {
user: {
...defaultUser.user,
isLoggedIn: false,
},
};

render(
withUserProvider(
withSearchProvider(<GenerativeAIToggle isSearchActive={true} />),
nonLoggedInUser
)
);

const checkbox = screen.getByRole("checkbox");
await user.click(checkbox);

const generativeAIDialog = screen.getByText(
"You must be logged in with a Northwestern NetID to use the Generative AI search feature."
);
const cancelButton = screen.getByText("Cancel");

expect(generativeAIDialog).toBeInTheDocument();
expect(screen.getByText("Login")).toBeInTheDocument();
expect(cancelButton).toBeInTheDocument();

user.click(cancelButton);

expect;
});

it("renders a toggled generative ai state when a query param is set", () => {});
});
2 changes: 1 addition & 1 deletion context/search-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ function useSearchState() {
return context;
}

export { SearchProvider, useSearchState };
export { SearchProvider, defaultState, useSearchState };

0 comments on commit 37baf21

Please sign in to comment.