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';
|
2024-06-30 18:37:27 +02:00
|
|
|
import { renderWithProviders } from '@/lib/rendering';
|
2024-07-31 19:01:45 +02:00
|
|
|
import { area_t } from '@/lib/dimensions';
|
2024-06-28 04:00:45 +02:00
|
|
|
|
|
|
|
describe('ProductEditorItem', () => {
|
2024-06-30 18:37:27 +02:00
|
|
|
const productName = "Product 1";
|
2024-07-31 19:01:45 +02:00
|
|
|
const mockProduct : Product = {
|
|
|
|
type: "area_rug",
|
|
|
|
dimensions: {
|
|
|
|
l: 1,
|
|
|
|
w: 1,
|
|
|
|
u: "feet",
|
|
|
|
},
|
|
|
|
pricePerUnit: 0.75,
|
|
|
|
attributes: {
|
|
|
|
name: productName,
|
|
|
|
}
|
|
|
|
}
|
2024-06-28 04:00:45 +02:00
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
const onAttributeAdded = jest.fn();
|
2024-06-28 04:00:45 +02:00
|
|
|
const mockOnProductDeleted = jest.fn();
|
2024-06-30 18:37:27 +02:00
|
|
|
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}
|
2024-06-30 18:37:27 +02:00
|
|
|
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', () => {
|
2024-06-30 18:37:27 +02:00
|
|
|
const {store} = renderWithProviders(
|
2024-06-28 04:00:45 +02:00
|
|
|
<ProductEditorItem
|
|
|
|
product={mockProduct}
|
2024-06-30 18:37:27 +02:00
|
|
|
onAttributeAdded={onAttributeAdded}
|
|
|
|
onAttributeDeleted={onAttributeDeleted}
|
|
|
|
onAttributeKeyChanged={onAttributeKeyChanged}
|
|
|
|
onAttributeUpdated={onAttributeUpdated}
|
|
|
|
onProductAdded={onProductAdded}
|
|
|
|
onPriceUpdated={onPriceUpdated}
|
|
|
|
onDimensionsUpdated={onDimensionUpdated}
|
2024-06-28 04:00:45 +02:00
|
|
|
onProductDeleted={mockOnProductDeleted}
|
2024-06-30 18:37:27 +02:00
|
|
|
/>, {
|
|
|
|
products: [mockProduct],
|
|
|
|
}
|
2024-06-28 04:00:45 +02:00
|
|
|
);
|
|
|
|
fireEvent.press(screen.getByText("Product 1"));
|
2024-07-31 19:01:45 +02:00
|
|
|
// expect(screen.getByLabelText("Units")).toBeTruthy();
|
2024-06-30 18:37:27 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|