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 all commits
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
74 changes: 74 additions & 0 deletions src/common/hooks/useMoveItemsMutation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import '../../../test/jest/__mock__';
import { renderHook } from '@testing-library/react-hooks';
import { QueryClient, QueryClientProvider } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';
import { useMoveItemsMutation } from './useMoveItemsMutation';

jest.mock('react-intl', () => {
const intl = {
formatMessage: ({ id }) => id,
};

return {
...jest.requireActual('react-intl'),
FormattedMessage: jest.fn(({ id, children }) => {
if (children) {
return children([id]);
}

return id;
}),
useIntl: () => intl,
injectIntl: (Component) => (props) => <Component {...props} intl={intl} />,
};
});

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);
const onSuccess = jest.fn();
const onError = jest.fn();

describe('useMoveItemsMutation', () => {
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.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: ['testId-1'] });
expect(result.current.variables).toEqual({ itemIds: ['itemId1'] });
});
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({ 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: [] });
});
});