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

UIPFU-82 - add jest test cases for PluginFindUser.js #279

Merged
merged 4 commits into from
Sep 30, 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 @@ -6,6 +6,7 @@
* Apply Prev/Next Pagination. Refs UIPFU-49.
* Add Jest unit tests for ui-plugin-find-user/src/Filters.js. Refs UIPFU-81.
* Fix pagination issues with `Unassigned` filter. Refs UIPFU-96.
* Add Jest unit tests for ui-plugin-find-user/src/PluginFindUser.js. Refs UIPFU-82.

## [7.1.2](https://github.com/folio-org/ui-plugin-find-user/tree/v7.1.2) (2024-09-05)
[Full Changelog](https://github.com/folio-org/ui-plugin-find-user/compare/v7.1.1...v7.1.2)
Expand Down
2 changes: 1 addition & 1 deletion src/PluginFindUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PluginFindUser extends Component {
onClick={this.openModal}
buttonRef={this.modalTrigger}
marginBottom0={marginBottom0}
data-test-plugin-find-user-button
data-testid="searchButton"
>
{searchLabel || <Icon icon="search" color="#fff" />}
</Button>
Expand Down
99 changes: 99 additions & 0 deletions src/PluginFindUser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { screen } from '@folio/jest-config-stripes/testing-library/react';
import userEvent from '@folio/jest-config-stripes/testing-library/user-event';

import renderWithRouter from 'helpers/renderWithRouter';

import PluginFindUser from './PluginFindUser';

jest.unmock('@folio/stripes/components');
jest.mock('./UserSearchModal', () => ({ closeCB, openWhen, modalRef }) => {
return (
<div>
{openWhen && (
<div ref={modalRef}>
User Search Modal
<button type="button" onClick={closeCB}>Close Modal</button>
</div>
)}
</div>
);
});

const afterCloseMock = jest.fn();
const renderTriggerMock = jest.fn();

const renderPluginFindUser = (props) => renderWithRouter(
<PluginFindUser {...props} />
);

const props = {
afterClose: afterCloseMock,
id: 'find-user',
searchLabel: <span>Search User</span>,
searchButtonStyle: 'primary',
marginBottom0: true,
marginTop0: false,
dataKey: 'find_patron',
initialSelectedUsers: {
user1: {
username: 'jdoe',
id: '123456',
active: true,
barcode: 'barcode123',
personal: {
lastName: 'Doe',
firstName: 'John',
email: '[email protected]',
},
patronGroup: 'Regular',
},
},
stripes: {
connect: jest.fn(),
logger: {
log: jest.fn(),
},
okapi: {
url: 'url',
tenant: 'tenant',
},
},
};

describe('PluginFindUser', () => {
it('should render the search button', () => {
renderPluginFindUser(props);
expect(screen.getByTestId('searchButton')).toBeInTheDocument();
});

it('should display the correct search label text', () => {
renderPluginFindUser(props);
expect(screen.getByText('Search User')).toBeInTheDocument();
});

it('should display the search icon when "searchLabel" is not provided', () => {
renderPluginFindUser({ ...props, searchLabel: undefined });
expect(screen.getByRole('presentation')).toBeInTheDocument();
});

it('should trigger renderTrigger() when provided in props', () => {
const propsWithRenderTrigger = { ...props, renderTrigger: renderTriggerMock };
renderPluginFindUser(propsWithRenderTrigger);
expect(renderTriggerMock).toHaveBeenCalledTimes(1);
});

it('should open the user search modal when the search button is clicked', async () => {
renderPluginFindUser(props);
const searchBtn = screen.getByTestId('searchButton');
await userEvent.click(searchBtn);
expect(screen.getByText('User Search Modal')).toBeInTheDocument();
});

it('should trigger afterClose() when the modal is closed', async () => {
renderPluginFindUser(props);
const searchBtn = screen.getByTestId('searchButton');
await userEvent.click(searchBtn);
await userEvent.click(screen.getByText('Close Modal'));
expect(afterCloseMock).toHaveBeenCalledTimes(1);
});
});
Loading