From dfa458b64e1650cdab2bf9ae752963db945b8f27 Mon Sep 17 00:00:00 2001 From: Alisher Musurmonov Date: Mon, 23 Oct 2023 13:24:03 +0500 Subject: [PATCH] tests: add test coverages --- .../BankingAccountTypeSettings.js | 110 ++++++++++-------- .../BankingAccountTypeSettings.test.js | 59 ++++++++++ 2 files changed, 119 insertions(+), 50 deletions(-) create mode 100644 src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js diff --git a/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js index 705e4ae5..a620cbf4 100644 --- a/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js +++ b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js @@ -1,62 +1,72 @@ +import React from 'react'; import { FormattedMessage } from 'react-intl'; -import { stripesConnect, stripesShape } from '@folio/stripes/core'; +import { stripesShape } from '@folio/stripes/core'; import { ControlledVocab } from '@folio/stripes/smart-components'; import { getControlledVocabTranslations } from '@folio/stripes-acq-components'; import { BANKING_ACCOUNT_TYPES_API } from '../constants'; -const BankingAccountTypeSettings = ({ stripes }) => { - const ConnectedComponent = stripes.connect(ControlledVocab); - - const columnMapping = { - value: , - action: , - }; - - const hasEditPerms = stripes.hasPerm('ui-organizations.settings'); - const actionSuppressor = { - edit: () => !hasEditPerms, - delete: () => !hasEditPerms, - }; - - const setUniqValidation = (value, index, items) => { - const errors = {}; - - const isBankingAccountTypeExist = items.some(({ id, name }) => { - return name?.toLowerCase() === value?.name?.toLowerCase() && id !== value?.id; - }); - - if (isBankingAccountTypeExist) { - errors.name = ; - } - - return errors; - }; - - return ( - } - translations={getControlledVocabTranslations('ui-organizations.settings.bankingAccountTypes')} - objectLabel="BankingAccountTypes" - visibleFields={['name']} - columnMapping={columnMapping} - hiddenFields={['lastUpdated', 'numberOfObjects']} - nameKey="bankingAccountTypes" - id="bankingAccountTypes" - validate={setUniqValidation} - sortby="name" - /> - ); -}; +class BankingAccountTypeSettings extends React.Component { + constructor(props) { + super(props); + this.connectedControlledVocab = props.stripes.connect(ControlledVocab); + } + + render() { + const { stripes } = this.props; + + const columnMapping = { + value: , + action: , + }; + + const hasEditPerms = stripes.hasPerm('ui-organizations.settings'); + const actionSuppressor = { + edit: () => !hasEditPerms, + delete: () => !hasEditPerms, + }; + + const setUniqValidation = (value, index, items) => { + const errors = {}; + + const isBankingAccountTypeExist = items.some(({ id, name }) => { + return name?.toLowerCase() === value?.name?.toLowerCase() && id !== value?.id; + }); + + if (isBankingAccountTypeExist) { + errors.name = ; + } + + return errors; + }; + + const ConnectedComponent = this.connectedControlledVocab; + + return ( + } + translations={getControlledVocabTranslations('ui-organizations.settings.bankingAccountTypes')} + objectLabel="BankingAccountTypes" + visibleFields={['name']} + columnMapping={columnMapping} + hiddenFields={['lastUpdated', 'numberOfObjects']} + nameKey="bankingAccountTypes" + id="bankingAccountTypes" + validate={setUniqValidation} + sortby="name" + /> + ); + } +} BankingAccountTypeSettings.propTypes = { stripes: stripesShape.isRequired, }; -export default stripesConnect(BankingAccountTypeSettings); +export default BankingAccountTypeSettings; diff --git a/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js new file mode 100644 index 00000000..2734316e --- /dev/null +++ b/src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js @@ -0,0 +1,59 @@ +import { render, screen } from '@folio/jest-config-stripes/testing-library/react'; + +import { ControlledVocab } from '@folio/stripes/smart-components'; + +import BankingAccountTypeSettings from './BankingAccountTypeSettings'; + +jest.mock('@folio/stripes/core', () => ({ + ...jest.requireActual('@folio/stripes/core'), + useStripes: jest.fn(), +})); + +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), + clone: jest.fn(), +}; + +const renderCategorySettings = () => render(); + +describe('BankingAccountTypeSettings', () => { + it('should render component', () => { + renderCategorySettings(); + + expect(screen.getByText('New')); + expect(screen.getByText('ui-organizations.settings.bankingAccountTypes')); + }); + + it('should check action suppression', () => { + renderCategorySettings(); + + const { actionSuppressor } = ControlledVocab.mock.calls[0][0]; + + expect(actionSuppressor.edit()).toBeFalsy(); + expect(actionSuppressor.delete()).toBeFalsy(); + }); +});