From e2e3d303c1a977a4e51da0c5e2444839a1336b21 Mon Sep 17 00:00:00 2001
From: David Laganiere <40720561+davidlag0@users.noreply.github.com>
Date: Mon, 12 Jun 2023 08:26:48 -0400
Subject: [PATCH] test(Bar): add tests to almost fully cover
---
.../__tests__/unit/components/Bar.test.jsx | 100 +++++++++++++++++-
.../__snapshots__/Bar.test.jsx.snap | 67 ++++++++++++
2 files changed, 164 insertions(+), 3 deletions(-)
diff --git a/frontend/__tests__/unit/components/Bar.test.jsx b/frontend/__tests__/unit/components/Bar.test.jsx
index 446d349..3ea3c39 100644
--- a/frontend/__tests__/unit/components/Bar.test.jsx
+++ b/frontend/__tests__/unit/components/Bar.test.jsx
@@ -1,7 +1,8 @@
-import { render } from "@testing-library/react";
+import { render, screen } from "@testing-library/react";
import Bar from "components/Bar";
-import { Router } from "react-router-dom";
+import { MemoryRouter, Route, Router } from "react-router-dom";
import { createMemoryHistory } from "history";
+import userEvent from "@testing-library/user-event";
// Useful reference: https://bholmes.dev/blog/mocking-browser-apis-fetch-localstorage-dates-the-easy-way-with-jest/
@@ -10,10 +11,10 @@ let mockStorage = {};
describe("Bar", () => {
beforeAll(() => {
global.Storage.prototype.getItem = jest.fn((key) => mockStorage[key]);
+ global.Storage.prototype.clear = jest.fn(() => (mockStorage = {}));
});
beforeEach(() => {
- // make sure the fridge starts out empty for each test
mockStorage = {};
});
@@ -44,4 +45,97 @@ describe("Bar", () => {
);
expect(container).toMatchSnapshot();
});
+
+ it("renders Bar unchanged when logged in, after clicking button to open menu", async () => {
+ const history = createMemoryHistory();
+ const user = userEvent.setup();
+ mockStorage["loggedIn"] = true;
+
+ const { container } = render(
+
+
+
+ );
+
+ const menuButton = screen.getByRole("button", {
+ name: "",
+ });
+
+ await user.click(menuButton);
+
+ const logOutMenuItem = screen.getByRole("menuitem", {
+ name: "Log out",
+ });
+ expect(logOutMenuItem).toBeInTheDocument();
+
+ expect(container).toMatchSnapshot();
+ });
+
+ test("open and close menu when logged in", async () => {
+ const history = createMemoryHistory();
+ const user = userEvent.setup();
+ mockStorage["loggedIn"] = true;
+
+ render(
+
+
+
+ );
+
+ const menuButton = screen.getByRole("button", {
+ name: "",
+ });
+
+ // Open menu
+ await user.click(menuButton);
+
+ const logOutMenuItem = screen.getByRole("menuitem", {
+ name: "Log out",
+ });
+ expect(logOutMenuItem).toBeInTheDocument();
+
+ // Close menu
+ await user.keyboard("{Escape}");
+
+ expect(logOutMenuItem).not.toBeInTheDocument();
+ });
+
+ test("open menu and click on 'Log out'", async () => {
+ const user = userEvent.setup();
+ mockStorage["loggedIn"] = true;
+ let testLocation;
+
+ render(
+
+
+
+
+ {
+ testLocation = location;
+ return null;
+ }}
+ />
+
+ );
+
+ const menuButton = screen.getByRole("button", {
+ name: "",
+ });
+
+ // Open menu
+ await user.click(menuButton);
+
+ const logOutMenuItem = screen.getByRole("menuitem", {
+ name: "Log out",
+ });
+ expect(logOutMenuItem).toBeInTheDocument();
+
+ // Click on 'Log out'
+ await user.click(logOutMenuItem);
+
+ expect(Object.keys(mockStorage).length).toBe(0);
+ expect(testLocation.pathname).toBe("/");
+ });
});
diff --git a/frontend/__tests__/unit/components/__snapshots__/Bar.test.jsx.snap b/frontend/__tests__/unit/components/__snapshots__/Bar.test.jsx.snap
index 6a4346c..305795d 100644
--- a/frontend/__tests__/unit/components/__snapshots__/Bar.test.jsx.snap
+++ b/frontend/__tests__/unit/components/__snapshots__/Bar.test.jsx.snap
@@ -56,6 +56,73 @@ exports[`Bar renders Bar unchanged when logged in 1`] = `
`;
+exports[`Bar renders Bar unchanged when logged in, after clicking button to open menu 1`] = `
+
+`;
+
exports[`Bar renders Bar unchanged when logged out 1`] = `