Skip to content

Commit

Permalink
UIIN-2667: Jest/RTL: Cover ImportRecord component with unit tests (#2462
Browse files Browse the repository at this point in the history
)
  • Loading branch information
UladzislauKutarkin authored Apr 29, 2024
1 parent 8284194 commit 0bab7ef
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Jest/RTL: Cover CalloutRenderer component with unit tests. Refs UIIN-2665.
* Jest/RTL: Cover ConnectedTitle component with unit tests. Refs UIIN-2666.
* Jest/RTL: Cover InstancePlugin component with unit tests. Refs UIIN-2668.
* Jest/RTL: Cover ImportRecord component with unit test. Refs UIIN-2667.

## [11.0.3] (IN PROGRESS)

Expand Down
157 changes: 157 additions & 0 deletions src/components/ImportRecord/ImportRecord.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import React from 'react';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router';
import ky from 'ky';
import { waitFor } from '@folio/jest-config-stripes/testing-library/react';
import ImportRecord from './ImportRecord';
import buildStripes from '../../../test/jest/__mock__/stripesCore.mock';

import { renderWithIntl } from '../../../test/jest/helpers';
import renderWithRouter from '../../../test/jest/helpers/renderWithRouter';

const mockProps = {
id: '123',
stripes: buildStripes(),
okapiKy: jest.fn(),
intl: {
formatMessage: jest.fn(),
},
getParams: jest.fn(() => ({
xidtype: 'type',
xid: 'externalId',
jobprofileid: 'profileId',
})),
updateLocation: jest.fn(),
};

describe('ImportRecord', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('calls loadExternalRecord on mount with correct params', async () => {
const history = createMemoryHistory();
const mockResponse = { internalIdentifier: 'newId' };
mockProps.okapiKy.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockResponse) });
await renderWithIntl(
<Router history={history}>
<ImportRecord {...mockProps} />
</Router>
);

await waitFor(() => {
expect(mockProps.okapiKy).toHaveBeenCalledWith('copycat/imports', {
timeout: 30000,
method: 'POST',
json: {
externalIdentifier: 'externalId',
internalIdentifier: '123',
profileId: 'type',
selectedJobProfileId: 'profileId',
},
});

expect(mockProps.updateLocation).toHaveBeenCalledWith(
{
_path: '/inventory/view/newId',
layer: undefined,
xid: undefined,
},
{ replace: true }
);
});
});

it('calls loadExternalRecord on mount with empty internalIdentifier', async () => {
const history = createMemoryHistory();
const mockResponse = { };
mockProps.okapiKy.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockResponse) });
await renderWithIntl(
<Router history={history}>
<ImportRecord {...mockProps} />
</Router>
);

await waitFor(() => {
expect(mockProps.okapiKy).toHaveBeenCalledWith('copycat/imports', {
timeout: 30000,
method: 'POST',
json: {
externalIdentifier: 'externalId',
internalIdentifier: '123',
profileId: 'type',
selectedJobProfileId: 'profileId',
},
});
expect(mockProps.updateLocation).toHaveBeenCalledWith(
{
_path: '/inventory/view/123',
layer: undefined,
xid: undefined,
},
{ replace: true }
);
});
});

it('handles failed API response correctly', async () => {
const errorResponse = {
status: 404,
statusText: 'Not Found',
text:() => 'Resource not found',
headers: {
get: jest.fn().mockResolvedValue('application/json')
},
};

const error = new ky.HTTPError(errorResponse);

mockProps.okapiKy.mockRejectedValue(error);

renderWithIntl(renderWithRouter(
<ImportRecord {...mockProps} />
));

await waitFor(() => {
expect(mockProps.okapiKy).toHaveBeenCalledWith('copycat/imports', expect.any(Object));
expect(mockProps.updateLocation).toHaveBeenCalledWith(
{
_path: '/inventory/view/123',
layer: undefined,
xid: undefined,
},
{ replace: true }
);
});
});

it('calls loadExternalRecord on mount with correct params and handles failure', async () => {
const mockError = new Error('Request failed');
mockProps.okapiKy.mockRejectedValue(mockError);

renderWithIntl(renderWithRouter(
<ImportRecord {...mockProps} />
));

await waitFor(() => {
expect(mockProps.okapiKy).toHaveBeenCalledWith('copycat/imports', expect.any(Object));
expect(mockProps.updateLocation).toHaveBeenCalledWith(
{
_path: '/inventory/view/123',
layer: undefined,
xid: undefined,
},
{ replace: true }
);
});
});

it('displays LoadingView during render', () => {
const mockResponse = { internalIdentifier: 'newId' };
mockProps.okapiKy.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockResponse) });
const { getByText } = renderWithIntl(renderWithRouter(
<ImportRecord {...mockProps} />
));
expect(getByText('LoadingView')).toBeInTheDocument();
});
});
2 changes: 2 additions & 0 deletions test/jest/__mock__/stripesCore.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const mockStripesCore = {
// eslint-disable-next-line react/prop-types
IfPermission: jest.fn(props => <>{props.children}</>),

withOkapiKy: jest.fn((Component) => (props) => <Component {...props} />),

// eslint-disable-next-line react/prop-types
IfInterface: jest.fn(props => <>{props.children}</>),

Expand Down

0 comments on commit 0bab7ef

Please sign in to comment.