import type { ComponentType } from 'react'; import {… · The Chat Archive
Explore / Programming import type { ComponentType } from 'react'; import { useAttributesFormExtraArgs } from… Save Share Download ▾ Reportimport type { ComponentType } from 'react';
import { useAttributesFormExtraArgs } from '@efsdf-ui/rapid-ufs';
import type { RateIndex } from '$entities/rates';
import { activePeriodIndexSelector, useActivePeriodStore } from '../store';
export interface WithRateParametersProps {
rateIndex: number;
activePeriodIndex: number;
}
type WrappedComponentProps = Omit<
ComponentProps,
'rateIndex' | 'activePeriodIndex'
;
export const withRateParameters = <ComponentProps,>(Component: ComponentType) => {
const WrappedComponent = (props: WrappedComponentProps) => {
const { rateIndex } = useAttributesFormExtraArgs();
const activePeriodIndex = useActivePeriodStore(activePeriodIndexSelector);
code Copy code
return (
<Component
{...(props as ComponentProps)}
rateIndex={rateIndex}
activePeriodIndex={activePeriodIndex}
/>
);
};
WrappedComponent.displayName = Component.displayName || Component.name || 'Component';
return WrappedComponent;
};
how mock jest
Assistant2024-07-19 17:17 UTC Copy response
To mock Jest with TypeScript for the withRateParameters higher-order component, you can use the following approach:
Create a mock for the required dependencies:
typescript Copy code
jest.mock('@efsdf-ui/rapid-ufs', () => ({
useAttributesFormExtraArgs: jest.fn(),
}));
jest.mock('../store', () => ({
activePeriodIndexSelector: jest.fn(),
useActivePeriodStore: jest.fn(),
}));
Create a mock implementation for the and functions:
ok, how mock this hoc here import { memo } from 'react';
import { useFormContext } from 'react-hook-form';
import { Select, useFormValueSelector } from '@sber-efsdf-ui/ufs-form-components';
import { Box, Flex, Text } from '@atomic-ui/react';
Show full message · 82 lines Assistant2024-07-19 17:18 UTC Copy response
To mock the higher-order component withRateParameters and test the component BaseIndicatorParams, you can follow these steps:
Mock the required dependencies:
Copy first prompt Copy first response
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 2 (4 messages)
Language en
Conversation date Jul 19, 2024
Added to archive Nov 16, 2026
ID 01m7h41rs9x93ycq8fsf46k4qfSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: e28f23da17b5ccc5a7f2cc319f222ad2
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
useAttributesFormExtraArgs
useActivePeriodStore
typescript Copy code
import { RateIndex } from '$entities/rates';
const mockUseAttributesFormExtraArgs = jest.fn().mockReturnValue({ rateIndex: 1 });
const mockUseActivePeriodStore = jest.fn().mockReturnValue(2);
jest.mock('@efsdf-ui/rapid-ufs', () => ({
useAttributesFormExtraArgs: mockUseAttributesFormExtraArgs,
}));
jest.mock('../store', () => ({
activePeriodIndexSelector: jest.fn(),
useActivePeriodStore: mockUseActivePeriodStore,
}));
Write your test case with the mocked dependencies:
typescript Copy code
import { render } from '@testing-library/react';
import { withRateParameters } from './withRateParameters';
describe('withRateParameters', () => {
it('passes rateIndex and activePeriodIndex to the wrapped component', () => {
const MockComponent = jest.fn();
const ComponentWithRateParameters = withRateParameters(MockComponent);
render(<ComponentWithRateParameters />);
expect(MockComponent).toHaveBeenCalledWith({ rateIndex: 1, activePeriodIndex: 2 });
});
});
Run your tests using Jest:
This will mock the required dependencies for the withRateParameters higher-order component and allow you to test its behavior in isolation.
import type {
BaseIndicatorTypeLabel,
InterestRatesFormState,
TermIndexesType,
} from '$entities/rates';
import type { WithRateParametersProps } from '../../../model';
import { baseIndicatorSelector, withRateParameters } from '../../../model';
interface BaseIndicatorParamsProps extends WithRateParametersProps {
baseIndicatorKindItems: Labeled[];
termIndexTypeItems: Labeled[];
defaultValue?: Labeled;
disabled?: boolean;
}
export const EnhancedBaseIndicatorParams = memo(
({
baseIndicatorKindItems,
termIndexTypeItems,
defaultValue,
activePeriodIndex,
rateIndex,
disabled = false,
}: BaseIndicatorParamsProps) => {
const { resetField } = useFormContext();
code Copy code
const { kind } = useFormValueSelector(
(formState: InterestRatesFormState) =>
baseIndicatorSelector({ formState, activePeriodIndex, rateIndex }),
[activePeriodIndex, rateIndex],
);
const handleChange = () => {
resetField(
`INTEREST_RATES.${rateIndex}.periods.${activePeriodIndex}.baseIndicator.termIndex`,
);
};
return (
<Flex direction="column" gap={4} w="50%">
<Text fontWeight="semibold" typography="16_24">
Базовый индикатор (БИ)
</Text>
<Box w="full">
<Select<InterestRatesFormState, Labeled<BaseIndicatorTypeLabel>>
title="Наименование индикатора"
placeholder="Наименование индикатора"
fieldName={`INTEREST_RATES.${rateIndex}.periods.${activePeriodIndex}.baseIndicator.kind`}
items={baseIndicatorKindItems}
labelExtractor={(item) => item.name}
onChange={handleChange}
defaultValue={defaultValue}
showClearIcon={false}
disabled={disabled}
/>
</Box>
{kind && kind?.id !== 'CBR_KEY_RATE' && (
<Box w="full">
<Select<InterestRatesFormState, Labeled<TermIndexesType>>
title="Индекс срока"
placeholder="Индекс срока"
fieldName={`INTEREST_RATES.${rateIndex}.periods.${activePeriodIndex}.baseIndicator.termIndex`}
items={termIndexTypeItems}
labelExtractor={(item) => item.name}
showClearIcon={false}
/>
</Box>
)}
</Flex>
);
},
export const BaseIndicatorParams = withRateParameters(EnhancedBaseIndicatorParams);
jest.mock('react', () => ({
...jest.requireActual('react'),
memo: (component) => component,
}));
jest.mock('react-hook-form', () => ({
useFormContext: jest.fn(),
}));
jest.mock('@sber-efsdf-ui/ufs-form-components', () => ({
Select: jest.fn(),
useFormValueSelector: jest.fn(),
}));
jest.mock('@atomic-ui/react', () => ({
Box: jest.fn(),
Flex: jest.fn(),
Text: jest.fn(),
}));
jest.mock('$entities/rates', () => ({
baseIndicatorSelector: jest.fn(),
withRateParameters: jest.fn(),
}));
Write your test case for BaseIndicatorParams component:
typescript Copy code
import { render } from '@testing-library/react';
import { BaseIndicatorParams } from './BaseIndicatorParams';
describe('BaseIndicatorParams', () => {
it('renders base indicator component with rate parameters', () => {
const baseIndicatorKindItems = [{ id: 1, name: 'Item 1' }];
const termIndexTypeItems = [{ id: 1, name: 'Term Item' }];
const { getByText } = render(
<BaseIndicatorParams
baseIndicatorKindItems={baseIndicatorKindItems}
termIndexTypeItems={termIndexTypeItems}
activePeriodIndex={0}
rateIndex={0}
/>
);
expect(getByText('Базовый индикатор (БИ)')).toBeInTheDocument();
expect(getByText('Наименование индикатора')).toBeInTheDocument();
expect(getByText('Индекс срока')).toBeInTheDocument();
});
});
Run your tests using Jest:
This setup mocks the dependencies for the higher-order component and tests the BaseIndicatorParams component with the required props.
Not yet included in a published release.