Skip to content

Commit

Permalink
Updated test file
Browse files Browse the repository at this point in the history
  • Loading branch information
KetineniM committed Apr 20, 2023
1 parent d9626d0 commit b9bf20e
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/Holding/CreateHolding/CreateHolding.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/* eslint-disable import/no-unresolved */
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 { 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 }),
Expand All @@ -31,40 +28,34 @@ 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 }) => (
<MemoryRouter>
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</MemoryRouter>
);

const renderCreateHolding = (props = {}) => render(
<CreateHolding
{...defaultProps}
{...props}
/>,
{ wrapper },
);
const renderCreateHolding = (props = {}) => render(<CreateHolding {...defaultProps} {...props} />, { wrapper });

describe('CreateHolding', () => {
const mockMutate = jest.fn();

beforeEach(() => {
useInstance.mockClear();
useHolding.mockClear();
useHoldingMutation.mockClear().mockReturnValue({ mutateHolding: mockMutate });
useHoldingMutation
.mockClear()
.mockReturnValue({ mutateHolding: mockMutate });
});

it('should render HoldingsForm', () => {
Expand All @@ -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();
});
});

0 comments on commit b9bf20e

Please sign in to comment.