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-1765 JEST/RTL test cases for CreateHolding #2117

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
82 changes: 82 additions & 0 deletions src/Holding/CreateHolding/CreateHolding.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* 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 { 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'),
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 }),
useHolding: jest.fn().mockReturnValue({ holding: {}, isLoading: false }),
}));

const defaultProps = {
goTo: jest.fn(),
instanceId: instance.id,
holdingId: 'holdingId',
referenceData: {},
mutator: {
GET: jest.fn(() => Promise.resolve({ data: {} })),
POST: jest.fn(() => Promise.resolve({ data: {} })),
holding: {
POST: jest.fn(() => Promise.resolve({ data: {} })),
},
},
};

const queryClient = new QueryClient();

const wrapper = ({ children }) => (
<MemoryRouter>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</MemoryRouter>
);

const renderCreateHolding = (props = {}) => render(<CreateHolding {...defaultProps} {...props} />, { 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', () => {
useInstance.mockReturnValue({ isLoading: false, instance: { id: 0 } });

renderCreateHolding();
HoldingsForm.mock.calls[0][0].onSubmit();
expect(defaultProps.mutator.holding.POST).toBeCalled();
});
});