Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Dec 18, 2024
1 parent b6a98fe commit 7c23fdb
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';

import { render, screen } from '@testing-library/react';
import stripesFinalForm from '@folio/stripes/final-form';

import { FieldClaimingDate } from './FieldClaimingDate';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { DelayClaimActionMenuItem } from './DelayClaimActionMenuItem';

const defaultProps = {
onClick: jest.fn(),
};

const renderComponent = (props = {}) => render(
<DelayClaimActionMenuItem
{...defaultProps}
{...props}
/>,
);

describe('DelayClaimActionMenuItem', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should call onClick when button is clicked', async () => {
renderComponent();

await userEvent.click(screen.getByTestId('delay-claim-button'));

expect(defaultProps.onClick).toHaveBeenCalledTimes(1);
});

it('should be disabled when disabled prop is true', () => {
renderComponent({ disabled: true });

expect(screen.getByTestId('delay-claim-button')).toBeDisabled();
});

it('should not call onClick when button is disabled and clicked', async () => {
renderComponent({ disabled: true });

await userEvent.click(screen.getByTestId('delay-claim-button'));

expect(defaultProps.onClick).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { MarkUnreceivableActionMenuItem } from './MarkUnreceivableActionMenuItem';

const defaultProps = {
onClick: jest.fn(),
disabled: false,
};

const renderComponent = (props = {}) => render(
<MarkUnreceivableActionMenuItem
{...defaultProps}
{...props}
/>,
);

describe('MarkUnreceivableActionMenuItem', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should render the button', () => {
renderComponent();

expect(screen.getByTestId('unreceivable-button')).toBeInTheDocument();
});

it('should call onClick when button is clicked', async () => {
renderComponent();

await userEvent.click(screen.getByTestId('unreceivable-button'));

expect(defaultProps.onClick).toHaveBeenCalledTimes(1);
});

it('should disable the button when disabled prop is true', () => {
renderComponent({ disabled: true });

expect(screen.getByTestId('unreceivable-button')).toBeDisabled();
});

it('should enable the button when disabled prop is false', () => {
renderComponent({ disabled: false });

expect(screen.getByTestId('unreceivable-button')).toBeEnabled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import {
render,
screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { SendClaimActionMenuItem } from './SendClaimActionMenuItem';

const defaultProps = {
onClick: jest.fn(),
};

const renderComponent = (props = {}) => render(
<SendClaimActionMenuItem
{...defaultProps}
{...props}
/>,
);

describe('SendClaimActionMenuItem', () => {
it('should render the button', () => {
renderComponent();

expect(screen.getByTestId('send-claim-button')).toBeInTheDocument();
});

it('should call onClick when button is clicked', async () => {
renderComponent();

await userEvent.click(screen.getByTestId('send-claim-button'));

expect(defaultProps.onClick).toHaveBeenCalled();
});

it('should disable the button when disabled prop is true', () => {
renderComponent({ disabled: true });

expect(screen.getByTestId('send-claim-button')).toBeDisabled();
});

it('should enable the button when disabled prop is false', () => {
renderComponent({ disabled: false });

expect(screen.getByTestId('send-claim-button')).not.toBeDisabled();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { render, screen } from '@testing-library/react';
import {
render,
screen,
} from '@testing-library/react';
import user from '@testing-library/user-event';
import { MemoryRouter } from 'react-router-dom';

Expand Down Expand Up @@ -41,10 +44,12 @@ describe('SendClaimsModal', () => {
const saveBtn = screen.getByRole('button', { name: 'stripes-acq-components.FormFooter.save' });

await user.click(saveBtn);

expect(screen.getByText('stripes-acq-components.validation.required')).toBeInTheDocument();

await user.type(screen.getByPlaceholderText(FORMAT), today.format(FORMAT));
await user.click(saveBtn);

expect(screen.getByText('stripes-acq-components.validation.dateAfter')).toBeInTheDocument();
});

Expand Down
59 changes: 59 additions & 0 deletions lib/claiming/hooks/useClaimsSend/useClaimsSend.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import { renderHook } from '@testing-library/react-hooks';
import {
QueryClient,
QueryClientProvider,
} from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';

import { SEND_CLAIMS_API } from '../../../constants';
import { useClaimsSend } from './useClaimsSend';

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

describe('useClaimsSend', () => {
const kyPostMock = jest.fn();

beforeEach(() => {
useOkapiKy.mockReturnValue({
post: kyPostMock,
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should call ky.post with correct arguments when sendClaims is called', async () => {
const { result } = renderHook(() => useClaimsSend(), { wrapper });
const data = { claim: 'test' };

kyPostMock.mockReturnValue({
json: jest.fn().mockResolvedValue({}),
});

await result.current.sendClaims({ data });

expect(kyPostMock).toHaveBeenCalledWith(SEND_CLAIMS_API, { json: data });
});

it('should handle errors when sendClaims is called', async () => {
const { result } = renderHook(() => useClaimsSend(), { wrapper });

const data = { claim: 'test' };
const error = new Error('Failed to send claims');

kyPostMock.mockReturnValue({
json: jest.fn().mockRejectedValue(error),
});

await expect(result.current.sendClaims({ data })).rejects.toThrow(error);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Developed collaboratively using AI (GitHub Copilot) */

import { renderHook } from '@testing-library/react-hooks';
import {
QueryClient,
QueryClientProvider,
} from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';

import { PIECES_BATCH_STATUS_API } from '../../constants';
import { usePiecesStatusBatchUpdate } from './usePiecesStatusBatchUpdate';

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

describe('usePiecesStatusBatchUpdate', () => {
const kyMock = {
put: jest.fn(() => ({
json: jest.fn(),
})),
};

beforeEach(() => {
useOkapiKy.mockReturnValue(kyMock);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should call ky.put with correct arguments', async () => {
const { result } = renderHook(() => usePiecesStatusBatchUpdate(), { wrapper });
const data = { status: 'Received' };

await result.current.updatePiecesStatus({ data });

expect(kyMock.put).toHaveBeenCalledWith(PIECES_BATCH_STATUS_API, { json: data });
});
});

0 comments on commit 7c23fdb

Please sign in to comment.