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

UIREQMED-68: Cover src/index.js file by jest/RTL tests #86

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.1.0 (IN PROGRESS)
* Make "Enter" buttons shorter using default styles. Refs UIREQMED-71.
* Cover src/index.js file by jest/RTL tests. Refs UIREQMED-68.

## [2.0.1] (https://github.com/folio-org/ui-requests-mediated/tree/v2.0.1) (2024-12-06)
[Full Changelog](https://github.com/folio-org/ui-requests-mediated/compare/v2.0.0...v2.0.1)
Expand Down
152 changes: 152 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { useLocation } from 'react-router-dom';

import { render } from '@folio/jest-config-stripes/testing-library/react';
import {
TitleManager,
Route,
} from '@folio/stripes/core';

import RequestsMediated from './index';
import RequestFormContainer from './components/MediatedRequestsActivities/components/RequestFormContainer';
import MediatedRequestsActivitiesContainer from './routes/MediatedRequestsActivitiesContainer';
import MediatedRequestsDetail from './components/MediatedRequestsActivities/components/MediatedRequestsDetail';
import {
CONFIRM_ITEM_ARRIVAL,
MEDIATED_REQUESTS_ACTIVITIES,
SEND_ITEM_IN_TRANSIT,
} from './constants';

const settings = {
settings: {},
};
const patronGroups = {
patronGroups: {
usergroups: [],
},
};
const location = {
search: '?query=test',
};
const labelIds = {
searchTitle: 'ui-requests-mediated.meta.searchTitle',
metaTitle: 'ui-requests-mediated.meta.title',
};
const basicProps = {
match: {
path: 'path',
},
showSettings: true,
};

jest.mock('react-router-dom', () => ({
useLocation: jest.fn(() => location),
}));
jest.mock('./hooks', () => ({
useGeneralTlrSettings: jest.fn(() => settings),
usePatronGroups: jest.fn(() => patronGroups),
}));
jest.mock('./components/ConfirmItemArrival', () => jest.fn(() => <div />));
jest.mock('./routes/MediatedRequestsActivitiesContainer', () => jest.fn(({ children }) => (
<div>
{children}
</div>
)));
jest.mock('./components/MediatedRequestsActivities/components/MediatedRequestsDetail', () => jest.fn(() => <div />));
jest.mock('./components/SendItemInTransit', () => jest.fn(() => <div />));
jest.mock('./components/MediatedRequestsActivities/components/RequestFormContainer', () => jest.fn(() => <div />));

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

describe('Initial render', () => {
beforeEach(() => {
render(<RequestsMediated {...basicProps} />);
});

it('should trigger TitleManager with correct props', () => {
const expectedProps = {
page: labelIds.searchTitle,
};

expect(TitleManager).toHaveBeenCalledWith(expectedProps, {});
});

it('should trigger RequestFormContainer twice with correct props', () => {
const expectedProps = [
{
isEditMode: false,
patronGroups: patronGroups.patronGroups.usergroups,
settings: settings.settings,
},
{
isEditMode: true,
patronGroups: patronGroups.patronGroups.usergroups,
settings: settings.settings,
request: null,
setRequest: expect.any(Function),
}
];

expectedProps.forEach(props => {
expect(RequestFormContainer).toHaveBeenCalledWith(props, {});
});
});

it('should trigger Routes with correct paths', () => {
const rootPath = basicProps.match.path;
const paths = [
`${rootPath}/${MEDIATED_REQUESTS_ACTIVITIES}/create`,
`${rootPath}/${MEDIATED_REQUESTS_ACTIVITIES}/edit/:id`,
`${rootPath}/${MEDIATED_REQUESTS_ACTIVITIES}/preview/:id`,
`${rootPath}/${MEDIATED_REQUESTS_ACTIVITIES}/notes/new`,
`${rootPath}/${MEDIATED_REQUESTS_ACTIVITIES}/notes/:noteId/edit`,
`${rootPath}/${MEDIATED_REQUESTS_ACTIVITIES}/notes/:noteId`,
`${rootPath}/${MEDIATED_REQUESTS_ACTIVITIES}`,
`${rootPath}/${CONFIRM_ITEM_ARRIVAL}`,
`${rootPath}/${SEND_ITEM_IN_TRANSIT}`,
];

paths.forEach(path => {
expect(Route).toHaveBeenCalledWith(expect.objectContaining({ path }), {});
});
});

it('should trigger MediatedRequestsActivitiesContainer with correct props', () => {
const expectedProps = {
...basicProps,
settings: settings.settings,
};

expect(MediatedRequestsActivitiesContainer).toHaveBeenCalledWith(expectedProps, {});
});

it('should trigger MediatedRequestsDetail with correct props', () => {
const expectedProps = {
...basicProps,
setRequest: expect.any(Function),
patronGroups: patronGroups.patronGroups.usergroups,
};

expect(MediatedRequestsDetail).toHaveBeenCalledWith(expectedProps, {});
});
});

describe('When search query is not provided', () => {
beforeEach(() => {
useLocation.mockReturnValueOnce({
search: '',
});
render(<RequestsMediated {...basicProps} />);
});

it('should trigger TitleManager with correct props', () => {
const expectedProps = {
page: labelIds.metaTitle,
};

expect(TitleManager).toHaveBeenCalledWith(expectedProps, {});
});
});
});
3 changes: 3 additions & 0 deletions test/jest/__mock__/stripesCore.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jest.mock('@folio/stripes/core', () => ({
</>
);
}),
Redirect: jest.fn(() => <div />),
Route: jest.fn(({ children }) => children),
Switch: jest.fn(({ children }) => children),
TitleManager: jest.fn(() => <div />),
useStripes: jest.fn(() => ({
connect: Component => Component,
Expand Down
Loading