-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIBULKED-403: Integrate query-plugin with bulk-edit API (#461)
- Loading branch information
1 parent
3ddd679
commit a3369fa
Showing
12 changed files
with
276 additions
and
66 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
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,50 @@ | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
export const useQueryPlugin = (recordType) => { | ||
const ky = useOkapiKy(); | ||
|
||
const entityTypeDataSource = async () => { | ||
if (recordType) { | ||
const response = ky.get(`entity-types/${recordType}`); | ||
return response.json(); | ||
} | ||
return null; | ||
}; | ||
|
||
const queryDetailsDataSource = async ({ queryId, includeContent, offset, limit }) => { | ||
const searchParams = { | ||
includeResults: includeContent, | ||
offset, | ||
limit | ||
}; | ||
|
||
const response = ky.get(`query/${queryId}`, { searchParams }); | ||
|
||
return response.json(); | ||
}; | ||
|
||
const testQueryDataSource = async ({ fqlQuery }) => { | ||
const response = ky.post('query', { json: { | ||
entityTypeId: recordType, | ||
fqlQuery: JSON.stringify(fqlQuery) | ||
} }); | ||
return response.json(); | ||
}; | ||
|
||
const getParamsSource = async ({ entityTypeId, columnName, searchValue }) => { | ||
const response = ky.get(`entity-types/${entityTypeId}/columns/${columnName}/values?search=${searchValue}`); | ||
return response.json(); | ||
}; | ||
|
||
const cancelQueryDataSource = async ({ queryId }) => { | ||
return ky.delete(`query/${queryId}`); | ||
}; | ||
|
||
return { | ||
entityTypeDataSource, | ||
queryDetailsDataSource, | ||
testQueryDataSource, | ||
getParamsSource, | ||
cancelQueryDataSource, | ||
}; | ||
}; |
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,23 @@ | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
import { useQuery } from 'react-query'; | ||
|
||
const ENTITY_TYPE_HASH = 'entityType'; | ||
|
||
export const useRecordTypes = () => { | ||
const ky = useOkapiKy(); | ||
const { data, isLoading, error } = useQuery({ | ||
queryKey: [ENTITY_TYPE_HASH], | ||
queryFn: async () => { | ||
const response = await ky.get('entity-types'); | ||
|
||
return response.json(); | ||
}, | ||
refetchOnWindowFocus: false, | ||
}); | ||
|
||
return ({ | ||
recordTypes: data, | ||
isLoading, | ||
error | ||
}); | ||
}; |
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,63 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
import { useQuery } from 'react-query'; | ||
import { act } from '@testing-library/react'; | ||
import { useRecordTypes } from './useRecordTypes'; | ||
|
||
jest.mock('@folio/stripes/core', () => ({ | ||
useOkapiKy: jest.fn(), | ||
})); | ||
|
||
jest.mock('react-query', () => ({ | ||
...jest.requireActual('react-query'), | ||
useQuery: jest.fn(), | ||
})); | ||
|
||
describe('useRecordTypes', () => { | ||
it('should return recordTypes, loading state, and error state', async () => { | ||
useOkapiKy.mockReturnValue({ | ||
get: jest.fn().mockResolvedValue({ json: jest.fn().mockResolvedValue({}) }), | ||
}); | ||
|
||
useQuery.mockReturnValue({ | ||
data: [ | ||
{ | ||
'id': '1', | ||
'label': 'Loans' | ||
}, | ||
{ | ||
'id': '2', | ||
'label': 'Users' | ||
}, | ||
{ | ||
'id': '3', | ||
'label': 'Items' | ||
} | ||
], | ||
isLoading: false, | ||
error: null, | ||
}); | ||
|
||
let result; | ||
await act(async () => { | ||
result = renderHook(() => useRecordTypes()).result; | ||
}); | ||
|
||
expect(result.current.recordTypes).toEqual([ | ||
{ | ||
'id': '1', | ||
'label': 'Loans' | ||
}, | ||
{ | ||
'id': '2', | ||
'label': 'Users' | ||
}, | ||
{ | ||
'id': '3', | ||
'label': 'Items' | ||
} | ||
]); | ||
expect(result.current.isLoading).toBeFalsy(); | ||
expect(result.current.error).toBeNull(); | ||
}); | ||
}); |
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,9 @@ | ||
import { CAPABILITIES, RECORD_TYPES } from '../constants'; | ||
|
||
export const getRecordType = (capability) => { | ||
if (Object.hasOwn(CAPABILITIES, capability)) { | ||
return RECORD_TYPES[CAPABILITIES[capability]]; | ||
} else { | ||
return null; | ||
} | ||
}; |
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,18 @@ | ||
import { getRecordType } from './getRecordType'; | ||
import { CAPABILITIES, RECORD_TYPES } from '../constants'; | ||
|
||
describe('getRecordType', () => { | ||
it('should return the correct record type for a valid capability', () => { | ||
// Test each capability | ||
Object.keys(CAPABILITIES).forEach((capability) => { | ||
const result = getRecordType(capability); | ||
expect(result).toEqual(RECORD_TYPES[CAPABILITIES[capability]]); | ||
}); | ||
}); | ||
|
||
it('should return null for an invalid capability', () => { | ||
const invalidCapability = 'INVALID_CAPABILITY'; | ||
const result = getRecordType(invalidCapability); | ||
expect(result).toBeNull(); | ||
}); | ||
}); |
Oops, something went wrong.