-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STSMACOM-886 provide LinkedUser component (#1555)
Link to a user-details record if permissions allow, or return a plaintext name. Mocks shamelessly copied from ui-users. Refs STSMACOM-886
- Loading branch information
Showing
27 changed files
with
2,558 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require('path'); | ||
const config = require('@folio/jest-config-stripes'); | ||
|
||
module.exports = { | ||
...config, | ||
setupFiles: [ | ||
...config.setupFiles, | ||
path.join(__dirname, './tests/jest/setupFiles.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,32 @@ | ||
import PropTypes from 'prop-types'; | ||
|
||
import { useStripes } from '@folio/stripes-core'; | ||
import { getFullName } from '@folio/stripes-util'; | ||
import { TextLink } from '@folio/stripes-components'; | ||
|
||
/** | ||
* LinkedUser | ||
* Link to a user-details record if permissions allow, | ||
* or return a plaintext name. | ||
*/ | ||
const LinkedUser = ({ user, formatter = getFullName }) => { | ||
const stripes = useStripes(); | ||
|
||
return stripes.hasPerm('ui-users.view') ? ( | ||
<TextLink to={`/users/preview/${user.id}?query=${user.username}`}> | ||
{formatter(user)} | ||
</TextLink> | ||
) : ( | ||
<>{formatter(user)}</> | ||
); | ||
}; | ||
|
||
LinkedUser.propTypes = { | ||
user: PropTypes.shape({ | ||
id: PropTypes.string, | ||
username: PropTypes.string, | ||
}), | ||
formatter: PropTypes.func, | ||
}; | ||
|
||
export default LinkedUser; |
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 '@folio/jest-config-stripes/testing-library/react'; | ||
import { useStripes } from '@folio/stripes-core'; | ||
|
||
import LinkedUser from './LinkedUser'; | ||
|
||
const mockHasPerm = jest.fn(); | ||
|
||
jest.mock('stripes-config', () => ( | ||
{ | ||
modules: [], | ||
metadata: {}, | ||
} | ||
), | ||
{ virtual: true }); | ||
|
||
jest.mock('@folio/stripes-core', () => ({ | ||
...jest.requireActual('@folio/stripes-core'), | ||
useStripes: () => ({ hasPerm: mockHasPerm }), | ||
})); | ||
|
||
const userRecord = { | ||
id: 'id', | ||
username: 'username', | ||
personal: { | ||
firstName: 'first', | ||
lastName: 'last', | ||
middleName: 'middle', | ||
} | ||
}; | ||
|
||
describe('useLinkedUser', () => { | ||
beforeEach(() => { | ||
mockHasPerm.mockReturnValue(true); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('returns a link when permission is present', () => { | ||
mockHasPerm.mockReturnValue(true); | ||
|
||
const { container } = render(<LinkedUser user={userRecord} />); | ||
expect(container.querySelector('a')).not.toBeNull(); | ||
screen.getByText(userRecord.personal.firstName, { exact: false }); | ||
}); | ||
|
||
it('returns plain text when permission is absent', () => { | ||
mockHasPerm.mockReturnValue(false); | ||
|
||
const { container } = render(<LinkedUser user={userRecord} />); | ||
expect(container.querySelector('a')).toBeNull(); | ||
screen.getByText(userRecord.personal.firstName, { exact: false }); | ||
}); | ||
|
||
it('uses provided formatter', () => { | ||
mockHasPerm.mockReturnValue(true); | ||
const formatter = (u) => u.username; | ||
|
||
const { container } = render(<LinkedUser user={userRecord} formatter={formatter} />); | ||
expect(container.querySelector('a')).not.toBeNull(); | ||
screen.getByText(userRecord.username); | ||
}); | ||
}); |
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 } from './LinkedUser'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
window.ResizeObserver = jest.fn().mockImplementation(() => ({ | ||
disconnect: jest.fn(), | ||
observe: jest.fn(), | ||
unobserve: jest.fn(), | ||
})); | ||
|
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 @@ | ||
jest.mock('currency-codes/data', () => ({ filter: () => [] })); |
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,8 @@ | ||
import './stripesConfig.mock'; | ||
import './stripesCore.mock'; | ||
import './stripes.mock'; | ||
import './intl.mock'; | ||
import './stripesIcon.mock'; | ||
import './stripesComponents.mock'; | ||
import './currencyData.mock'; | ||
import './resizeObserver.mock'; |
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,61 @@ | ||
import React from 'react'; | ||
|
||
jest.mock('react-intl', () => { | ||
const intl = { | ||
formatMessage: ({ id }) => id, | ||
formatNumber: (value) => value, | ||
formatTime: (value) => value, | ||
formatDisplayName: (value) => value, | ||
formatDate: (value) => value, | ||
formatDateToParts: jest.fn(() => ([ | ||
{ | ||
'type': 'month', | ||
'value': '7' | ||
}, | ||
{ | ||
'type': 'literal', | ||
'value': '/' | ||
}, | ||
{ | ||
'type': 'day', | ||
'value': '31' | ||
}, | ||
{ | ||
'type': 'literal', | ||
'value': '/' | ||
}, | ||
{ | ||
'type': 'year', | ||
'value': '2024' | ||
} | ||
] | ||
)), | ||
}; | ||
|
||
return { | ||
...jest.requireActual('react-intl'), | ||
FormattedMessage: jest.fn(({ id, children }) => { | ||
if (children) { | ||
return children([id]); | ||
} | ||
|
||
return id; | ||
}), | ||
FormattedTime: jest.fn(({ value, children }) => { | ||
if (children) { | ||
return children([value]); | ||
} | ||
|
||
return value; | ||
}), | ||
FormattedDate: jest.fn(({ value, children }) => { | ||
if (children) { | ||
return children([value]); | ||
} | ||
|
||
return value; | ||
}), | ||
useIntl: () => intl, | ||
injectIntl: (Component) => (props) => <Component {...props} intl={intl} />, | ||
}; | ||
}); |
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,13 @@ | ||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation(query => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // Deprecated | ||
removeListener: jest.fn(), // Deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})) | ||
}); |
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,9 @@ | ||
import React from 'react'; | ||
|
||
jest.mock('react-final-form-arrays', () => { | ||
return { | ||
...jest.requireActual('react-final-form-arrays'), | ||
|
||
FieldArray: () => <div>FieldArray</div>, | ||
}; | ||
}); |
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,9 @@ | ||
import React from 'react'; | ||
|
||
jest.mock('react-final-form-listeners', () => { | ||
return { | ||
...jest.requireActual('react-final-form-listeners'), | ||
|
||
OnChange: jest.fn(({ children }) => <>{children()}</>), | ||
}; | ||
}); |
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,5 @@ | ||
global.ResizeObserver = jest.fn().mockImplementation(() => ({ | ||
observe: jest.fn(), | ||
unobserve: jest.fn(), | ||
disconnect: jest.fn(), | ||
})); |
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,26 @@ | ||
import { noop } from 'lodash'; | ||
|
||
const buildStripes = (otherProperties = {}) => ({ | ||
hasPerm: noop, | ||
hasInterface: noop, | ||
clone: noop, | ||
logger: { log: noop }, | ||
config: {}, | ||
okapi: { | ||
url: '', | ||
tenant: '', | ||
}, | ||
locale: 'en-US', | ||
withOkapi: true, | ||
setToken: noop, | ||
actionNames: [], | ||
setLocale: noop, | ||
setTimezone: noop, | ||
setCurrency: noop, | ||
setSinglePlugin: noop, | ||
setBindings: noop, | ||
connect: noop, | ||
...otherProperties, | ||
}); | ||
|
||
export default buildStripes; |
Oops, something went wrong.