PliWould/components/__tests__/ProductCalculatorSelector-test.tsx

73 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

import { render, fireEvent, screen, act, within } from '@testing-library/react-native';
2024-07-01 15:15:43 +02:00
import { Provider } from 'react-redux';
import ProductCalculatorSelector from '@/components/ProductCalculatorSelector';
2024-07-01 17:05:24 +02:00
import { renderWithProviders } from '@/lib/rendering';
import { Product } from '@/lib/product';
2024-07-01 15:15:43 +02:00
describe('ProductCalculatorSelector', () => {
2024-07-01 17:05:24 +02:00
const mockAreaProduct = new Product(
100,
{ l: 4, w: 8, u: "ft" },
{"name": "area product"},
);
const mockLengthProduct = new Product(
100,
{ l: 4, u: "ft" },
{"name": "length product"},
);
2024-07-01 15:15:43 +02:00
it('renders correctly', () => {
2024-07-01 17:05:24 +02:00
renderWithProviders(
(<ProductCalculatorSelector />),
{
products: [
mockAreaProduct.asObject,
mockLengthProduct.asObject,
],
}
)
2024-07-01 15:15:43 +02:00
2024-07-01 17:05:24 +02:00
expect(screen.getByText('Please select a product')).toBeTruthy();
const label = `${mockAreaProduct.attributes.name} (${mockAreaProduct.pricePerUnitDisplay})`;
expect(screen.getByText(label)).toBeTruthy();
2024-07-01 15:15:43 +02:00
});
2024-07-01 17:05:24 +02:00
it('a product can be selected', () => {
renderWithProviders(
(<ProductCalculatorSelector />),
{
products: [
mockLengthProduct.asObject,
mockAreaProduct.asObject,
]
}
2024-07-01 15:15:43 +02:00
);
2024-07-01 17:05:24 +02:00
expect(screen.getByText('Please select a product')).toBeTruthy();
const areaLabel = `${mockAreaProduct.attributes.name} (${mockAreaProduct.pricePerUnitDisplay})`;
const lengthLabel = `${mockLengthProduct.attributes.name} (${mockLengthProduct.pricePerUnitDisplay})`;
2024-07-01 15:15:43 +02:00
2024-07-01 17:05:24 +02:00
fireEvent.press(screen.getByText(areaLabel));
const lengthInput = screen.getByLabelText("enter length");
const widthInput = screen.getByLabelText("enter length");
expect(lengthInput).toBeTruthy();
expect(widthInput).toBeTruthy();
2024-07-01 15:15:43 +02:00
2024-07-01 17:05:24 +02:00
fireEvent.press(screen.getByText("in"));
2024-07-01 15:15:43 +02:00
2024-07-01 17:05:24 +02:00
act(() => {
fireEvent.changeText(lengthInput, "2");
fireEvent.changeText(widthInput, "4");
});
jest.advanceTimersByTime(3000);
2024-07-01 15:15:43 +02:00
2024-07-01 17:05:24 +02:00
const price = mockAreaProduct.priceFor({l: 2, w: 4, u: "ft"});
const sPrice = price.toLocaleString(undefined, {maximumFractionDigits: 2, minimumFractionDigits: 2,});
const element = screen.getByLabelText("calculated price");
const {getByText} = within(element);
expect(getByText(sPrice)).toBeTruthy();
2024-07-01 15:15:43 +02:00
});
});