From b1239935da1dff2902d5f6af9a9622f4e887479f Mon Sep 17 00:00:00 2001
From: Joe Banks <joe@jb3.dev>
Date: Tue, 9 Jul 2024 21:55:24 +0100
Subject: Add new testing utility for rendering with Redux stores

---
 src/tests/utils.tsx | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 src/tests/utils.tsx

(limited to 'src/tests/utils.tsx')

diff --git a/src/tests/utils.tsx b/src/tests/utils.tsx
new file mode 100644
index 0000000..011291a
--- /dev/null
+++ b/src/tests/utils.tsx
@@ -0,0 +1,36 @@
+/** @jsx jsx */
+import { jsx } from "@emotion/react";
+import { PropsWithChildren } from "react";
+import { render } from "@testing-library/react";
+import type { RenderOptions } from "@testing-library/react";
+import { Provider } from "react-redux";
+
+import type { AppStore, RootState } from "../store";
+import { setupStore } from "../store";
+
+interface ExtendedRenderOptions extends Omit<RenderOptions, "queries"> {
+  preloadedState?: Partial<RootState>
+  store?: AppStore
+}
+
+export function renderWithProviders(
+    ui: React.ReactElement,
+    extendedRenderOptions: ExtendedRenderOptions = {}
+) {
+    const {
+        preloadedState = {},
+        // Automatically create a store instance if no store was passed in
+        store = setupStore(preloadedState),
+        ...renderOptions
+    } = extendedRenderOptions;
+
+    const Wrapper = ({ children }: PropsWithChildren) => (
+        <Provider store={store}>{children}</Provider>
+    );
+
+    // Return an object with the store and all of RTL"s query functions
+    return {
+        store,
+        ...render(ui, { wrapper: Wrapper, ...renderOptions })
+    };
+}
-- 
cgit v1.2.3

