diff --git a/lib/DonorsList/DonorsContainer.test.js b/lib/DonorsList/DonorsContainer.test.js new file mode 100644 index 00000000..9ada4541 --- /dev/null +++ b/lib/DonorsList/DonorsContainer.test.js @@ -0,0 +1,93 @@ +import { render, screen } from '@testing-library/react'; +import { MemoryRouter } from 'react-router-dom'; + +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) { + return 'stripes-components.tableEmpty'; + } + + return Object.values(donorsMap).map(donor =>
{donor.name}
); +})); + +jest.mock('./hooks', () => ({ + useFetchDonors: jest.fn().mockReturnValue({ + fetchDonorsMutation: jest.fn(), + isLoading: false, + }), +})); + +const defaultProps = { + name: 'donors', + donorOrganizationIds: [], +}; + +const renderForm = (props = {}) => ( +
+ + + +); + +const FormCmpt = stripesFinalForm({})(renderForm); + +const renderComponent = (props = {}) => (render( + + { }} {...props} /> + , +)); + +describe('DonorsContainer', () => { + beforeEach(() => { + useFetchDonors.mockClear().mockReturnValue({ + fetchDonorsMutation: jest.fn(), + isLoading: false, + }); + }); + + it('should render component', () => { + renderComponent(); + + expect(screen.getByText('stripes-components.tableEmpty')).toBeDefined(); + }); + + it('should render Loading component', () => { + useFetchDonors.mockClear().mockReturnValue({ + fetchDonorsMutation: jest.fn(), + isLoading: true, + }); + + renderComponent(); + + expect(screen.getByText('Loading')).toBeDefined(); + }); + + it('should call `fetchDonorsMutation` with `donorOrganizationIds`', () => { + const mockData = [{ name: 'Amazon', code: 'AMAZ', id: '1' }]; + const fetchDonorsMutationMock = jest.fn().mockReturnValue({ + then: (cb) => cb(mockData), + }); + + useFetchDonors.mockClear().mockReturnValue({ + fetchDonorsMutation: fetchDonorsMutationMock, + isLoading: false, + }); + + renderComponent({ donorOrganizationIds: ['1'] }); + + expect(fetchDonorsMutationMock).toHaveBeenCalled(); + expect(screen.getByText(mockData[0].name)).toBeDefined(); + }); +}); diff --git a/lib/DonorsList/DonorsList.test.js b/lib/DonorsList/DonorsList.test.js new file mode 100644 index 00000000..9a2d28d8 --- /dev/null +++ b/lib/DonorsList/DonorsList.test.js @@ -0,0 +1,53 @@ +import { render, screen } from '@testing-library/react'; +import { MemoryRouter } from 'react-router-dom'; + +import DonorsList from './DonorsList'; + +const mockFetchDonors = jest.fn(); + +const defaultProps = { + fetchDonors: mockFetchDonors, + fields: {}, + donorsMap: {}, + id: 'donors', +}; + +const wrapper = ({ children }) => ( + + {children} + +); + +const renderComponent = (props = {}) => (render( + , + { wrapper }, +)); + +describe('DonorsList', () => { + it('should render component', () => { + renderComponent(); + + expect(screen.getByText('stripes-components.tableEmpty')).toBeDefined(); + }); + + it('should render the list of donor organizations', () => { + renderComponent({ + fields: { + value: [ + '1', + '2', + ], + }, + donorsMap: { + 1: { id: '1', name: 'Amazon' }, + 2: { id: '2', name: 'Google' }, + }, + }); + + expect(screen.getByText('Amazon')).toBeDefined(); + expect(screen.getByText('Google')).toBeDefined(); + }); +});