2024-06-28 15:04:50 +02:00
|
|
|
import { renderWithProviders } from "@/lib/rendering";
|
|
|
|
import { ProductEditor } from "@/components/ProductEditor";
|
2024-06-30 18:37:27 +02:00
|
|
|
import { act, fireEvent, screen } from "@testing-library/react-native";
|
2024-06-28 23:53:48 +02:00
|
|
|
import { selectProducts } from "@/features/product/productSlice";
|
2024-08-15 23:07:19 +02:00
|
|
|
import { LumberProduct, Product, productLabel } from "@/lib/product";
|
|
|
|
|
|
|
|
import initialProducts from "@/__fixtures__/initialProducts";
|
2024-06-28 15:04:50 +02:00
|
|
|
|
|
|
|
describe("ProductEditor", () => {
|
2024-07-31 19:01:45 +02:00
|
|
|
const productName = "Flooring";
|
2024-08-15 23:07:19 +02:00
|
|
|
const mockProduct = initialProducts[0];
|
2024-07-31 19:01:45 +02:00
|
|
|
it("renders correctly", async () => {
|
|
|
|
const { store } = renderWithProviders(<ProductEditor />, {
|
|
|
|
products: [mockProduct],
|
2024-06-28 15:04:50 +02:00
|
|
|
});
|
2024-07-31 19:01:45 +02:00
|
|
|
|
|
|
|
const state1 = store.getState();
|
|
|
|
|
|
|
|
let products = selectProducts(state1);
|
|
|
|
|
|
|
|
expect(products).toHaveLength(1);
|
|
|
|
|
|
|
|
// Check if the product names are rendered
|
|
|
|
expect(
|
2024-08-15 23:07:19 +02:00
|
|
|
screen.getByText(mockProduct.attributes?.name as string)
|
2024-07-31 19:01:45 +02:00
|
|
|
).toBeTruthy();
|
|
|
|
|
2024-08-15 23:07:19 +02:00
|
|
|
const label = productLabel(mockProduct);
|
|
|
|
|
2024-07-31 19:01:45 +02:00
|
|
|
// Start to edit a product
|
2024-08-15 23:07:19 +02:00
|
|
|
act(() => {
|
|
|
|
fireEvent.press(screen.getByText(label));
|
|
|
|
})
|
2024-07-31 19:01:45 +02:00
|
|
|
|
|
|
|
// Change properties of the product to make sure it's updated in the store
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
fireEvent.changeText(screen.getByLabelText("length"), "16");
|
|
|
|
});
|
|
|
|
products = selectProducts(store.getState());
|
|
|
|
expect(products[0].dimensions.l).toBe(16);
|
|
|
|
act(() => {
|
|
|
|
fireEvent.changeText(screen.getByLabelText("width"), "32");
|
|
|
|
});
|
|
|
|
products = selectProducts(store.getState());
|
|
|
|
|
|
|
|
expect(products[0].dimensions.w).toBe(32);
|
|
|
|
|
2024-08-15 23:07:19 +02:00
|
|
|
act(() => {
|
|
|
|
fireEvent.press(screen.getByLabelText("delete product"));
|
|
|
|
})
|
2024-07-31 19:01:45 +02:00
|
|
|
products = selectProducts(store.getState());
|
|
|
|
expect(products.length).toBe(0);
|
|
|
|
});
|
2024-06-28 15:04:50 +02:00
|
|
|
});
|