2024-07-01 21:23:45 +02:00
|
|
|
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';
|
2024-08-10 19:06:25 +02:00
|
|
|
import { Product, pricePerUnitDisplay, productPriceFor } from '@/lib/product';
|
2024-07-01 15:15:43 +02:00
|
|
|
|
2024-08-10 19:06:25 +02:00
|
|
|
import initialProducts from '@/__fixtures__/initialProducts';
|
2024-07-01 17:05:24 +02:00
|
|
|
|
2024-08-10 19:06:25 +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
|
|
|
|
2024-08-10 19:06:25 +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: [
|
2024-08-10 19:06:25 +02:00
|
|
|
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();
|
2024-08-10 19:06:25 +02:00
|
|
|
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: [
|
2024-08-10 19:06:25 +02:00
|
|
|
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();
|
2024-08-10 19:06:25 +02:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
jest.advanceTimersByTime(3000);
|
2024-07-01 15:15:43 +02:00
|
|
|
|
2024-08-10 19:06:25 +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,});
|
2024-07-01 21:23:45 +02:00
|
|
|
const element = screen.getByLabelText("calculated price");
|
|
|
|
const {getByText} = within(element);
|
2024-08-10 19:06:25 +02:00
|
|
|
expect(getByText(/\$.*0.*\.10/)).toBeTruthy();
|
2024-07-01 15:15:43 +02:00
|
|
|
});
|
|
|
|
});
|