fix rendering and storage issues for unit tests.
This commit is contained in:
23
components/__tests__/ProductEditor-test.tsx
Normal file
23
components/__tests__/ProductEditor-test.tsx
Normal 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();
|
||||
});
|
||||
});
|
@ -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 })
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user