-
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.
UISACQCOMP-230 Move reusable version history components to the ACQ lib (
#831) * UISACQCOMP-230 Move reusable version history components to the ACQ lib * add unit tests * remove debug
- Loading branch information
1 parent
7f664e2
commit 583d17e
Showing
19 changed files
with
335 additions
and
10 deletions.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
lib/VersionHistory/components/VersionCheckbox/VersionCheckbox.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,40 @@ | ||
import PropTypes from 'prop-types'; | ||
import { useContext, useMemo } from 'react'; | ||
|
||
import { Checkbox } from '@folio/stripes/components'; | ||
|
||
import { VersionViewContext } from '../../VersionViewContext'; | ||
|
||
export const VersionCheckbox = ({ | ||
checked, | ||
label, | ||
name, | ||
...props | ||
}) => { | ||
const versionContext = useContext(VersionViewContext); | ||
const isUpdated = useMemo(() => ( | ||
versionContext?.paths?.includes(name) | ||
), [name, versionContext?.paths]); | ||
|
||
const checkboxLabel = isUpdated ? <mark>{label}</mark> : label; | ||
|
||
return ( | ||
<Checkbox | ||
checked={Boolean(checked)} | ||
disabled | ||
label={checkboxLabel} | ||
vertical | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
VersionCheckbox.defaultProps = { | ||
checked: false, | ||
}; | ||
|
||
VersionCheckbox.propTypes = { | ||
checked: PropTypes.bool, | ||
label: PropTypes.node.isRequired, | ||
name: PropTypes.string.isRequired, | ||
}; |
37 changes: 37 additions & 0 deletions
37
lib/VersionHistory/components/VersionCheckbox/VersionCheckbox.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,37 @@ | ||
import { | ||
render, | ||
screen, | ||
} from '@testing-library/react'; | ||
|
||
import { VersionViewContext } from '../../VersionViewContext'; | ||
import { VersionCheckbox } from './VersionCheckbox'; | ||
|
||
const defaultProps = { | ||
label: 'Test Label', | ||
name: 'testName', | ||
}; | ||
|
||
const renderVersionCheckbox = (props = {}, contextValue = {}) => { | ||
return render( | ||
<VersionViewContext.Provider value={contextValue}> | ||
<VersionCheckbox | ||
{...defaultProps} | ||
{...props} | ||
/> | ||
</VersionViewContext.Provider>, | ||
); | ||
}; | ||
|
||
describe('VersionCheckbox', () => { | ||
it('renders with marked label when name is in context paths', () => { | ||
renderVersionCheckbox({}, { paths: ['testName'] }); | ||
|
||
expect(screen.getByText('Test Label').closest('mark')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders with normal label when name is not in context paths', () => { | ||
renderVersionCheckbox({}, { paths: ['otherName'] }); | ||
|
||
expect(screen.getByText('Test Label').closest('mark')).not.toBeInTheDocument(); | ||
}); | ||
}); |
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 @@ | ||
export { VersionCheckbox } from './VersionCheckbox'; |
49 changes: 49 additions & 0 deletions
49
lib/VersionHistory/components/VersionKeyValue/VersionKeyValue.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,49 @@ | ||
import PropTypes from 'prop-types'; | ||
import { | ||
useContext, | ||
useMemo, | ||
} from 'react'; | ||
|
||
import { | ||
KeyValue, | ||
NoValue, | ||
} from '@folio/stripes/components'; | ||
|
||
import { VersionViewContext } from '../../VersionViewContext'; | ||
|
||
export const VersionKeyValue = ({ | ||
children, | ||
label, | ||
multiple, | ||
name, | ||
value, | ||
}) => { | ||
const versionContext = useContext(VersionViewContext); | ||
const isUpdated = useMemo(() => ( | ||
multiple | ||
? versionContext?.paths?.find((field) => new RegExp(`^${name}\\[\\d\\]$`).test(field)) | ||
: versionContext?.paths?.includes(name) | ||
), [multiple, name, versionContext?.paths]); | ||
|
||
const content = (children || value) || <NoValue />; | ||
const displayValue = isUpdated ? <mark>{content}</mark> : content; | ||
|
||
return ( | ||
<KeyValue | ||
label={label} | ||
value={displayValue} | ||
/> | ||
); | ||
}; | ||
|
||
VersionKeyValue.defaultProps = { | ||
multiple: false, | ||
}; | ||
|
||
VersionKeyValue.propTypes = { | ||
children: PropTypes.node, | ||
label: PropTypes.node.isRequired, | ||
multiple: PropTypes.bool, | ||
name: PropTypes.string.isRequired, | ||
value: PropTypes.node, | ||
}; |
64 changes: 64 additions & 0 deletions
64
lib/VersionHistory/components/VersionKeyValue/VersionKeyValue.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,64 @@ | ||
import { | ||
render, | ||
screen, | ||
} from '@testing-library/react'; | ||
|
||
import { VersionViewContext } from '../../VersionViewContext'; | ||
import { VersionKeyValue } from './VersionKeyValue'; | ||
|
||
const defaultProps = { | ||
label: 'Test Label', | ||
value: 'Test Value', | ||
name: 'testName', | ||
}; | ||
|
||
const renderComponent = (props = {}, contextValue = {}) => { | ||
return render( | ||
<VersionViewContext.Provider value={contextValue}> | ||
<VersionKeyValue | ||
{...defaultProps} | ||
{...props} | ||
/> | ||
</VersionViewContext.Provider>, | ||
); | ||
}; | ||
|
||
describe('VersionKeyValue', () => { | ||
it('should render label and value', () => { | ||
renderComponent(); | ||
|
||
expect(screen.getByText('Test Label')).toBeInTheDocument(); | ||
expect(screen.getByText('Test Value')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render NoValue when value is not provided', () => { | ||
renderComponent({ value: undefined }); | ||
|
||
expect(screen.getByText('Test Label')).toBeInTheDocument(); | ||
expect(screen.getByText('stripes-components.noValue.noValueSet')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should highlight updated value', () => { | ||
renderComponent({ name: 'testName' }, { paths: ['testName'] }); | ||
|
||
expect(screen.getByText('Test Value').closest('mark')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not highlight non-updated value', () => { | ||
renderComponent({}, { paths: ['anotherName'] }); | ||
|
||
expect(screen.getByText('Test Value').closest('mark')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should highlight updated value for multiple fields', () => { | ||
renderComponent({ multiple: true }, { paths: ['testName[0]'] }); | ||
|
||
expect(screen.getByText('Test Value').closest('mark')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not highlight non-updated value for multiple fields', () => { | ||
renderComponent({ multiple: true }, { paths: ['anotherName[0]'] }); | ||
|
||
expect(screen.getByText('Test Value').closest('mark')).not.toBeInTheDocument(); | ||
}); | ||
}); |
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 @@ | ||
export { VersionKeyValue } from './VersionKeyValue'; |
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,76 @@ | ||
import PropTypes from 'prop-types'; | ||
import { | ||
memo, | ||
useMemo, | ||
} from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import { | ||
Layout, | ||
LoadingPane, | ||
Pane, | ||
PaneMenu, | ||
} from '@folio/stripes/components'; | ||
|
||
import { TagsBadge } from '../../../Tags'; | ||
import { VersionHistoryButton } from '../../VersionHistoryButton'; | ||
|
||
const VersionView = ({ | ||
children, | ||
id, | ||
isLoading, | ||
onClose, | ||
tags, | ||
versionId, | ||
...props | ||
}) => { | ||
const isVersionExist = Boolean(versionId && !isLoading); | ||
|
||
const lastMenu = useMemo(() => ( | ||
<PaneMenu> | ||
{tags && ( | ||
<TagsBadge | ||
disabled | ||
tagsQuantity={tags.length} | ||
/> | ||
)} | ||
<VersionHistoryButton disabled /> | ||
</PaneMenu> | ||
), [tags]); | ||
|
||
if (isLoading) return <LoadingPane />; | ||
|
||
return ( | ||
<Pane | ||
id={`${id}-version-view`} | ||
defaultWidth="fill" | ||
onClose={onClose} | ||
lastMenu={lastMenu} | ||
{...props} | ||
> | ||
{ | ||
isVersionExist | ||
? children | ||
: ( | ||
<Layout | ||
element="span" | ||
className="flex centerContent" | ||
> | ||
<FormattedMessage id="stripes-acq-components.versionHistory.noVersion" /> | ||
</Layout> | ||
) | ||
} | ||
</Pane> | ||
); | ||
}; | ||
|
||
VersionView.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
id: PropTypes.string.isRequired, | ||
isLoading: PropTypes.bool, | ||
onClose: PropTypes.func, | ||
tags: PropTypes.arrayOf(PropTypes.object), | ||
versionId: PropTypes.string, | ||
}; | ||
|
||
export default memo(VersionView); |
52 changes: 52 additions & 0 deletions
52
lib/VersionHistory/components/VersionView/VersionView.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,52 @@ | ||
import { | ||
render, | ||
screen, | ||
} from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import VersionView from './VersionView'; | ||
|
||
const defaultProps = { | ||
children: <div>Version Content</div>, | ||
id: 'test-id', | ||
isLoading: false, | ||
onClose: jest.fn(), | ||
tags: [{ id: 'tag1' }, { id: 'tag2' }], | ||
versionId: 'version1', | ||
dismissible: true, | ||
}; | ||
|
||
const renderComponent = (props = {}) => render( | ||
<VersionView | ||
{...defaultProps} | ||
{...props} | ||
/>, | ||
); | ||
|
||
describe('VersionView', () => { | ||
it('should render loading pane when isLoading is true', () => { | ||
renderComponent({ isLoading: true }); | ||
|
||
expect(screen.queryByText('Version Content')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render children when version exists and is not loading', () => { | ||
renderComponent(); | ||
|
||
expect(screen.getByText('Version Content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render no version message when version does not exist', () => { | ||
renderComponent({ versionId: null }); | ||
|
||
expect(screen.getByText('stripes-acq-components.versionHistory.noVersion')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call onClose when Pane onClose is triggered', async () => { | ||
renderComponent(); | ||
|
||
await userEvent.click(screen.getByRole('button', { name: 'stripes-components.closeItem' })); | ||
|
||
expect(defaultProps.onClose).toHaveBeenCalled(); | ||
}); | ||
}); |
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 @@ | ||
export { default as VersionView } from './VersionView'; |
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,3 @@ | ||
export { VersionCheckbox } from './VersionCheckbox'; | ||
export { VersionKeyValue } from './VersionKeyValue'; | ||
export { VersionView } from './VersionView'; |
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
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
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
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
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
Oops, something went wrong.