-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8efd321
commit dfa458b
Showing
2 changed files
with
119 additions
and
50 deletions.
There are no files selected for viewing
110 changes: 60 additions & 50 deletions
110
src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: <FormattedMessage id="ui-organizations.settings.name" />, | ||
action: <FormattedMessage id="ui-organizations.settings.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 = <FormattedMessage id="ui-organizations.settings.accountTypes.save.error.accountTypeMustBeUnique" />; | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
return ( | ||
<ConnectedComponent | ||
actionSuppressor={actionSuppressor} | ||
canCreate={hasEditPerms} | ||
stripes={stripes} | ||
baseUrl={BANKING_ACCOUNT_TYPES_API} | ||
records="bankingAccountTypes" | ||
label={<FormattedMessage id="ui-organizations.settings.bankingAccountTypes" />} | ||
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: <FormattedMessage id="ui-organizations.settings.name" />, | ||
action: <FormattedMessage id="ui-organizations.settings.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 = <FormattedMessage id="ui-organizations.settings.accountTypes.save.error.accountTypeMustBeUnique" />; | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
const ConnectedComponent = this.connectedControlledVocab; | ||
|
||
return ( | ||
<ConnectedComponent | ||
actionSuppressor={actionSuppressor} | ||
canCreate={hasEditPerms} | ||
stripes={stripes} | ||
baseUrl={BANKING_ACCOUNT_TYPES_API} | ||
records="bankingAccountTypes" | ||
label={<FormattedMessage id="ui-organizations.settings.bankingAccountTypes" />} | ||
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; |
59 changes: 59 additions & 0 deletions
59
src/Settings/BankingAccountTypeSettings/BankingAccountTypeSettings.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
<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), | ||
clone: jest.fn(), | ||
}; | ||
|
||
const renderCategorySettings = () => render(<BankingAccountTypeSettings stripes={stripesMock} resources={[]} />); | ||
|
||
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(); | ||
}); | ||
}); |