Skip to content

Commit

Permalink
Adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
rosschapman committed Jan 18, 2025
1 parent a0f5f9d commit c3562bc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
53 changes: 53 additions & 0 deletions app/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { App } from "App";
import { HelmetProvider } from "react-helmet-async";
import { AppProvider } from "utils";

jest.mock("instantsearch.js/es/lib/routers", () => ({}));
jest.mock("./utils/useAppContext", () => ({
AppProvider: jest.fn(),
}));

describe("<App />", () => {
it("renders", async () => {
render(
<HelmetProvider>
<App />
</HelmetProvider>,
{ wrapper: BrowserRouter }
);

await waitFor(() => {
expect(screen.getByTestId("app-container")).toBeInTheDocument();
});
});

// Generally we want to avoid testing prop passing directly, and instead
// figure out a test that reflects real user behavior to confirm the props.
// Let's use this for the time being until we have a better way to test these
// props being set on page load. It may make sense to move the data fetching
// and setting to a hook we can test independently.
it("sends the correct props to the <AppProvider />", async () => {
const expectedArg1 = expect.objectContaining({
aroundLatLng: "37.7749,-122.4194",
userLocation: { lat: 37.7749, lng: -122.4194 },
aroundUserLocationRadius: "all",
setAroundRadius: expect.any(Function),
setAroundLatLng: expect.any(Function),
});
const expectedArg2 = {};

render(
<HelmetProvider>
<App />
</HelmetProvider>,
{ wrapper: BrowserRouter }
);

await waitFor(() => {
expect(AppProvider).toHaveBeenCalledWith(expectedArg1, expectedArg2);
});
});
});
9 changes: 8 additions & 1 deletion app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export const App = () => {
}, [location, setAroundLatLng]);

if (!userLocation) {
console.log("🪵 ~ App ~ userLocation:", userLocation);

return <Loader />;
}

Expand All @@ -66,9 +68,14 @@ export const App = () => {
aroundUserLocationRadius,
setAroundRadius,
};
console.log("🪵 ~ App ~ props:", props);

return (
<div id={OUTER_CONTAINER_ID} className={styles.outerContainer}>
<div
id={OUTER_CONTAINER_ID}
className={styles.outerContainer}
data-testid={"app-container"}
>
<AppProvider {...props}>
<Helmet>
<title>{title}</title>
Expand Down
5 changes: 1 addition & 4 deletions app/pages/EventDetailPage/EventDetailPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import { EventDetailPage } from "pages/EventDetailPage/EventDetailPage";
import {
createFormattedEventData,
EVENTS_DATA,
} from "../../../test/fixtures/EventsData";
import { createFormattedEventData } from "../../../test/fixtures/EventsData";
import { useEventData } from "hooks/StrapiAPI";
import { HelmetProvider } from "react-helmet-async";

Expand Down
1 change: 0 additions & 1 deletion app/utils/useAppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, {
useMemo,
createContext,
useContext,
useState,
Dispatch,
SetStateAction,
} from "react";
Expand Down
5 changes: 4 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ const config: Config = {

// A set of global variables that need to be available in all test environments
globals: {
CONFIG: {},
CONFIG: {
ALGOLIA_APPLICATION_ID: "ALGOLIA_APPLICATION_ID",
ALGOLIA_READ_ONLY_API_KEY: "ALGOLIA_READ_ONLY_API_KEY",
},
},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
Expand Down
15 changes: 14 additions & 1 deletion test/templates/Template.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* coding from there!
*/
import React from "react";
import { render, screen } from "@testing-library/react";
import { render, screen, waitFor } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";

// Normally imported from components directory
const MyFakeComponent = ({ text }: { text: string }) => <>{text}</>;

// Put any module mocks outside test blocks
jest.mock("<my-module>", () => (<'mock implementation'>));

describe("<MyFakeComponent />", () => {
const parameters = {
text: "expected content",
Expand All @@ -22,4 +25,14 @@ describe("<MyFakeComponent />", () => {
parameters.text
);
});

it("renders with async effects", async () => {
render(<MyFakeComponent {...parameters} />, { wrapper: BrowserRouter });

await waitFor(() => {
expect(screen.getByTestId("my-component-test-id")).toHaveTextContent(
parameters.text
);
});
});
});

0 comments on commit c3562bc

Please sign in to comment.