test: add test behavior to back button

This commit is contained in:
Corbin Crutchley
2023-08-16 03:39:46 -07:00
parent b81d2ba717
commit b1ff493357
2 changed files with 40 additions and 1 deletions

View File

@@ -9,12 +9,21 @@ import "jest-location-mock";
global.plausible = null; global.plausible = null;
let history = [];
window.history.pushState = (data, unused, url) => { window.history.pushState = (data, unused, url) => {
window.location.assign(url); window.location.assign(url);
history.push(url);
}; };
window.history.replaceState = (data, unused, url) => { window.history.replaceState = (data, unused, url) => {
window.location.assign(url); window.location.assign(url);
history[history.length - 1] = url;
};
window.history.back = () => {
history.pop();
window.location.assign(history[history.length - 1]);
}; };
global.IntersectionObserver = class IntersectionObserver { global.IntersectionObserver = class IntersectionObserver {

View File

@@ -1129,5 +1129,35 @@ describe("Search page", () => {
); );
}); });
test.todo("Back button should show last query"); test("Back button should show last query", async () => {
mockFetch(() => ({
posts: [],
totalPosts: 0,
totalCollections: 0,
collections: [],
}));
const { getByTestId, getByText, getByLabelText, queryByText } = render(
<SearchPage unicornProfilePicMap={[]} />,
);
const searchInput = getByLabelText("Search");
await user.type(searchInput, "blog");
await waitFor(() =>
expect(getByText("No results found...")).toBeInTheDocument(),
);
await user.type(searchInput, "other");
await waitFor(() =>
expect(window.location.search).toBe("?searchQuery=blogother"),
);
history.back();
await waitFor(() =>
expect(window.location.search).toBe("?searchQuery=blog"),
);
});
}); });