From 3b8aca62fd51bfddc4cc0d1ac1c144f037d526bb Mon Sep 17 00:00:00 2001 From: VSnehalatha Date: Fri, 14 Apr 2023 11:52:56 +0530 Subject: [PATCH 1/2] create Jest/RTL test for useMoveItemsMutation.js --- src/common/hooks/useMoveItemsMutation.test.js | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/common/hooks/useMoveItemsMutation.test.js diff --git a/src/common/hooks/useMoveItemsMutation.test.js b/src/common/hooks/useMoveItemsMutation.test.js new file mode 100644 index 000000000..cac9e3888 --- /dev/null +++ b/src/common/hooks/useMoveItemsMutation.test.js @@ -0,0 +1,71 @@ +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 { 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, + }; + + return { + ...jest.requireActual('react-intl'), + FormattedMessage: jest.fn(({ id, children }) => { + if (children) { + return children([id]); + } + + return id; + }), + useIntl: () => intl, + injectIntl: (Component) => (props) => , + }; +}); + +const queryClient = new QueryClient(); +const wrapper = ({ children }) => ( + + {children} + +); +const onSuccess = jest.fn(); +const onError = jest.fn(); + +describe('useMoveItemsMutation', () => { + it('Triggering mutate function with variables', async () => { + 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 }); + + expect(result.current.isLoading).toBe(true); + expect(result.current.status).toBe('loading'); + expect(result.current.isIdle).toBe(false); + expect(result.current.variables).toEqual({ itemIds: ['itemId1'] }); + }); + it('Triggering mutateAsync function with nonUpdatedIds is empty', async () => { + const { result } = renderHook(() => useMoveItemsMutation({ onError, onSuccess }), { wrapper }); + + await result.current.mutateAsync(); + + expect(result.current.isSuccess).toBe(true); + expect(result.current.status).toBe('success'); + expect(result.current.isIdle).toBe(false); + expect(result.current.data).toEqual({ nonUpdatedIds: [] }); + }); +}); From 257f8958d90236250a5b72e6228848d0b4651f0c Mon Sep 17 00:00:00 2001 From: VSnehalatha Date: Fri, 14 Apr 2023 15:59:25 +0530 Subject: [PATCH 2/2] Clearing QC test errors --- src/common/hooks/useMoveItemsMutation.test.js | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/common/hooks/useMoveItemsMutation.test.js b/src/common/hooks/useMoveItemsMutation.test.js index cac9e3888..c20b55828 100644 --- a/src/common/hooks/useMoveItemsMutation.test.js +++ b/src/common/hooks/useMoveItemsMutation.test.js @@ -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: [] }); }); });