From d9626d03dc398ddc8a7cb8da676ae23a93ce8c98 Mon Sep 17 00:00:00 2001 From: KetineniM Date: Fri, 14 Apr 2023 10:08:46 +0530 Subject: [PATCH 1/2] UIIN-1765 JEST/RTL test cases for CreateHolding --- .../CreateHolding/CreateHolding.test.js | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/Holding/CreateHolding/CreateHolding.test.js diff --git a/src/Holding/CreateHolding/CreateHolding.test.js b/src/Holding/CreateHolding/CreateHolding.test.js new file mode 100644 index 000000000..6d0af4af5 --- /dev/null +++ b/src/Holding/CreateHolding/CreateHolding.test.js @@ -0,0 +1,88 @@ +import '../../../test/jest/__mock__'; + +import { MemoryRouter } from 'react-router-dom'; +import { QueryClient, QueryClientProvider } from 'react-query'; +import { render, screen } from '@testing-library/react'; +import { Promise } from 'core-js'; +import { instance } from '../../../test/fixtures/instance'; +import { + useHolding, + useInstance, +} from '../../common/hooks'; +import { useHoldingMutation } from '../../hooks'; +import HoldingsForm from '../../edit/holdings/HoldingsForm'; +import CreateHolding from './CreateHolding'; + + +jest.mock('../../edit/holdings/HoldingsForm', () => jest.fn().mockReturnValue('HoldingsForm')); +jest.mock('../../hooks', () => ({ + ...jest.requireActual('../../hooks'), + useCallout: jest.fn().mockReturnValue({ sendCallout: jest.fn() }), + useHoldingMutation: jest.fn().mockReturnValue({ mutateHolding: jest.fn() }), +})); +jest.mock('../../common/hooks', () => ({ + ...jest.requireActual('../../common/hooks'), + useInstance: jest.fn().mockReturnValue({ instance: {}, isLoading: false }), + useHolding: jest.fn().mockReturnValue({ holding: {}, isLoading: false }), +})); + +const defaultProps = { + goTo: jest.fn(), + instanceId: instance.id, + holdingId: 'holdingId', + referenceData: {}, + mutator:{ + GET:jest.fn().mockReturnValue({}), + POST:jest.fn().mockReturnValue({}), + holding: { + POST:jest.fn(() => Promise.resolve({ data:{} })) + }, + } +}; + +const queryClient = new QueryClient(); + +const wrapper = ({ children }) => ( + + + {children} + + +); + +const renderCreateHolding = (props = {}) => render( + , + { wrapper }, +); + +describe('CreateHolding', () => { + const mockMutate = jest.fn(); + + beforeEach(() => { + useInstance.mockClear(); + useHolding.mockClear(); + useHoldingMutation.mockClear().mockReturnValue({ mutateHolding: mockMutate }); + }); + + it('should render HoldingsForm', () => { + renderCreateHolding(); + + expect(screen.getByText('HoldingsForm')).toBeInTheDocument(); + }); + + it('should render LoadingView if page is loading', () => { + useInstance.mockReturnValue({ isLoading: true }); + + renderCreateHolding(); + + expect(screen.getByText('LoadingView')).toBeInTheDocument(); + }); + + it('should call holding mutation when the holding form is submitted', () => { + renderCreateHolding(); + HoldingsForm.mock.calls[0][0].onSubmit(); + }); +}); From b9bf20e47f9a2f055906421c0fa208a95f426b2d Mon Sep 17 00:00:00 2001 From: KetineniM Date: Thu, 20 Apr 2023 17:11:29 +0530 Subject: [PATCH 2/2] Updated test file --- .../CreateHolding/CreateHolding.test.js | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/Holding/CreateHolding/CreateHolding.test.js b/src/Holding/CreateHolding/CreateHolding.test.js index 6d0af4af5..a45633c2a 100644 --- a/src/Holding/CreateHolding/CreateHolding.test.js +++ b/src/Holding/CreateHolding/CreateHolding.test.js @@ -1,3 +1,4 @@ +/* eslint-disable import/no-unresolved */ import '../../../test/jest/__mock__'; import { MemoryRouter } from 'react-router-dom'; @@ -5,21 +6,17 @@ import { QueryClient, QueryClientProvider } from 'react-query'; import { render, screen } from '@testing-library/react'; import { Promise } from 'core-js'; import { instance } from '../../../test/fixtures/instance'; -import { - useHolding, - useInstance, -} from '../../common/hooks'; +import { useHolding, useInstance } from '../../common/hooks'; import { useHoldingMutation } from '../../hooks'; import HoldingsForm from '../../edit/holdings/HoldingsForm'; import CreateHolding from './CreateHolding'; - jest.mock('../../edit/holdings/HoldingsForm', () => jest.fn().mockReturnValue('HoldingsForm')); jest.mock('../../hooks', () => ({ ...jest.requireActual('../../hooks'), - useCallout: jest.fn().mockReturnValue({ sendCallout: jest.fn() }), useHoldingMutation: jest.fn().mockReturnValue({ mutateHolding: jest.fn() }), })); +jest.mock('../../hooks/useCallout', () => jest.fn().mockReturnValue({ sendCallout: jest.fn() })); jest.mock('../../common/hooks', () => ({ ...jest.requireActual('../../common/hooks'), useInstance: jest.fn().mockReturnValue({ instance: {}, isLoading: false }), @@ -31,32 +28,24 @@ const defaultProps = { instanceId: instance.id, holdingId: 'holdingId', referenceData: {}, - mutator:{ - GET:jest.fn().mockReturnValue({}), - POST:jest.fn().mockReturnValue({}), + mutator: { + GET: jest.fn(() => Promise.resolve({ data: {} })), + POST: jest.fn(() => Promise.resolve({ data: {} })), holding: { - POST:jest.fn(() => Promise.resolve({ data:{} })) + POST: jest.fn(() => Promise.resolve({ data: {} })), }, - } + }, }; const queryClient = new QueryClient(); const wrapper = ({ children }) => ( - - {children} - + {children} ); -const renderCreateHolding = (props = {}) => render( - , - { wrapper }, -); +const renderCreateHolding = (props = {}) => render(, { wrapper }); describe('CreateHolding', () => { const mockMutate = jest.fn(); @@ -64,7 +53,9 @@ describe('CreateHolding', () => { beforeEach(() => { useInstance.mockClear(); useHolding.mockClear(); - useHoldingMutation.mockClear().mockReturnValue({ mutateHolding: mockMutate }); + useHoldingMutation + .mockClear() + .mockReturnValue({ mutateHolding: mockMutate }); }); it('should render HoldingsForm', () => { @@ -82,7 +73,10 @@ describe('CreateHolding', () => { }); it('should call holding mutation when the holding form is submitted', () => { + useInstance.mockReturnValue({ isLoading: false, instance: { id: 0 } }); + renderCreateHolding(); HoldingsForm.mock.calls[0][0].onSubmit(); + expect(defaultProps.mutator.holding.POST).toBeCalled(); }); });