Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIIN-1722 create Jest/RTL test for useMoveItemsMutation.js #2119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Clearing QC test errors
  • Loading branch information
VSnehalatha committed Apr 14, 2023
commit 257f8958d90236250a5b72e6228848d0b4651f0c
41 changes: 22 additions & 19 deletions src/common/hooks/useMoveItemsMutation.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import '../../../test/jest/__mock__';
import { renderHook } from '@testing-library/react-hooks';
import '../../../test/jest/__mock__/stripesConfig.mock';
import '../../../test/jest/__mock__/stripesComponents.mock';
import '../../../test/jest/__mock__/currencyData.mock';
import { QueryClient, QueryClientProvider } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';
import { useMoveItemsMutation } from './useMoveItemsMutation';

jest.mock('@folio/stripes/core', () => ({
...jest.requireActual('@folio/stripes/core'),
useOkapiKy: jest.fn().mockImplementation(() => ({
post: jest.fn().mockReturnValue({ nonUpdatedIds: [] }),
}))
.mockImplementationOnce(() => ({
post: jest.fn().mockReturnValue({ nonUpdatedIds: ['testId-1'] }),
})),
}));

jest.mock('react-intl', () => {
const intl = {
formatMessage: ({ id }) => id,
@@ -44,28 +33,42 @@ const onSuccess = jest.fn();
const onError = jest.fn();

describe('useMoveItemsMutation', () => {
it('Triggering mutate function with variables', async () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('Data and variables should pass with values', async () => {
const mockPost = jest.fn().mockReturnValue({ nonUpdatedIds: ['testId-1'] });
useOkapiKy.mockClear().mockReturnValue({
post: mockPost,
});
const itemIds = ['itemId1'];
const { result } = renderHook(() => useMoveItemsMutation({ onError, onSuccess }), { wrapper });
expect(result.current.isLoading).toBe(false);
expect(result.current.status).toBe('idle');
expect(result.current.isIdle).toBe(true);

await result.current.mutate({ itemIds });
await result.current.mutateAsync({ itemIds });

expect(result.current.isLoading).toBe(true);
expect(result.current.status).toBe('loading');
expect(result.current.isSuccess).toBe(true);
expect(result.current.status).toBe('success');
expect(result.current.isIdle).toBe(false);
expect(result.current.data).toEqual({ nonUpdatedIds: ['testId-1'] });
expect(result.current.variables).toEqual({ itemIds: ['itemId1'] });
});
it('Triggering mutateAsync function with nonUpdatedIds is empty', async () => {
it('Data and variables should pass with empty values', async () => {
const mockPost = jest.fn().mockReturnValue({ nonUpdatedIds: [] });
useOkapiKy.mockClear().mockReturnValue({
post: mockPost,
});
const itemIds = [];
const { result } = renderHook(() => useMoveItemsMutation({ onError, onSuccess }), { wrapper });

await result.current.mutateAsync();
await result.current.mutateAsync({ itemIds });

expect(result.current.isSuccess).toBe(true);
expect(result.current.status).toBe('success');
expect(result.current.isIdle).toBe(false);
expect(result.current.data).toEqual({ nonUpdatedIds: [] });
expect(result.current.variables).toEqual({ itemIds: [] });
});
});