Skip to content

πŸ™Œ OpenHands: AI-Driven Development

setup env for controlled integration tests with redux (#1180)

sp.wack426f3871235c
diff --git a/frontend/src/components/Browser.test.tsx b/frontend/src/components/Browser.test.tsx
new file mode 100644
index 0000000000..83d094e18a
--- /dev/null
+++ b/frontend/src/components/Browser.test.tsx
@@ -0,0 +1,33 @@
+import React from "react";
+import Browser from "./Browser";
+import { renderWithProviders } from "../../test-utils";
+
+describe("Browser", () => {
+ it("renders a message if no screenshotSrc is provided", () => {
+ const { getByText } = renderWithProviders(<Browser />, {
+ preloadedState: {
+ browser: {
+ url: "https://example.com",
+ screenshotSrc: "",
+ },
+ },
+ });
+
+ expect(getByText(/no screenshot available/i)).toBeInTheDocument();
+ });
+
+ it("renders the url and a screenshot", () => {
+ const { getByText, getByAltText } = renderWithProviders(<Browser />, {
+ preloadedState: {
+ browser: {
+ url: "https://example.com",
+ screenshotSrc:
+ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mN0uGvyHwAFCAJS091fQwAAAABJRU5ErkJggg==",
+ },
+ },
+ });
+
+ expect(getByText("https://example.com")).toBeInTheDocument();
+ expect(getByAltText(/browser screenshot/i)).toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/setupTests.ts b/frontend/src/setupTests.ts
deleted file mode 100644
index 1dd407a63e..0000000000
--- a/frontend/src/setupTests.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-// jest-dom adds custom jest matchers for asserting on DOM nodes.
-// allows you to do things like:
-// expect(element).toHaveTextContent(/react/i)
-// learn more: https://github.com/testing-library/jest-dom
-import "@testing-library/jest-dom";
diff --git a/frontend/src/store.ts b/frontend/src/store.ts
index b0a54a7467..f74c8ec7b8 100644
--- a/frontend/src/store.ts
+++ b/frontend/src/store.ts
@@ -1,4 +1,4 @@
-import { configureStore } from "@reduxjs/toolkit";
+import { combineReducers, configureStore } from "@reduxjs/toolkit";
import browserReducer from "./state/browserSlice";
import chatReducer from "./state/chatSlice";
import codeReducer from "./state/codeSlice";
@@ -7,19 +7,22 @@ import taskReducer from "./state/taskSlice";
import errorsReducer from "./state/errorsSlice";
import settingsReducer from "./state/settingsSlice";
+export const rootReducer = combineReducers({
+ browser: browserReducer,
+ chat: chatReducer,
+ code: codeReducer,
+ cmd: commandReducer,
+ task: taskReducer,
+ errors: errorsReducer,
+ settings: settingsReducer,
+});
+
const store = configureStore({
- reducer: {
- browser: browserReducer,
- chat: chatReducer,
- code: codeReducer,
- cmd: commandReducer,
- task: taskReducer,
- errors: errorsReducer,
- settings: settingsReducer,
- },
+ reducer: rootReducer,
});
export type RootState = ReturnType<typeof store.getState>;
+export type AppStore = typeof store;
export type AppDispatch = typeof store.dispatch;
export default store;
diff --git a/frontend/test-utils.tsx b/frontend/test-utils.tsx
new file mode 100644
index 0000000000..669403b684
--- /dev/null
+++ b/frontend/test-utils.tsx
@@ -0,0 +1,38 @@
+// See https://redux.js.org/usage/writing-tests#setting-up-a-reusable-test-render-function for more information
+
+import React, { PropsWithChildren } from "react";
+import { Provider } from "react-redux";
+import { configureStore } from "@reduxjs/toolkit";
+// eslint-disable-next-line import/no-extraneous-dependencies
+import { RenderOptions, render } from "@testing-library/react";
+import { AppStore, RootState, rootReducer } from "./src/store";
+
+const setupStore = (preloadedState?: Partial<RootState>): AppStore =>
+ configureStore({
+ reducer: rootReducer,
+ preloadedState,
+ });
+
+// This type interface extends the default options for render from RTL, as well
+// as allows the user to specify other things such as initialState, store.
+interface ExtendedRenderOptions extends Omit<RenderOptions, "queries"> {
+ preloadedState?: Partial<RootState>;
+ store?: AppStore;
+}
+
+// Export our own customized renderWithProviders function that creates a new Redux store and renders a <Provider>
+// Note that this creates a separate Redux store instance for every test, rather than reusing the same store instance and resetting its state
+export function renderWithProviders(
+ ui: React.ReactElement,
+ {
+ preloadedState = {},
+ // Automatically create a store instance if no store was passed in
+ store = setupStore(preloadedState),
+ ...renderOptions
+ }: ExtendedRenderOptions = {},
+) {
+ function Wrapper({ children }: PropsWithChildren<object>): JSX.Element {
+ return <Provider store={store}>{children}</Provider>;
+ }
+ return { store, ...render(ui, { wrapper: Wrapper, ...renderOptions }) };
+}
diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json
index b237bcddc5..620b737ede 100644
--- a/frontend/tsconfig.json
+++ b/frontend/tsconfig.json
@@ -24,6 +24,8 @@
"include": [
"src",
"vite-env.d.ts",
- "vite.config.ts"
+ "vite.config.ts",
+ "vitest.setup.ts",
+ "test-utils.tsx"
]
}
diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts
index b1c9e25533..d6cec1167b 100644
--- a/frontend/vite.config.ts
+++ b/frontend/vite.config.ts
@@ -35,5 +35,6 @@ export default defineConfig({
test: {
environment: "jsdom",
globals: true,
+ setupFiles: ["vitest.setup.ts"],
},
});
diff --git a/frontend/vitest.setup.ts b/frontend/vitest.setup.ts
new file mode 100644
index 0000000000..7e3bc7a68f
--- /dev/null
+++ b/frontend/vitest.setup.ts
@@ -0,0 +1,3 @@
+// learn more: https://github.com/testing-library/jest-dom
+// eslint-disable-next-line import/no-extraneous-dependencies
+import "@testing-library/jest-dom";