PliWould/components/__tests__/ProductEditorItem-test.tsx

77 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-06-28 04:00:45 +02:00
import React from 'react';
import { render, fireEvent, screen } from '@testing-library/react-native';
import { ProductEditorItem } from '../ProductEditorItem';
import { Product } from '@/lib/product';
import { area } from 'enheter';
import { renderWithProviders } from '@/lib/rendering';
2024-06-28 04:00:45 +02:00
describe('ProductEditorItem', () => {
const productName = "Product 1";
2024-06-28 04:00:45 +02:00
const mockProduct = new Product(
25,
{l: 4, u: 'feet'},
{"name": productName},
2024-06-28 04:00:45 +02:00
)
const onAttributeAdded = jest.fn();
2024-06-28 04:00:45 +02:00
const mockOnProductDeleted = jest.fn();
const onAttributeDeleted = jest.fn();
const onAttributeKeyChanged = jest.fn();
const onAttributeUpdated = jest.fn();
const onProductAdded = jest.fn();
const onPriceUpdated = jest.fn();
const onDimensionUpdated = jest.fn();
2024-06-28 04:00:45 +02:00
it('renders correctly', () => {
render(
<ProductEditorItem
product={mockProduct}
onAttributeAdded={onAttributeAdded}
onAttributeDeleted={onAttributeDeleted}
onAttributeKeyChanged={onAttributeKeyChanged}
onAttributeUpdated={onAttributeUpdated}
onProductAdded={onProductAdded}
onPriceUpdated={onPriceUpdated}
onDimensionsUpdated={onDimensionUpdated}
2024-06-28 04:00:45 +02:00
onProductDeleted={mockOnProductDeleted}
/>
);
expect(screen.getAllByText('Product 1').length).toEqual(1);
});
it('calls onProductUpdated when TouchableHighlight is pressed', () => {
const {store} = renderWithProviders(
2024-06-28 04:00:45 +02:00
<ProductEditorItem
product={mockProduct}
onAttributeAdded={onAttributeAdded}
onAttributeDeleted={onAttributeDeleted}
onAttributeKeyChanged={onAttributeKeyChanged}
onAttributeUpdated={onAttributeUpdated}
onProductAdded={onProductAdded}
onPriceUpdated={onPriceUpdated}
onDimensionsUpdated={onDimensionUpdated}
2024-06-28 04:00:45 +02:00
onProductDeleted={mockOnProductDeleted}
/>, {
products: [mockProduct],
}
2024-06-28 04:00:45 +02:00
);
fireEvent.press(screen.getByText("Product 1"));
expect(screen.getByLabelText("units")).toBeTruthy();
expect(screen.getByLabelText("Edit Key")).toBeTruthy();
expect(screen.getAllByLabelText("Edit Value").length).toEqual(1);
// Now start modifying the properties.
fireEvent.changeText(screen.getByLabelText("price per unit"), "40.00");
expect(onPriceUpdated).toHaveBeenCalled();
fireEvent.changeText(screen.getByLabelText("length"), "12");
expect(onDimensionUpdated).toHaveBeenCalled();
fireEvent.changeText(screen.getByLabelText("width"), "12");
expect(onDimensionUpdated).toHaveBeenCalled();
fireEvent.press(screen.getByLabelText("delete product"));
expect(mockOnProductDeleted).toHaveBeenCalled();
2024-06-28 04:00:45 +02:00
});
});