fix rendering and storage issues for unit tests.

This commit is contained in:
Jordan
2024-06-28 06:04:50 -07:00
parent ab7a43ef84
commit 3d03f1d9e8
6 changed files with 43 additions and 25 deletions

View File

@ -0,0 +1,23 @@
import { renderWithProviders } from "@/lib/rendering";
import { ProductEditor } from "@/components/ProductEditor";
import {products as fixtures} from "../../__fixtures__/initialProducts";
import React from "react";
import { useAppSelector } from "@/app/store";
import { selectProducts } from "@/features/product/productSlice";
import { screen } from "@testing-library/react-native";
describe("ProductEditor", () => {
it("renders correctly", async () => {
const {store} = renderWithProviders(<ProductEditor />, {
products: fixtures,
});
const products = store.getState();
// Check if the product names are rendered
expect(screen.getByText(products[0].attributes.name as string)).toBeTruthy();
expect(screen.getByText(products[1].attributes.name as string)).toBeTruthy();
expect(screen.getByText(products[2].attributes.name as string)).toBeTruthy();
expect(screen.getByText(products[3].attributes.name as string)).toBeTruthy();
});
});

View File

@ -1,41 +0,0 @@
import { RenderOptions, render } from "@testing-library/react-native";
import { PropsWithChildren, ReactElement } from "react";
import { Provider } from "react-redux";
import { setupStore, RootState } from "@/app/store";
import { Price, Product } from "@/lib/product";
import { area, length } from "enheter";
export interface ExtendedRenderOptions extends Omit<RenderOptions, 'queries'> {
preloadedState?: Partial<RootState>;
store?: any; // TODO
}
const basicState = {
products: [
new Product(20.00, length("foot", 5), {name: "Track"}),
new Product(20.00, area("squareFoot", 5), {name: "Shelf"}),
]
}
export function renderWithProviders(
ui: ReactElement,
preloadedState = basicState,
extendedRenderOptions: ExtendedRenderOptions = {},
) {
const {
// Automatically create a store instance if no store was passed in
store = setupStore(preloadedState as Partial<RootState>),
...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 })
};
}