Skip to content

Commit

Permalink
tests: add test coverages
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Oct 20, 2023
1 parent 5884302 commit d46a134
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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}
<div onChange={rowFilterFunction}>{rowFilter}</div>
<button
data-testid="button-new"
type="button"
onClick={() => {
preCreateHook();
listSuppressor();
}}
>
New
</button>
</>
)));

const stripesMock = {
connect: component => component,
hasPerm: jest.fn(() => true),
};

const renderBankingAccountTypeSettings = () => render(
<BankingAccountTypeSettings
stripes={stripesMock}
/>,
);

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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RadioButtonGroup,
Row,
} from '@folio/stripes/components';
import { useShowCallout } from '@folio/stripes-acq-components';
import {
useOkapiKy,
useNamespace,
Expand All @@ -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: <FormattedMessage id="ui-organizations.settings.accountTypes.save.success.message" />,
});
} catch (error) {
sendCallout({
type: 'success',
message: <FormattedMessage id="settings.accountTypes.save.error.generic.message" />,
});
}
};

if (isLoading) {
Expand Down Expand Up @@ -71,7 +83,9 @@ const BankingInformationSettings = () => {
value="false"
/>
</Field>
<Button type="submit" buttonStyle="primary">Save</Button>
<Button type="submit" buttonStyle="primary">
<FormattedMessage id="ui-organizations.settings.accountTypes.save.button" />
</Button>
</form>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
<BankingInformationSettings />,
);

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();
});
});
52 changes: 52 additions & 0 deletions src/Settings/hooks/useBankingInformation.test.js
Original file line number Diff line number Diff line change
@@ -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 }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

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,
});
});
});
16 changes: 10 additions & 6 deletions translations/ui-organizations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 <b>{term}</b> was successfully <b>deleted</b>",
"settings.bankingAccountTypes.termWillBeDeleted": "The banking account type <b>{term}</b> will be <b>deleted.</b>",
"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 <b>{term}</b> was successfully <b>deleted</b>",
"settings.bankingAccountTypes.termWillBeDeleted": "The account type <b>{term}</b> will be <b>deleted.</b>",
"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",
Expand Down

0 comments on commit d46a134

Please sign in to comment.