diff --git a/frontend/__tests__/__mocks__/fileMock.js b/frontend/__tests__/__mocks__/fileMock.js new file mode 100644 index 0000000..0a445d0 --- /dev/null +++ b/frontend/__tests__/__mocks__/fileMock.js @@ -0,0 +1 @@ +module.exports = "test-file-stub"; diff --git a/frontend/__tests__/unit/components/Bar.test.jsx b/frontend/__tests__/unit/components/Bar.test.jsx new file mode 100644 index 0000000..446d349 --- /dev/null +++ b/frontend/__tests__/unit/components/Bar.test.jsx @@ -0,0 +1,47 @@ +import { render } from "@testing-library/react"; +import Bar from "components/Bar"; +import { Router } from "react-router-dom"; +import { createMemoryHistory } from "history"; + +// Useful reference: https://bholmes.dev/blog/mocking-browser-apis-fetch-localstorage-dates-the-easy-way-with-jest/ + +let mockStorage = {}; + +describe("Bar", () => { + beforeAll(() => { + global.Storage.prototype.getItem = jest.fn((key) => mockStorage[key]); + }); + + beforeEach(() => { + // make sure the fridge starts out empty for each test + mockStorage = {}; + }); + + afterAll(() => { + global.Storage.prototype.getItem.mockReset(); + }); + + it("renders Bar unchanged when logged out", () => { + const history = createMemoryHistory(); + mockStorage["loggedIn"] = false; + + const { container } = render( + + + + ); + expect(container).toMatchSnapshot(); + }); + + it("renders Bar unchanged when logged in", () => { + const history = createMemoryHistory(); + mockStorage["loggedIn"] = true; + + const { container } = render( + + + + ); + expect(container).toMatchSnapshot(); + }); +}); diff --git a/frontend/__tests__/unit/components/__snapshots__/Bar.test.jsx.snap b/frontend/__tests__/unit/components/__snapshots__/Bar.test.jsx.snap new file mode 100644 index 0000000..6a4346c --- /dev/null +++ b/frontend/__tests__/unit/components/__snapshots__/Bar.test.jsx.snap @@ -0,0 +1,104 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Bar renders Bar unchanged when logged in 1`] = ` +
+
+
+
+
+ + logo + +
+
+ +
+
+
+`; + +exports[`Bar renders Bar unchanged when logged out 1`] = ` +
+
+
+
+
+ + logo + +
+
+ +
+
+
+`; diff --git a/frontend/jest.config.js b/frontend/jest.config.js index 73948c2..4dea552 100644 --- a/frontend/jest.config.js +++ b/frontend/jest.config.js @@ -24,7 +24,8 @@ const customJestConfig = { moduleNameMapper: { "^uuid$": require.resolve("uuid"), "^@fontsource/roboto$": "identity-obj-proxy", - "\\.(png|css)$": "identity-obj-proxy", + "\\.(css)$": "identity-obj-proxy", + "\\.(png|pdf|svg|jpg|jpeg)$": "/__tests__/__mocks__/fileMock.js", }, testPathIgnorePatterns: ["/cypress/"], };