Skip to content

Commit 2344cca

Browse files
committed
tests: add test coverages
1 parent baf9373 commit 2344cca

5 files changed

+350
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { MemoryRouter } from 'react-router-dom';
2+
import { render, screen } from '@testing-library/react';
3+
import user from '@testing-library/user-event';
4+
5+
import stripesFinalForm from '@folio/stripes/final-form';
6+
7+
import { useFetchPrivilegedContacts } from './hooks';
8+
import { PrivilegedDonorContacts } from './PrivilegedDonorContacts';
9+
10+
jest.mock('./hooks', () => ({
11+
useFetchPrivilegedContacts: jest.fn(),
12+
useCategories: jest.fn().mockReturnValue({ categories: [] }),
13+
}));
14+
15+
jest.mock('./PrivilegedDonorContactsLookup', () => ({
16+
PrivilegedDonorContactsLookup: jest.fn(({ onAddContacts }) => (
17+
<button
18+
type="button"
19+
onClick={() => onAddContacts([{ id: 'donorId' }])}
20+
>
21+
Add donor
22+
</button>
23+
)),
24+
}));
25+
26+
const mockOnChange = jest.fn();
27+
28+
const renderForm = (props = {}) => (
29+
<form>
30+
<PrivilegedDonorContacts
31+
name="privilegedDonorContacts"
32+
privilegedContactIds={['1']}
33+
onChange={mockOnChange}
34+
contacts={[]}
35+
fields={{ value: [], push: jest.fn() }}
36+
{...props}
37+
/>
38+
<button type="submit">Submit</button>
39+
</form>
40+
);
41+
42+
const FormCmpt = stripesFinalForm({})(renderForm);
43+
44+
const renderComponent = (props = {}) => (render(
45+
<MemoryRouter>
46+
<FormCmpt onSubmit={() => { }} {...props} />
47+
</MemoryRouter>,
48+
));
49+
50+
describe('PrivilegedDonorContacts', () => {
51+
beforeEach(() => {
52+
useFetchPrivilegedContacts.mockClear().mockReturnValue({
53+
contacts: [],
54+
isLoading: false,
55+
});
56+
});
57+
58+
it('should render component', () => {
59+
renderComponent();
60+
61+
expect(screen.getByText('stripes-components.tableEmpty')).toBeDefined();
62+
});
63+
64+
it('should call onChange when contactIds changed', () => {
65+
const onChange = jest.fn();
66+
67+
renderComponent({ onChange });
68+
69+
const addDonorButton = screen.getByText('Add donor');
70+
71+
user.click(addDonorButton);
72+
73+
expect(onChange).toHaveBeenCalled();
74+
});
75+
});
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { MemoryRouter } from 'react-router-dom';
2+
import { render, screen } from '@testing-library/react';
3+
import user from '@testing-library/user-event';
4+
5+
import stripesFinalForm from '@folio/stripes/final-form';
6+
7+
import { PrivilegedDonorContactsContainer } from './PrivilegedDonorContactsContainer';
8+
import { useCategories } from './hooks';
9+
10+
const mockVendor = { id: '1', name: 'Amazon' };
11+
12+
jest.mock('./PrivilegedDonorContactsList', () => ({
13+
PrivilegedDonorContactsList: jest.fn(({ contentData }) => {
14+
return (
15+
<div>
16+
{contentData.map(({ name }) => (
17+
<div key={name}>{name}</div>
18+
))}
19+
</div>
20+
);
21+
}),
22+
}));
23+
24+
jest.mock('./PrivilegedDonorContactsLookup', () => ({
25+
PrivilegedDonorContactsLookup: jest.fn(({ onAddContacts }) => {
26+
return (
27+
<div>
28+
<button
29+
type="button"
30+
onClick={() => onAddContacts([mockVendor])}
31+
>
32+
Add donor
33+
</button>
34+
</div>
35+
);
36+
}),
37+
}));
38+
39+
const setContactIds = jest.fn();
40+
41+
jest.mock('./hooks', () => ({
42+
useCategories: jest.fn().mockReturnValue({
43+
categories: [],
44+
isLoading: false,
45+
}),
46+
}));
47+
48+
const defaultProps = {
49+
columnMapping: {},
50+
columnWidths: {},
51+
contacts: [],
52+
fields: {
53+
value: [
54+
'1',
55+
'2',
56+
],
57+
},
58+
formatter: {},
59+
id: 'donors',
60+
setContactIds,
61+
searchLabel: 'Search',
62+
showTriggerButton: true,
63+
visibleColumns: ['name'],
64+
};
65+
66+
const renderForm = (props = {}) => (
67+
<form>
68+
<PrivilegedDonorContactsContainer
69+
{...defaultProps}
70+
{...props}
71+
/>
72+
<button type="submit">Submit</button>
73+
</form>
74+
);
75+
76+
const FormCmpt = stripesFinalForm({})(renderForm);
77+
78+
const renderComponent = (props = {}) => (render(
79+
<MemoryRouter>
80+
<FormCmpt onSubmit={() => { }} {...props} />
81+
</MemoryRouter>,
82+
));
83+
84+
describe('DonorsContainer', () => {
85+
beforeEach(() => {
86+
useCategories.mockClear().mockReturnValue({
87+
donors: [],
88+
isLoading: false,
89+
});
90+
});
91+
92+
it('should render component', () => {
93+
renderComponent();
94+
95+
expect(screen.getByText('Add donor')).toBeDefined();
96+
});
97+
98+
it('should call `setContactIds` when `onAddContacts` is called', () => {
99+
renderComponent({
100+
donors: [mockVendor],
101+
fields: {
102+
value: [],
103+
push: jest.fn(),
104+
},
105+
});
106+
107+
const addDonorsButton = screen.getByText('Add donor');
108+
109+
expect(addDonorsButton).toBeDefined();
110+
user.click(addDonorsButton);
111+
expect(setContactIds).toHaveBeenCalled();
112+
});
113+
114+
it('should not render `DonorsLookup` when `showTriggerButton` is false', () => {
115+
renderComponent({ showTriggerButton: false });
116+
117+
expect(screen.queryByText('Add donor')).toBeNull();
118+
});
119+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { MemoryRouter } from 'react-router-dom';
2+
import { render, screen } from '@testing-library/react';
3+
4+
import { PrivilegedDonorContactsList } from './PrivilegedDonorContactsList';
5+
6+
jest.mock('./hooks', () => ({
7+
useCategories: jest.fn().mockReturnValue({
8+
categories: [],
9+
isLoading: false,
10+
}),
11+
}));
12+
13+
const defaultProps = {
14+
contentData: [],
15+
id: 'donors',
16+
};
17+
18+
const wrapper = ({ children }) => (
19+
<MemoryRouter>
20+
{children}
21+
</MemoryRouter>
22+
);
23+
24+
const renderComponent = (props = {}) => (render(
25+
<PrivilegedDonorContactsList
26+
{...defaultProps}
27+
{...props}
28+
/>,
29+
{ wrapper },
30+
));
31+
32+
describe('PrivilegedDonorContactsList', () => {
33+
it('should render component', () => {
34+
renderComponent();
35+
36+
expect(screen.getByText('stripes-components.tableEmpty')).toBeDefined();
37+
});
38+
39+
it('should render the list of donor contacts', () => {
40+
renderComponent({
41+
contentData: [
42+
{ id: '1', firstName: 'John', lastName: 'Smith' },
43+
{ id: '2', firstName: 'Tim', lastName: 'Jones' },
44+
],
45+
});
46+
47+
expect(screen.getByText('Jones, Tim')).toBeDefined();
48+
expect(screen.getByText('Smith, John')).toBeDefined();
49+
});
50+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { MemoryRouter } from 'react-router-dom';
2+
import { render, screen } from '@testing-library/react';
3+
4+
import { PrivilegedDonorsListContainer } from './PrivilegedDonorContactsListContainer';
5+
import { useFetchPrivilegedContacts } from './hooks';
6+
7+
jest.mock('@folio/stripes/components', () => ({
8+
...jest.requireActual('@folio/stripes/components'),
9+
Loading: jest.fn(() => 'Loading'),
10+
}));
11+
12+
jest.mock('./PrivilegedDonorContactsList', () => ({
13+
PrivilegedDonorContactsList: jest.fn(() => 'PrivilegedDonorContactsList'),
14+
}));
15+
16+
jest.mock('./hooks', () => ({
17+
useFetchPrivilegedContacts: jest.fn().mockReturnValue({
18+
contacts: [],
19+
isLoading: false,
20+
}),
21+
}));
22+
23+
const defaultProps = {
24+
donorOrganizationIds: [],
25+
};
26+
27+
const wrapper = ({ children }) => (
28+
<MemoryRouter>
29+
{children}
30+
</MemoryRouter>
31+
);
32+
33+
const renderComponent = (props = {}) => (render(
34+
<PrivilegedDonorsListContainer
35+
{...defaultProps}
36+
{...props}
37+
/>,
38+
{ wrapper },
39+
));
40+
41+
describe('PrivilegedDonorsListContainer', () => {
42+
it('should render component', () => {
43+
renderComponent();
44+
45+
expect(screen.getByText('PrivilegedDonorContactsList')).toBeDefined();
46+
});
47+
48+
it('should render loading component', () => {
49+
useFetchPrivilegedContacts.mockClear().mockReturnValue({
50+
contacts: [],
51+
isLoading: true,
52+
});
53+
54+
renderComponent();
55+
56+
expect(screen.getByText('Loading')).toBeDefined();
57+
});
58+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { render, screen } from '@testing-library/react';
2+
import user from '@testing-library/user-event';
3+
4+
import { PrivilegedDonorContactsLookup } from './PrivilegedDonorContactsLookup';
5+
6+
const mockContactData = { id: '1', name: 'Amazon' };
7+
8+
jest.mock('@folio/stripes/core', () => ({
9+
...jest.requireActual('@folio/stripes/core'),
10+
Pluggable: jest.fn(({ children, ...rest }) => {
11+
return (
12+
<div>
13+
{children}
14+
<button
15+
type="button"
16+
id={rest?.name}
17+
onClick={() => rest?.addContacts([mockContactData])}
18+
>
19+
Add donor
20+
</button>
21+
</div>
22+
);
23+
}),
24+
}));
25+
26+
const mockOnAddContacts = jest.fn();
27+
28+
const defaultProps = {
29+
onAddContacts: mockOnAddContacts,
30+
name: 'donors',
31+
};
32+
33+
const renderComponent = (props = defaultProps) => (render(
34+
<PrivilegedDonorContactsLookup {...props} />,
35+
));
36+
37+
describe('PrivilegedDonorContactsLookup', () => {
38+
it('should render component', () => {
39+
renderComponent();
40+
41+
const addDonorsButton = screen.getByText('Add donor');
42+
43+
expect(addDonorsButton).toBeDefined();
44+
45+
user.click(addDonorsButton);
46+
expect(mockOnAddContacts).toHaveBeenCalledWith([mockContactData]);
47+
});
48+
});

0 commit comments

Comments
 (0)