Skip to content

Commit

Permalink
test: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Nov 7, 2023
1 parent ddbc2b2 commit 7c8377d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 77 deletions.
52 changes: 24 additions & 28 deletions lib/DonorsList/DonorsContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ import stripesFinalForm from '@folio/stripes/final-form';
import DonorsContainer from './DonorsContainer';
import { useFetchDonors } from './hooks';

jest.mock('@folio/stripes/components', () => ({
...jest.requireActual('@folio/stripes/components'),
Loading: jest.fn(() => 'Loading'),
}));

jest.mock('./DonorsList', () => jest.fn(({ donorsMap }) => {
if (!Object.values(donorsMap).length) {
jest.mock('./DonorsList', () => jest.fn(({ contentData }) => {
if (!contentData.length) {
return 'stripes-components.tableEmpty';
}

return Object.values(donorsMap).map(({ name }) => <div key={name}>{name}</div>);
return contentData.map(({ name }) => <div key={name}>{name}</div>);
}));

jest.mock('./hooks', () => ({
Expand All @@ -27,8 +22,25 @@ jest.mock('./hooks', () => ({
}));

const defaultProps = {
name: 'donors',
donorOrganizationIds: [],
columnMapping: {},
columnWidths: {},
donors: [],
fields: {
value: [
'1',
'2',
],
},
donorsMap: {
1: { id: '1', name: 'Amazon' },
2: { id: '2', name: 'Google' },
},
formatter: jest.fn(),
id: 'donors',
setDonorIds: jest.fn(),
searchLabel: 'Search',
showTriggerButton: true,
visibleColumns: ['name'],
};

const renderForm = (props = {}) => (
Expand Down Expand Up @@ -60,29 +72,13 @@ describe('DonorsContainer', () => {
it('should render component', () => {
renderComponent();

expect(screen.getByText('stripes-components.tableEmpty')).toBeDefined();
});

it('should render Loading component', () => {
useFetchDonors.mockClear().mockReturnValue({
donors: [],
isLoading: true,
});

renderComponent();

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

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

useFetchDonors.mockClear().mockReturnValue({
donors: mockData,
isLoading: false,
});

renderComponent({ donorOrganizationIds: ['1'] });
renderComponent({ donors: mockData });

expect(screen.getByText(mockData[0].name)).toBeDefined();
});
Expand Down
21 changes: 0 additions & 21 deletions lib/DonorsList/DonorsForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ jest.mock('@folio/stripes/components', () => ({
Loading: jest.fn(() => 'Loading'),
}));

jest.mock('./DonorsList', () => jest.fn(({ donorsMap }) => {
if (!Object.values(donorsMap).length) {
return 'stripes-components.tableEmpty';
}

return Object.values(donorsMap).map(({ name }) => <div key={name}>{name}</div>);
}));

jest.mock('./hooks', () => ({
useFetchDonors: jest.fn().mockReturnValue({
donors: [],
Expand Down Expand Up @@ -73,17 +65,4 @@ describe('DonorsForm', () => {

expect(screen.getByText('Loading')).toBeDefined();
});

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

useFetchDonors.mockClear().mockReturnValue({
donors: mockData,
isLoading: false,
});

renderComponent({ donorOrganizationIds: ['1'] });

expect(screen.getByText(mockData[0].name)).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion lib/DonorsList/DonorsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const DonorsList = ({
DonorsList.propTypes = {
columnMapping: PropTypes.object,
columnWidths: PropTypes.object,
contentData: PropTypes.arrayOf(PropTypes.object),
contentData: PropTypes.arrayOf(PropTypes.object).isRequired,
formatter: PropTypes.object,
id: PropTypes.string.isRequired,
visibleColumns: PropTypes.arrayOf(PropTypes.string),
Expand Down
20 changes: 5 additions & 15 deletions lib/DonorsList/DonorsList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import { render, screen } from '@testing-library/react';

import DonorsList from './DonorsList';

const mockSetDonorIds = jest.fn();

const defaultProps = {
setDonorIds: mockSetDonorIds,
fields: {},
donorsMap: {},
contentData: [],
id: 'donors',
};

Expand All @@ -35,16 +31,10 @@ describe('DonorsList', () => {

it('should render the list of donor organizations', () => {
renderComponent({
fields: {
value: [
'1',
'2',
],
},
donorsMap: {
1: { id: '1', name: 'Amazon' },
2: { id: '2', name: 'Google' },
},
contentData: [
{ id: '1', name: 'Amazon' },
{ id: '2', name: 'Google' },
],
});

expect(screen.getByText('Amazon')).toBeDefined();
Expand Down
20 changes: 8 additions & 12 deletions lib/DonorsList/DonorsLookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ const mockOnAddDonors = jest.fn();

const defaultProps = {
onAddDonors: mockOnAddDonors,
fields: {
name: 'donors',
},
name: 'donors',
};

Expand All @@ -39,21 +36,20 @@ const renderComponent = (props = defaultProps) => (render(

describe('DonorsLookup', () => {
it('should render component', async () => {
renderComponent({
fields: {
name: 'donors',
push: jest.fn(),
},
name: 'donors',
onAddDonors: mockOnAddDonors,
});
renderComponent();

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

expect(addDonorsButton).toBeDefined();

await user.click(addDonorsButton);

expect(mockOnAddDonors).toHaveBeenCalledWith([mockVendorData.id]);
expect(mockOnAddDonors).toHaveBeenCalledWith([mockVendorData]);
});

it('should not render component', () => {
renderComponent({ showTriggerButton: false });

expect(screen.queryByText('Add donor')).toBeNull();
});
});
13 changes: 13 additions & 0 deletions lib/DonorsList/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getResultsFormatter } from './utils';

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

expect(result).toEqual(expect.objectContaining({
name: expect.any(Function),
code: expect.any(Function),
unassignDonor: expect.any(Function),
}));
});
});

0 comments on commit 7c8377d

Please sign in to comment.