Skip to content

Commit

Permalink
add tests for sessionStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiequach committed Jul 14, 2022
1 parent cb19d47 commit b712165
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/__tests__/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -629,30 +629,52 @@ describe("Renders total works correctly when feature flag is set", () => {
beforeEach(() => {
act(() => {
resizeWindow(300, 1000);
Object.defineProperty(window, "sessionStorage", {
value: {
getItem: jest.fn(() => null),
setItem: jest.fn(() => null),
},
writable: true,
});
});
});
test("Total works is shown when feature flag query is true", () => {

test("Shown when feature flag query is true", () => {
mockRouter.push("?feature_totalCount=true");
render(
<SearchResults searchQuery={searchQuery} searchResults={searchResults} />
);
expect(window.sessionStorage.getItem).toHaveBeenCalledTimes(1);
expect(window.sessionStorage.setItem).toHaveBeenCalledTimes(1);
expect(window.sessionStorage.setItem).toHaveBeenCalledWith(
"featureFlags",
JSON.stringify({ totalCount: true })
);
expect(screen.getByText("Total number of works: 26")).toBeInTheDocument();
});

test("Total works is not shown when feature flag query is false", () => {
test("Not shown when feature flag query is false", () => {
mockRouter.push("?feature_totalCount=false");
render(
<SearchResults searchQuery={searchQuery} searchResults={searchResults} />
);
expect(window.sessionStorage.getItem).toHaveBeenCalledTimes(1);
expect(window.sessionStorage.setItem).toHaveBeenCalledTimes(1);
expect(window.sessionStorage.setItem).toHaveBeenCalledWith(
"featureFlags",
JSON.stringify({ totalCount: false })
);
expect(
screen.queryByText("Total number of works: 26")
).not.toBeInTheDocument();
});

test("Total works is not shown when feature flag query is not passed", () => {
test("Not shown when feature flag query is not passed", () => {
render(
<SearchResults searchQuery={searchQuery} searchResults={searchResults} />
);
expect(window.sessionStorage.getItem).toHaveBeenCalledTimes(1);
expect(window.sessionStorage.setItem).toHaveBeenCalledTimes(1);
expect(
screen.queryByText("Total number of works: 26")
).not.toBeInTheDocument();
Expand Down

0 comments on commit b712165

Please sign in to comment.