PliWould/components/__tests__/ProductCalculatorSelector-test.tsx

65 lines
2.2 KiB
TypeScript
Raw 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, pricePerUnitDisplay, productPriceFor } from '@/lib/product';
2024-07-01 15:15:43 +02:00
import initialProducts from '@/__fixtures__/initialProducts';
2024-07-01 17:05:24 +02:00
const mockAreaProduct = initialProducts.find(p => 'w' in p.dimensions ) as Product
const mockLengthProduct = initialProducts.find(p => (!('w' in p.dimensions)) ) as Product
2024-07-01 17:05:24 +02:00
describe('ProductCalculatorSelector', () => {
2024-07-01 15:15:43 +02:00
it('renders correctly', () => {
2024-07-01 17:05:24 +02:00
renderWithProviders(
(<ProductCalculatorSelector />),
{
products: [
mockAreaProduct,
mockLengthProduct,
2024-07-01 17:05:24 +02:00
],
}
)
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} (${pricePerUnitDisplay(mockAreaProduct)})`;
2024-07-01 17:05:24 +02:00
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,
mockAreaProduct,
2024-07-01 17:05:24 +02:00
]
}
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} (${pricePerUnitDisplay(mockAreaProduct)})`;
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
const price = productPriceFor(mockAreaProduct, {l: 2, w: 4, u: "ft"})
2024-07-01 17:05:24 +02:00
const sPrice = price.toLocaleString(undefined, {maximumFractionDigits: 2, minimumFractionDigits: 2,});
const element = screen.getByLabelText("calculated price");
const {getByText} = within(element);
expect(getByText(/\$.*0.*\.10/)).toBeTruthy();
2024-07-01 15:15:43 +02:00
});
});