Skip to content

Commit

Permalink
test: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Nov 8, 2023
1 parent 7c8377d commit 646391c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
46 changes: 38 additions & 8 deletions lib/DonorsList/DonorsContainer.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { MemoryRouter } from 'react-router-dom';
import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';

import stripesFinalForm from '@folio/stripes/final-form';

import DonorsContainer from './DonorsContainer';
import { useFetchDonors } from './hooks';

const mockVendor = { id: '1', name: 'Amazon' };

jest.mock('./DonorsList', () => jest.fn(({ contentData }) => {
if (!contentData.length) {
return 'stripes-components.tableEmpty';
Expand All @@ -14,6 +17,20 @@ jest.mock('./DonorsList', () => jest.fn(({ contentData }) => {
return contentData.map(({ name }) => <div key={name}>{name}</div>);
}));

jest.mock('./DonorsLookup', () => jest.fn(({ onAddDonors, name }) => {
return (
<button
type="button"
id={name}
onClick={() => onAddDonors([mockVendor])}
>
Add donor
</button>
);
}));

const setDonorIds = jest.fn();

jest.mock('./hooks', () => ({
useFetchDonors: jest.fn().mockReturnValue({
donors: [],
Expand All @@ -31,13 +48,9 @@ const defaultProps = {
'2',
],
},
donorsMap: {
1: { id: '1', name: 'Amazon' },
2: { id: '2', name: 'Google' },
},
formatter: jest.fn(),
formatter: {},
id: 'donors',
setDonorIds: jest.fn(),
setDonorIds,
searchLabel: 'Search',
showTriggerButton: true,
visibleColumns: ['name'],
Expand Down Expand Up @@ -72,14 +85,31 @@ describe('DonorsContainer', () => {
it('should render component', () => {
renderComponent();

expect(screen.getByText('stripes-acq-components.donors.noFindOrganizationPlugin')).toBeDefined();
expect(screen.getByText('Add donor')).toBeDefined();
});

it('should call `useFetchDonors` with `donorOrganizationIds`', () => {
const mockData = [{ name: 'Amazon', code: 'AMAZ', id: '1' }];

renderComponent({ donors: mockData });

expect(screen.getByText(mockData[0].name)).toBeDefined();
});

it('should call `setDonorIds` when `onAddDonors` is called', async () => {
renderComponent({
donors: [mockVendor],
fields: {
value: [],
push: jest.fn(),
},
});

const addDonorsButton = screen.getByText('Add donor');

expect(addDonorsButton).toBeDefined();

await user.click(addDonorsButton);

expect(setDonorIds).toHaveBeenCalled();
});
});
12 changes: 11 additions & 1 deletion lib/DonorsList/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { getResultsFormatter } from './utils';

const defaultProps = {
canViewOrganizations: true,
fields: {
remove: jest.fn(),
},
intl: {
formatMessage: jest.fn((id) => id),
},
};

describe('getResultsFormatter', () => {
it('should return object with name, code and unassignDonor functions', () => {
const result = getResultsFormatter({});
const result = getResultsFormatter(defaultProps);

expect(result).toEqual(expect.objectContaining({
name: expect.any(Function),
Expand Down

0 comments on commit 646391c

Please sign in to comment.