From d46a134b86daf84cce2090c4557709615aa5cb9a Mon Sep 17 00:00:00 2001 From: Alisher Musurmonov Date: Fri, 20 Oct 2023 17:26:41 +0500 Subject: [PATCH] tests: add test coverages --- .../BankingAccountTypeSettings.test.js | 51 ++++++++++++++++ .../BankingInformationSettings.js | 26 ++++++-- .../BankingInformationSettings.test.js | 61 +++++++++++++++++++ .../hooks/useBankingInformation.test.js | 52 ++++++++++++++++ translations/ui-organizations/en.json | 16 +++-- 5 files changed, 194 insertions(+), 12 deletions(-) create mode 100644 src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js create mode 100644 src/Settings/BankingInformationSettings/BankingInformationSettings.test.js create mode 100644 src/Settings/hooks/useBankingInformation.test.js diff --git a/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js new file mode 100644 index 00000000..cff7a19e --- /dev/null +++ b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js @@ -0,0 +1,51 @@ +import { render } from '@folio/jest-config-stripes/testing-library/react'; + +import { ControlledVocab } from '@folio/stripes/smart-components'; + +import BankingAccountTypeSettings from './BankingAccountTypeSettings'; + +jest.mock('@folio/stripes-smart-components/lib/ControlledVocab', () => jest.fn(({ + rowFilter, + label, + rowFilterFunction, + preCreateHook, + listSuppressor, +}) => ( + <> + {label} +
{rowFilter}
+ + +))); + +const stripesMock = { + connect: component => component, + hasPerm: jest.fn(() => true), +}; + +const renderBankingAccountTypeSettings = () => render( + , +); + +describe('BankingAccountTypeSettings', () => { + it('should check action suppression', () => { + renderBankingAccountTypeSettings(); + + const { actionSuppressor } = ControlledVocab.mock.calls[0][0]; + + expect(stripesMock.hasPerm).toHaveBeenCalled(); + expect(actionSuppressor.edit()).toBeFalsy(); + expect(actionSuppressor.delete()).toBeFalsy(); + }); +}); diff --git a/src/Settings/BankingInformationSettings/BankingInformationSettings.js b/src/Settings/BankingInformationSettings/BankingInformationSettings.js index fb29d547..0f42d106 100644 --- a/src/Settings/BankingInformationSettings/BankingInformationSettings.js +++ b/src/Settings/BankingInformationSettings/BankingInformationSettings.js @@ -12,6 +12,7 @@ import { RadioButtonGroup, Row, } from '@folio/stripes/components'; +import { useShowCallout } from '@folio/stripes-acq-components'; import { useOkapiKy, useNamespace, @@ -25,13 +26,24 @@ const BankingInformationSettings = () => { const ky = useOkapiKy(); const queryClient = useQueryClient(); const [namespace] = useNamespace({ key: 'banking-information-settings' }); + const sendCallout = useShowCallout(); - const onSubmit = ({ value }) => { - ky.put(`${SETTINGS_API}/${bankingInformationId}`, { - json: { value, key }, - }).then(() => { + const onSubmit = async ({ value }) => { + try { + await ky.put(`${SETTINGS_API}/${bankingInformationId}`, { + json: { value, key }, + }); queryClient.invalidateQueries([namespace]); - }); + sendCallout({ + type: 'success', + message: , + }); + } catch (error) { + sendCallout({ + type: 'success', + message: , + }); + } }; if (isLoading) { @@ -71,7 +83,9 @@ const BankingInformationSettings = () => { value="false" /> - + )} /> diff --git a/src/Settings/BankingInformationSettings/BankingInformationSettings.test.js b/src/Settings/BankingInformationSettings/BankingInformationSettings.test.js new file mode 100644 index 00000000..2bae5f6f --- /dev/null +++ b/src/Settings/BankingInformationSettings/BankingInformationSettings.test.js @@ -0,0 +1,61 @@ +import { render, screen, act } from '@folio/jest-config-stripes/testing-library/react'; +import user from '@folio/jest-config-stripes/testing-library/user-event'; +import { useOkapiKy } from '@folio/stripes/core'; + +import BankingInformationSettings from './BankingInformationSettings'; + +jest.mock('react-query', () => ({ + ...jest.requireActual('react-query'), + useQueryClient: jest.fn(() => ({ + invalidateQueries: jest.fn(), + })), +})); + +jest.mock('../hooks/useBankingInformation', () => ({ + useBankingInformation: jest.fn(() => ({ isLoading: false, enabled: false })), +})); + +const renderBankingInformationSettings = () => render( + , +); + +describe('BankingInformationSettings component', () => { + it('should display pane headings', () => { + renderBankingInformationSettings(); + + const paneTitle = screen.getAllByText('ui-organizations.settings.bankingInformation'); + + expect(paneTitle).toHaveLength(2); + }); + + it('should display radio buttons', async () => { + renderBankingInformationSettings(); + + expect(screen.getByText('ui-organizations.settings.bankingInformation.enabled')).toBeInTheDocument(); + expect(screen.getByText('ui-organizations.settings.bankingInformation.disabled')).toBeInTheDocument(); + }); + + it('should save banking options', async () => { + const mockPutMethod = jest.fn(() => ({ + json: () => Promise.resolve('ok'), + })); + + useOkapiKy + .mockClear() + .mockReturnValue({ + put: mockPutMethod, + }); + + renderBankingInformationSettings(); + + const enabledButton = screen.getByText('ui-organizations.settings.bankingInformation.enabled'); + const saveButton = screen.getByText('ui-organizations.settings.accountTypes.save.button'); + + await act(async () => { + await user.click(enabledButton); + await user.click(saveButton); + }); + + expect(mockPutMethod).toHaveBeenCalled(); + }); +}); diff --git a/src/Settings/hooks/useBankingInformation.test.js b/src/Settings/hooks/useBankingInformation.test.js new file mode 100644 index 00000000..1fe01567 --- /dev/null +++ b/src/Settings/hooks/useBankingInformation.test.js @@ -0,0 +1,52 @@ +import { + QueryClient, + QueryClientProvider, +} from 'react-query'; + +import { + renderHook, + waitFor, +} from '@folio/jest-config-stripes/testing-library/react'; +import { useOkapiKy } from '@folio/stripes/core'; + +import { useBankingInformation } from './useBankingInformation'; + +const queryClient = new QueryClient(); + +const MOCK_BANKING_INFORMATION = { + 'id': 'cb007def-4b68-496c-ad78-ea8e039e819d', + 'key': 'BANKING_INFORMATION_ENABLED', + 'value': 'true', +}; + +// eslint-disable-next-line react/prop-types +const wrapper = ({ children }) => ( + + {children} + +); + +describe('useBankingInformation', () => { + beforeEach(() => { + useOkapiKy + .mockClear() + .mockReturnValue({ + get: () => ({ + json: () => Promise.resolve({ settings: [MOCK_BANKING_INFORMATION] }), + }), + }); + }); + + it('should fetch all organization types', async () => { + const { result } = renderHook(() => useBankingInformation(), { wrapper }); + + await waitFor(() => expect(result.current.isLoading).toBeFalsy()); + + expect(result.current).toEqual({ + enabled: true, + isLoading: false, + id: MOCK_BANKING_INFORMATION.id, + key: MOCK_BANKING_INFORMATION.key, + }); + }); +}); diff --git a/translations/ui-organizations/en.json b/translations/ui-organizations/en.json index 7fa0aa18..ac28a66b 100644 --- a/translations/ui-organizations/en.json +++ b/translations/ui-organizations/en.json @@ -36,6 +36,7 @@ "filterConfig.contactPeopleCategory": "Contact people category", "filterConfig.country": "Country", "filterConfig.isVendor": "Is vendor", + "filterConfig.isDonor": "Is donor", "filterConfig.languages": "Languages", "filterConfig.paymentMethod": "Payment method", "filterConfig.statsAvailable": "Stats available", @@ -446,12 +447,15 @@ "settings.bankingInformation.enabled": "Enable", "settings.bankingInformation.disabled": "Disable", "settings.bankingAccountTypes": "Account types", - "settings.bankingAccountTypes.cannotDeleteTermHeader": "Cannot delete banking account type", - "settings.bankingAccountTypes.cannotDeleteTermMessage": "This banking account type cannot be deleted, as it is in use by one or more records.", - "settings.bankingAccountTypes.deleteEntry": "Delete banking account type", - "settings.bankingAccountTypes.termDeleted": "The banking account type {term} was successfully deleted", - "settings.bankingAccountTypes.termWillBeDeleted": "The banking account type {term} will be deleted.", - "settings.accountTypes.save.error.accountTypeMustBeUnique": "Banking account type must be unique.", + "settings.bankingAccountTypes.cannotDeleteTermHeader": "Cannot delete account type", + "settings.bankingAccountTypes.cannotDeleteTermMessage": "This account type cannot be deleted, as it is in use by one or more records.", + "settings.bankingAccountTypes.deleteEntry": "Delete account type", + "settings.bankingAccountTypes.termDeleted": "The account type {term} was successfully deleted", + "settings.bankingAccountTypes.termWillBeDeleted": "The account type {term} will be deleted.", + "settings.accountTypes.save.error.accountTypeMustBeUnique": "Account type must be unique.", + "settings.accountTypes.save.button": "Save", + "settings.accountTypes.save.success.message": "Setting was successfully updated.", + "settings.accountTypes.save.error.generic.message": "Something went wrong.", "permission.view": "Organizations: View", "permission.edit": "Organizations: View, edit",