Generated component unittests.

This commit is contained in:
Jordan 2024-07-01 06:15:43 -07:00
parent fb68beb1b3
commit 76fe4eb34a
4 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,23 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { AreaInput } from '../AreaInput';
describe('AreaInput', () => {
it('renders correctly', () => {
const { getByPlaceholderText } = render(<AreaInput units="foot" />);
const lengthInput = getByPlaceholderText('Length');
const widthInput = getByPlaceholderText('Width');
expect(lengthInput).toBeTruthy();
expect(widthInput).toBeTruthy();
});
it('calls onValueSet when a value is entered', () => {
const onValueSetMock = jest.fn();
const { getByPlaceholderText } = render(
<AreaInput units="foot" onValueSet={onValueSetMock} />
);
const lengthInput = getByPlaceholderText('Length');
fireEvent.changeText(lengthInput, '10');
expect(onValueSetMock).toHaveBeenCalledTimes(1);
});
});

View File

@ -0,0 +1,18 @@
import { render, fireEvent } from '@testing-library/react-native';
import { MeasurementInput } from '../MeasurementInput';
describe('MeasurementInput', () => {
it('renders correctly', () => {
const { getByPlaceholderText } = render(<MeasurementInput units="foot" defaultValue={10} />);
const input = getByPlaceholderText('Enter measurement');
expect(input).toBeTruthy();
});
it('calls onValueSet when value is changed', () => {
const mockOnValueSet = jest.fn();
const { getByPlaceholderText } = render(<MeasurementInput units="foot" defaultValue={10} onValueSet={mockOnValueSet} />);
const input = getByPlaceholderText('Enter measurement');
fireEvent.changeText(input, '20');
expect(mockOnValueSet).toHaveBeenCalledWith({ l: 20, u: 'foot' });
});
});

View File

@ -0,0 +1,42 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { Provider } from 'react-redux';
import { store } from '@/app/store';
import ProductCalculatorSelector from '@/components/ProductCalculatorSelector';
describe('ProductCalculatorSelector', () => {
it('renders correctly', () => {
const { getByText } = render(
<Provider store={store}>
<ProductCalculatorSelector />
</Provider>
);
expect(getByText('Please select a product')).toBeTruthy();
});
it('updates price when measurement is set', () => {
const { getByTestId, getByText } = render(
<Provider store={store}>
<ProductCalculatorSelector />
</Provider>
);
// Assume there is a product with a priceFor function that returns 100
const product = {
priceFor: jest.fn().mockReturnValue(100),
};
// Assume there are units for measurement
const units = ['cm', 'in'];
// Assume there is a measurement input
const measurementInput = getByTestId('measurement-input');
// Simulate user input
fireEvent.changeText(measurementInput, '10');
// Check if the price has been updated
expect(getByText('Price: 100')).toBeTruthy();
});
});

View File

@ -0,0 +1,29 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import UnitChooser from "../UnitChooser";
import { Length } from 'safe-units';
describe('UnitChooser', () => {
const mockOnChoicePressed = jest.fn();
const choices = ['foot', 'inch'] as Length [];
it('renders correctly', () => {
const { getByText } = render(
<UnitChooser choices={choices} onChoicePressed={mockOnChoicePressed} />
);
choices.forEach(choice => {
expect(getByText(choice)).toBeTruthy();
});
});
it('calls onChoicePressed when a button is pressed', () => {
const { getByText } = render(
<UnitChooser choices={choices} onChoicePressed={mockOnChoicePressed} />
);
fireEvent.press(getByText(choices[0]));
expect(mockOnChoicePressed).toHaveBeenCalledWith(choices[0]);
});
});