From 76fe4eb34abef4f532b966552345ff7ea51b54a9 Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 1 Jul 2024 06:15:43 -0700 Subject: [PATCH] Generated component unittests. --- components/__tests__/AreaInput-test.tsx | 23 ++++++++++ .../__tests__/MeasurementInput-test.tsx | 18 ++++++++ .../ProductCalculatorSelector-test.tsx | 42 +++++++++++++++++++ components/__tests__/UnitChooser-test.tsx | 29 +++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 components/__tests__/AreaInput-test.tsx create mode 100644 components/__tests__/MeasurementInput-test.tsx create mode 100644 components/__tests__/ProductCalculatorSelector-test.tsx create mode 100644 components/__tests__/UnitChooser-test.tsx diff --git a/components/__tests__/AreaInput-test.tsx b/components/__tests__/AreaInput-test.tsx new file mode 100644 index 0000000..c7418c1 --- /dev/null +++ b/components/__tests__/AreaInput-test.tsx @@ -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(); + 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( + + ); + const lengthInput = getByPlaceholderText('Length'); + fireEvent.changeText(lengthInput, '10'); + expect(onValueSetMock).toHaveBeenCalledTimes(1); + }); +}); diff --git a/components/__tests__/MeasurementInput-test.tsx b/components/__tests__/MeasurementInput-test.tsx new file mode 100644 index 0000000..a69ce1a --- /dev/null +++ b/components/__tests__/MeasurementInput-test.tsx @@ -0,0 +1,18 @@ +import { render, fireEvent } from '@testing-library/react-native'; +import { MeasurementInput } from '../MeasurementInput'; + +describe('MeasurementInput', () => { + it('renders correctly', () => { + const { getByPlaceholderText } = render(); + const input = getByPlaceholderText('Enter measurement'); + expect(input).toBeTruthy(); + }); + + it('calls onValueSet when value is changed', () => { + const mockOnValueSet = jest.fn(); + const { getByPlaceholderText } = render(); + const input = getByPlaceholderText('Enter measurement'); + fireEvent.changeText(input, '20'); + expect(mockOnValueSet).toHaveBeenCalledWith({ l: 20, u: 'foot' }); + }); +}); diff --git a/components/__tests__/ProductCalculatorSelector-test.tsx b/components/__tests__/ProductCalculatorSelector-test.tsx new file mode 100644 index 0000000..06248f4 --- /dev/null +++ b/components/__tests__/ProductCalculatorSelector-test.tsx @@ -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( + + + + ); + + expect(getByText('Please select a product')).toBeTruthy(); + }); + + it('updates price when measurement is set', () => { + const { getByTestId, getByText } = render( + + + + ); + + // 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(); + }); +}); diff --git a/components/__tests__/UnitChooser-test.tsx b/components/__tests__/UnitChooser-test.tsx new file mode 100644 index 0000000..8be883a --- /dev/null +++ b/components/__tests__/UnitChooser-test.tsx @@ -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( + + ); + + choices.forEach(choice => { + expect(getByText(choice)).toBeTruthy(); + }); + }); + + it('calls onChoicePressed when a button is pressed', () => { + const { getByText } = render( + + ); + + fireEvent.press(getByText(choices[0])); + + expect(mockOnChoicePressed).toHaveBeenCalledWith(choices[0]); + }); +});