write jest tests import type { DocumentRapidId } from '@sber-pprb-efsdf/rapid-ui-core';
import { useDocumentStore } from '@sber-pprb-efsdf/rapid-ui-core';
import { useShallow } from 'zustand/shallow';
import type { DocumentActionButton } from 'rapid−ufs′;rapid-ufs';
Assistant
User
write tests jest import { useCallback } from 'react';
import type { DropdownControls } from '@atomic-ui/react';
import {
Box,
Button,
ButtonLabel,
IconChevronDownSm24,
List,
RightButtonIcon,
} from '@atomic-ui/react';
import type { DocumentRapidId } from '@sber-pprb-efsdf/rapid-ui-core';
import { Dropdown } from '$ufs-common';
import { usePrkoFormReadonly } from '$prko-form/entities/prkoForm';
import { useProductDocActions } from '../../../../../model/hooks/useProductDocActions';
type ProductActionsProps = {
docId: DocumentRapidId;
};
To write Jest tests for the useProductDocActions hook, you'll need to set up your test environment and mock the required dependencies appropriately. Below is an example of how you might structure your Jest tests for the useProductDocActions hook:
Import necessary libraries and modules.
Mock the required dependencies such as useDocumentStore and useOpenCopyProductModal.
Write the test cases to verify the behavior of the hook.
Here's an example of how you can write Jest tests for your useProductDocActions hook:
javascript
import { renderHook, act } from '@testing-library/react-hooks';
import { useProductDocActions } from './path/to/your/hook';
import { useDocumentStore } from '@sber-pprb-efsdf/rapid-ui-core';
import { useOpenCopyProductModal } from '../thunks/openCopyProductModal';
// Mock the dependencies
jest.mock('@sber-pprb-efsdf/rapid-ui-core', () => ({
useDocumentStore: jest.fn(),
}));
jest.mock('../thunks/openCopyProductModal', () => ({
useOpenCopyProductModal: jest.fn(),
}));
describe('useProductDocActions', () => {
const mockProductDocId = 'some-doc-id';
beforeEach(() => {
// Reset mocks before each test
jest.clearAllMocks();
});
it('should return correct action buttons for COPY action', () => {
const mockOpenCopyProductModal = jest.fn();
useOpenCopyProductModal.mockReturnValue(mockOpenCopyProductModal);
// Mock return value of useDocumentStore
useDocumentStore.mockReturnValue([
{ code: 'COPY' },
{ code: 'DELETE' },
{ code: 'CREATE_SUBPRODUCT' }
]);
const { result } = renderHook(() => useProductDocActions({ productDocId: mockProductDocId }));
expect(result.current).toHaveLength(3);
const copyAction = result.current[0];
expect(copyAction.code).toBe('COPY');
expect(copyAction.disabled).toBe(false);
// Simulate clicking the copy action
act(() => {
copyAction.onClick();
});
expect(mockOpenCopyProductModal).toHaveBeenCalledWith({ productDocId: mockProductDocId });
});
it('should return base props for DELETE action', () => {
const mockOpenCopyProductModal = jest.fn();
useOpenCopyProductModal.mockReturnValue(mockOpenCopyProductModal);
useDocumentStore.mockReturnValue([
{ code: 'DELETE' }
]);
const { result } = renderHook(() => useProductDocActions({ productDocId: mockProductDocId }));
const deleteAction = result.current[0];
expect(deleteAction.code).toBe('DELETE');
expect(deleteAction.disabled).toBe(false);
expect(deleteAction.onClick).toBeDefined(); // Ensure there is an onClick prop but it should not do anything yet
});
it('should return base props for CREATE_SUBPRODUCT action', () => {
const mockOpenCopyProductModal = jest.fn();
useOpenCopyProductModal.mockReturnValue(mockOpenCopyProductModal);
useDocumentStore.mockReturnValue([
{ code: 'CREATE_SUBPRODUCT' }
]);
const { result } = renderHook(() => useProductDocActions({ productDocId: mockProductDocId }));
const createSubProductAction = result.current[0];
expect(createSubProductAction.code).toBe('CREATE_SUBPRODUCT');
expect(createSubProductAction.disabled).toBe(false);
expect(createSubProductAction.onClick).toBeDefined(); // Ensure there is an onClick prop but it should not do anything yet
});
// Add more tests as needed for other scenarios
});
Explanation:
We use jest.mock to mock dependencies that the hook relies upon.
We use the renderHook utility from @testing-library/react-hooks to test our custom hook.
We verify:
The correct number of action buttons is returned.
The behavior when clicking the "COPY" action (i.e., openCopyProductModal is called with the correct parameter).
The DELETE and CREATE_SUBPRODUCT buttons return their base props correctly.
This provides a good foundation for testing functionality and ensuring your hook behaves as expected. You can add more tests for other actions and scenarios as needed.
To write Jest tests for the ProductActions component, you'll need to simulate the component's props, mock dependencies, and verify the rendering and behavior based on the given props. Below, I provide an example of how to set up these tests using @testing-library/react for rendering and interacting with the component.
Setting Up Tests for ProductActions Component
Make sure you have the necessary dependencies installed:
bash
npm install @testing-library/react @testing-library/jest-dom jest
Example Jest Tests for the ProductActions Component
Here's how you might structure the tests:
javascript
// ProductActions.test.js
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { ProductActions } from './ProductActions'; // Adjust the import path as necessary
import { usePrkoFormReadonly } from '$prko-form/entities/prkoForm';
import { useProductDocActions } from '../../../../../model/hooks/useProductDocActions';
import { Dropdown } from '$ufs-common';
// Mock the dependencies
jest.mock('$prko-form/entities/prkoForm');
jest.mock('../../../../../model/hooks/useProductDocActions');
jest.mock('$ufs-common', () => ({
Dropdown: jest.fn(({ content }) => <div>{content({ close: jest.fn() })}</div>), // Mocked dropdown for testing
}));
describe('ProductActions', () => {
const mockDocId = 'test-doc-id';
const mockProductActions = [
{ name: 'Copy', code: 'COPY', disabled: false, onClick: jest.fn() },
{ name: 'Delete', code: 'DELETE', disabled: false, onClick: jest.fn() },
];
beforeEach(() => {
jest.clearAllMocks();
usePrkoFormReadonly.mockReturnValue(false); // Set readonly state to false
useProductDocActions.mockReturnValue(mockProductActions);
});
it('renders the dropdown when not readonly and product actions are available', () => {
render(<ProductActions docId={mockDocId} />);
// Assert the button is rendered
expect(screen.getByRole('button', { name: /Действия/i })).toBeInTheDocument();
});
it('does not render the dropdown when readonly', () => {
usePrkoFormReadonly.mockReturnValue(true); // Set readonly state to true
const { container } = render(<ProductActions docId={mockDocId} />);
// Assert that the dropdown is not rendered
expect(container).toBeEmptyDOMElement();
});
it('executes the onClick function when an action is clicked', () => {
render(<ProductActions docId={mockDocId} />);
// Open the dropdown content
fireEvent.click(screen.getByRole('button', { name: /Действия/i }));
// Click the Copy action
fireEvent.click(screen.getByText('Copy'));
// Assert that the onClick function for the Copy action has been called
expect(mockProductActions[0].onClick).toHaveBeenCalled();
});
it('closes the dropdown after clicking an action', () => {
const closeMock = jest.fn();
Dropdown.mockImplementation(({ content }) => <div>{content({ close: closeMock })}</div>);
render(<ProductActions docId={mockDocId} />);
// Open the dropdown content
fireEvent.click(screen.getByRole('button', { name: /Действия/i }));
// Click the Delete action
fireEvent.click(screen.getByText('Delete'));
// Assert the close function was called
expect(closeMock).toHaveBeenCalled();
});
});
Explanation:
Mocks:
We mock usePrkoFormReadonly and useProductDocActions so that we can control their return values in the tests.
We also mock the Dropdown component to isolate the ProductActions component during testing.
Tests:
Rendering Tests: We check if the dropdown button renders when it's not read-only and when there are document actions available.
Behavior Tests: We simulate clicks on the dropdown button and the action items to ensure the appropriate handlers are called.
State Tests: We test the component's behavior when it's in a read-only state, asserting that it doesn't render the dropdown.
This setup allows you to comprehensively test the component's rendering and behavior based on its input props and internal logic. You can expand upon these tests based on additional features or edge cases needed for your application.