write jests tests import { ComponentType, PropsWithChildren, ReactNode } from 'react';…
- User
write jests tests import { ComponentType, PropsWithChildren, ReactNode } from 'react'; import { FieldValues } from 'react-hook-form'; import { OverridedRegisterOptions } from '@/commons/hooks/useComposeController';
/**
- HOC модифицирующий title.
- Добавляет постфикс "*", если включен флаг showHighlightingSymbolForRequiringFields и rules не пусты. */ export const withModifiedTitle = <BaseFieldValue, ComponentProps extends { title?: ReactNode }>( Component: ComponentType, ) => { const WrappedComponent = ({ rules, showHighlightingSymbolForRequiringFields, title, ...otherProps }: PropsWithChildren< ComponentProps & { showHighlightingSymbolForRequiringFields?: boolean; rules?: OverridedRegisterOptions<BaseFieldValue, FormValues>; }
) => { if (!title) { return <Component {...(otherProps as ComponentProps)} />; }
const isRequiredField = rules ? Object.values(rules).length > 0 : false; const modifiedTitle = typeof title === 'string' && showHighlightingSymbolForRequiringFields && isRequiredField ? `${title}*` : title; return <Component {...(otherProps as ComponentProps)} title={modifiedTitle} />;};
WrappedComponent.displayName = Component.displayName || Component.name || 'Component';
return WrappedComponent; };
- Assistant
To write Jest tests for the
withModifiedTitleHigher-Order Component (HOC), we need to test various scenarios, including when the title is present or absent, and when the conditions related torulesandshowHighlightingSymbolForRequiringFieldsaffect the final title.Here's a sample test suite using Jest and React Testing Library:
import React from 'react'; import { render } from '@testing-library/react'; import { withModifiedTitle } from './path/to/your/hoc'; // Adjust the import path accordingly // Create a simple mock component for testing const MockComponent = ({ title }) => <div>{title}</div>; const EnhancedComponent = withModifiedTitle(MockComponent); describe('withModifiedTitle', () => { it('renders the component without modification when title is not provided', () => { const { getByText } = render(<EnhancedComponent />); expect(getByText('')).toBeInTheDocument(); // Assuming title is undefined renders empty text }); it('renders the title as is when title is provided and flags are not set', () => { const { getByText } = render(<EnhancedComponent title="Test Title" />); expect(getByText('Test Title')).toBeInTheDocument(); }); it('adds an asterisk to the title when showHighlightingSymbolForRequiringFields is true and rules are not empty', () => { const rules = { required: true }; // Simulating non-empty rules const { getByText } = render( <EnhancedComponent title="Test Title" showHighlightingSymbolForRequiringFields={true} rules={rules} /> ); expect(getByText('Test Title*')).toBeInTheDocument(); // Title modified with an asterisk }); it('does not modify the title when showHighlightingSymbolForRequiringFields is true but rules are empty', () => { const rules = {}; // Simulating empty rules const { getByText } = render( <EnhancedComponent title="Test Title" showHighlightingSymbolForRequiringFields={true} rules={rules} /> ); expect(getByText('Test Title')).toBeInTheDocument(); // Title remains unchanged }); it('does not modify the title when showHighlightingSymbolForRequiringFields is false', () => { const rules = { required: true }; // Simulating non-empty rules const { getByText } = render( <EnhancedComponent title="Test Title" showHighlightingSymbolForRequiringFields={false} rules={rules} /> ); expect(getByText('Test Title')).toBeInTheDocument(); // Title remains unchanged }); });Explanation:
-
Mock Component: We create a simple
MockComponentthat takes atitleprop and displays it. This will be wrapped by our HOC. -
Testing Different Scenarios:
- No Title Provided: We check that if no title is supplied, the component can still render (assumably showing nothing or empty).
- Title Given: When a title is given without other flags being activated, it should display that title.
- Asterisk Addition: If
showHighlightingSymbolForRequiringFieldsis true and the rules are non-empty, then the title should display with an asterisk. - No Asterisk When Conditions Aren’t Met: If the rules are empty or
showHighlightingSymbolForRequiringFieldsis false, the title should remain unchanged.
Note:
- Adjust paths according to your project structure.
- Depending on how your component is structured (and if it has default props or other behaviors), you might need to adapt the tests further.
- Additionally, you may need to install
@testing-library/reactif it's not already included in your project.
-