-
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.
Add reusable custom hook useVersionHistoryValueResolvers to display p…
…roper object property and user full name
- Loading branch information
1 parent
03b5d13
commit 0299976
Showing
4 changed files
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useVersionHistoryValueResolvers } from './useVersionHistoryValueResolvers'; |
42 changes: 42 additions & 0 deletions
42
lib/hooks/useVersionHistoryValueResolvers/useVersionHistoryValueResolvers.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,42 @@ | ||
/* Developed collaboratively using AI (Chat GPT) */ | ||
|
||
import { useIntl } from 'react-intl'; | ||
|
||
import { getFullName } from '@folio/stripes/util'; | ||
|
||
export const useVersionHistoryValueResolvers = () => { | ||
const intl = useIntl(); | ||
const deletedRecordLabel = intl.formatMessage({ id: 'stripes-acq-components.versionHistory.deletedRecord' }); | ||
|
||
/** | ||
* Resolves a value from a map by ID and property. | ||
* @param {string} id - The ID to resolve. | ||
* @param {string} property - The property to retrieve from the map entry. | ||
* @param {Object} obj - The map object containing items. | ||
* @returns {string | null} - Resolved value or fallback label. | ||
*/ | ||
const getObjectPropertyById = (id, property, obj = {}) => { | ||
if (!id) return null; | ||
if (id in obj) { | ||
return obj[id]?.[property] || null; | ||
} | ||
|
||
return deletedRecordLabel; | ||
}; | ||
|
||
/** | ||
* Resolves a user's full name by ID using a users map. | ||
* @param {string} id - The user ID to resolve. | ||
* @param {Object} usersMap - The map of user objects. | ||
* @returns {string | null} - Full name of the user or fallback label. | ||
*/ | ||
const getUserFullNameById = (id, usersMap = {}) => { | ||
if (!id) return null; | ||
|
||
return id in usersMap | ||
? getFullName(usersMap[id]) | ||
: deletedRecordLabel; | ||
}; | ||
|
||
return { getObjectPropertyById, getUserFullNameById }; | ||
}; |
91 changes: 91 additions & 0 deletions
91
lib/hooks/useVersionHistoryValueResolvers/useVersionHistoryValueResolvers.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,91 @@ | ||
/* Developed collaboratively using AI (Chat GPT) */ | ||
|
||
import { useIntl } from 'react-intl'; | ||
|
||
import { renderHook } from '@folio/jest-config-stripes/testing-library/react'; | ||
import { getFullName } from '@folio/stripes/util'; | ||
|
||
import { useVersionHistoryValueResolvers } from './useVersionHistoryValueResolvers'; | ||
|
||
jest.mock('react-intl', () => ({ | ||
useIntl: jest.fn(), | ||
})); | ||
|
||
jest.mock('@folio/stripes/util', () => ({ | ||
getFullName: jest.fn(), | ||
})); | ||
|
||
describe('useVersionHistoryValueResolvers', () => { | ||
let intlMock; | ||
|
||
beforeEach(() => { | ||
intlMock = { | ||
formatMessage: jest.fn(({ id }) => id), | ||
}; | ||
useIntl.mockReturnValue(intlMock); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return object property by ID if ID and property exist in the map', () => { | ||
const { result } = renderHook(() => useVersionHistoryValueResolvers()); | ||
|
||
const obj = { | ||
id1: { property1: 'value1', property2: 'value2' }, | ||
id2: { property1: 'value3' }, | ||
}; | ||
|
||
const value = result.current.getObjectPropertyById('id1', 'property1', obj); | ||
|
||
expect(value).toBe('value1'); | ||
}); | ||
|
||
it('should return null when ID is not provided', () => { | ||
const { result } = renderHook(() => useVersionHistoryValueResolvers()); | ||
const obj = { id1: { property1: 'value1' } }; | ||
|
||
const value = result.current.getObjectPropertyById(null, 'property1', obj); | ||
|
||
expect(value).toBeNull(); | ||
}); | ||
|
||
it('should return deletedRecordLabel when ID is not found in the object', () => { | ||
const { result } = renderHook(() => useVersionHistoryValueResolvers()); | ||
|
||
const obj = { id1: { property1: 'value1' } }; | ||
const value = result.current.getObjectPropertyById('id2', 'property1', obj); | ||
|
||
expect(value).toBe('stripes-acq-components.versionHistory.deletedRecord'); | ||
}); | ||
|
||
it('should return null when ID is not provided for getUserFullNameById', () => { | ||
const { result } = renderHook(() => useVersionHistoryValueResolvers()); | ||
const usersMap = { user1: { firstName: 'John', lastName: 'Doe' } }; | ||
|
||
const value = result.current.getUserFullNameById(null, usersMap); | ||
|
||
expect(value).toBeNull(); | ||
}); | ||
|
||
it('should return user full name by ID using the users map', () => { | ||
getFullName.mockReturnValue('John Doe'); | ||
const { result } = renderHook(() => useVersionHistoryValueResolvers()); | ||
|
||
const usersMap = { user1: { firstName: 'John', lastName: 'Doe' } }; | ||
const value = result.current.getUserFullNameById('user1', usersMap); | ||
|
||
expect(getFullName).toHaveBeenCalledWith(usersMap.user1); | ||
expect(value).toBe('John Doe'); | ||
}); | ||
|
||
it('should return deletedRecordLabel when ID is not found in the users map', () => { | ||
const { result } = renderHook(() => useVersionHistoryValueResolvers()); | ||
|
||
const usersMap = { user1: { firstName: 'John', lastName: 'Doe' } }; | ||
const value = result.current.getUserFullNameById('user2', usersMap); | ||
|
||
expect(value).toBe('stripes-acq-components.versionHistory.deletedRecord'); | ||
}); | ||
}); |