Skip to content

Commit

Permalink
Revamp tests, update expires time to now + 1 hour.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewjordan committed Dec 4, 2024
1 parent ca2ef6d commit 1c1daa9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
18 changes: 14 additions & 4 deletions components/Search/GenerativeAIToggle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ describe("GenerativeAIToggle", () => {

await user.click(checkbox);
expect(checkbox).toHaveAttribute("data-state", "checked");
expect(localStorage.getItem("ai")).toEqual(JSON.stringify("true"));

const ai = JSON.parse(String(localStorage.getItem("ai")));
expect(ai?.enabled).toEqual("true");
expect(typeof ai?.expires).toEqual("number");
expect(ai?.expires).toBeGreaterThan(Date.now());
});

it("renders the generative AI tooltip", () => {
Expand Down Expand Up @@ -99,7 +103,10 @@ describe("GenerativeAIToggle", () => {
...defaultSearchState,
};

localStorage.setItem("ai", JSON.stringify("true"));
localStorage.setItem(
"ai",
JSON.stringify({ enabled: "true", expires: 9733324925021 }),
);

mockRouter.setCurrentUrl("/search");
render(
Expand All @@ -117,7 +124,7 @@ describe("GenerativeAIToggle", () => {

mockRouter.setCurrentUrl("/");

localStorage.setItem("ai", JSON.stringify("false"));
localStorage.setItem("ai", JSON.stringify({ enabled: "false" }));

render(
withUserProvider(
Expand All @@ -127,6 +134,9 @@ describe("GenerativeAIToggle", () => {

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

expect(localStorage.getItem("ai")).toEqual(JSON.stringify("true"));
const ai = JSON.parse(String(localStorage.getItem("ai")));
expect(ai?.enabled).toEqual("true");
expect(typeof ai?.expires).toEqual("number");
expect(ai?.expires).toBeGreaterThan(Date.now());
});
});
5 changes: 4 additions & 1 deletion components/Search/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ describe("Search component", () => {
});

it("renders generative AI placeholder text when AI search is active", () => {
localStorage.setItem("ai", JSON.stringify("true"));
localStorage.setItem(
"ai",
JSON.stringify({ enabled: "true", expires: 9733324925021 }),
);

render(withUserProvider(<Search isSearchActive={mockIsSearchActive} />));

Expand Down
2 changes: 1 addition & 1 deletion hooks/useGenerativeAISearchToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function useGenerativeAISearchToggle() {

const [dialog, setDialog] = useState(defaultModalState);

const expires = Date.now() + 1000 * 60 * 1;
const expires = Date.now() + 1000 * 60 * 60;
const isAIPreference = ai.enabled === "true";
const isChecked = isAIPreference && user?.isLoggedIn;

Expand Down

0 comments on commit 1c1daa9

Please sign in to comment.