-
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-228 Add more reusable hooks and utilities (#828)
* UISACQCOMP-228 Add more reusable hooks and utilities * UISACQCOMP-228 Add shared API utils to fetch ACQ entities * add constant for allRecords cql * add tests * add unit test * update date query builder
- Loading branch information
1 parent
ad2522d
commit d543693
Showing
23 changed files
with
366 additions
and
5 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
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 { useAddresses } from './useAddresses'; |
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,57 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
import { | ||
useNamespace, | ||
useOkapiKy, | ||
} from '@folio/stripes/core'; | ||
|
||
import { | ||
CONFIG_API, | ||
LIMIT_MAX, | ||
} from '../../constants'; | ||
import { getAddresses } from '../../utils'; | ||
|
||
const MODULE_TENANT = 'TENANT'; | ||
const CONFIG_ADDRESSES = 'tenant.addresses'; | ||
const DEFAULT_DATA = []; | ||
|
||
export const useAddresses = (options = {}) => { | ||
const { | ||
enabled = true, | ||
tenantId, | ||
...queryOptions | ||
} = options; | ||
|
||
const [namespace] = useNamespace({ key: 'acquisitions-units' }); | ||
const ky = useOkapiKy({ tenant: tenantId }); | ||
|
||
const searchParams = { | ||
query: `(module=${MODULE_TENANT} and configName=${CONFIG_ADDRESSES})`, | ||
limit: LIMIT_MAX, | ||
}; | ||
|
||
const { | ||
data, | ||
isFetching, | ||
isLoading, | ||
} = useQuery({ | ||
queryKey: [namespace, tenantId], | ||
queryFn: ({ signal }) => { | ||
return ky.get(CONFIG_API, { searchParams, signal }) | ||
.json() | ||
.then(({ configs, totalRecords }) => ({ | ||
addresses: getAddresses(configs), | ||
totalRecords, | ||
})); | ||
}, | ||
enabled, | ||
...queryOptions, | ||
}); | ||
|
||
return ({ | ||
addresses: data?.addresses || DEFAULT_DATA, | ||
totalRecords: data?.totalRecords || 0, | ||
isFetching, | ||
isLoading, | ||
}); | ||
}; |
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,51 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { | ||
QueryClient, | ||
QueryClientProvider, | ||
} from 'react-query'; | ||
|
||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { getAddresses } from '../../utils'; | ||
import { useAddresses } from './useAddresses'; | ||
|
||
jest.mock('../../utils', () => ({ | ||
getAddresses: jest.fn(), | ||
})); | ||
|
||
const queryClient = new QueryClient(); | ||
const wrapper = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
|
||
describe('useAddresses', () => { | ||
const mockGet = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
useOkapiKy.mockReturnValue({ | ||
get: mockGet, | ||
}); | ||
}); | ||
|
||
it('should return addresses and totalRecords', async () => { | ||
const configs = [{ value: 'address1' }, { value: 'address2' }]; | ||
const totalRecords = 2; | ||
|
||
mockGet.mockReturnValue({ | ||
json: jest.fn().mockResolvedValue({ configs, totalRecords }), | ||
}); | ||
|
||
getAddresses.mockReturnValue(['address1', 'address2']); | ||
|
||
const { result, waitFor } = renderHook(() => useAddresses(), { wrapper }); | ||
|
||
await waitFor(() => !result.current.isLoading); | ||
|
||
expect(result.current.addresses).toEqual(['address1', 'address2']); | ||
expect(result.current.totalRecords).toBe(2); | ||
}); | ||
}); |
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,113 @@ | ||
import { | ||
LIMIT_PARAMETER, | ||
SEARCH_PARAMETER, | ||
} from '../../AcqList/constants'; | ||
import { | ||
LIMIT_MAX, | ||
LINES_API, | ||
ORDER_PIECES_API, | ||
ORDERS_API, | ||
RECEIVING_TITLES_API, | ||
VENDORS_API, | ||
} from '../../constants'; | ||
|
||
import { fetchOrderLines } from './fetchOrderLines'; | ||
import { fetchOrderLinesByIds } from './fetchOrderLinesByIds'; | ||
import { fetchOrders } from './fetchOrders'; | ||
import { fetchOrdersByIds } from './fetchOrdersByIds'; | ||
import { fetchOrganizations } from './fetchOrganizations'; | ||
import { fetchOrganizationsByIds } from './fetchOrganizationsByIds'; | ||
import { fetchPieces } from './fetchPieces'; | ||
import { fetchReceivingTitles } from './fetchReceivingTitles'; | ||
import { fetchReceivingTitlesByIds } from './fetchReceivingTitlesByIds'; | ||
|
||
const httpClient = { | ||
get: jest.fn(() => ({ | ||
json: jest.fn(() => Promise.resolve({})), | ||
})), | ||
}; | ||
|
||
const ids = ['1', '2']; | ||
const options = {}; | ||
const searchParams = { | ||
[LIMIT_PARAMETER]: LIMIT_MAX, | ||
[SEARCH_PARAMETER]: 'id==1 or id==2', | ||
}; | ||
|
||
describe('API utils', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('fetchOrderLines', () => { | ||
it('should fetch order lines', async () => { | ||
await fetchOrderLines(httpClient)(options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(LINES_API, options); | ||
}); | ||
}); | ||
|
||
describe('fetchOrderLinesByIds', () => { | ||
it('should fetch order lines by ids', async () => { | ||
await fetchOrderLinesByIds(httpClient)(ids, options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(LINES_API, { searchParams }); | ||
}); | ||
}); | ||
|
||
describe('fetchOrders', () => { | ||
it('should fetch orders', async () => { | ||
await fetchOrders(httpClient)(options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(ORDERS_API, options); | ||
}); | ||
}); | ||
|
||
describe('fetchOrdersByIds', () => { | ||
it('should fetch orders by ids', async () => { | ||
await fetchOrdersByIds(httpClient)(ids, options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(ORDERS_API, { searchParams }); | ||
}); | ||
}); | ||
|
||
describe('fetchOrganizations', () => { | ||
it('should fetch organizations', async () => { | ||
await fetchOrganizations(httpClient)(options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(VENDORS_API, options); | ||
}); | ||
}); | ||
|
||
describe('fetchOrganizationsByIds', () => { | ||
it('should fetch organizations by ids', async () => { | ||
await fetchOrganizationsByIds(httpClient)(ids, options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(VENDORS_API, { searchParams }); | ||
}); | ||
}); | ||
|
||
describe('fetchPieces', () => { | ||
it('should fetch pieces', async () => { | ||
await fetchPieces(httpClient)(options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(ORDER_PIECES_API, options); | ||
}); | ||
}); | ||
|
||
describe('fetchReceivingTitles', () => { | ||
it('should fetch receiving titles', async () => { | ||
await fetchReceivingTitles(httpClient)(options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(RECEIVING_TITLES_API, options); | ||
}); | ||
}); | ||
|
||
describe('fetchReceivingTitlesByIds', () => { | ||
it('should fetch receiving titles by ids', async () => { | ||
await fetchReceivingTitlesByIds(httpClient)(ids, options); | ||
|
||
expect(httpClient.get).toHaveBeenCalledWith(RECEIVING_TITLES_API, { searchParams }); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
import { LINES_API } from '../../constants'; | ||
|
||
export const fetchOrderLines = (httpClient) => async (options) => { | ||
return httpClient.get(LINES_API, options).json(); | ||
}; |
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,19 @@ | ||
import { batchRequest } from '../batchFetch'; | ||
import { fetchOrderLines } from './fetchOrderLines'; | ||
|
||
export const fetchOrderLinesByIds = (httpClient) => { | ||
return (ids, options) => { | ||
const requestFn = ({ params }) => { | ||
return fetchOrderLines(httpClient)({ ...options, searchParams: params }); | ||
}; | ||
|
||
return batchRequest(requestFn, ids).then((responses) => { | ||
return responses.reduce((acc, response) => { | ||
return { | ||
poLines: acc.poLines.concat(response.poLines), | ||
totalRecords: acc.totalRecords + response.totalRecords, | ||
}; | ||
}, { poLines: [], totalRecords: 0 }); | ||
}); | ||
}; | ||
}; |
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 @@ | ||
import { ORDERS_API } from '../../constants'; | ||
|
||
export const fetchOrders = (httpClient) => async (options) => { | ||
return httpClient.get(ORDERS_API, options).json(); | ||
}; |
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,19 @@ | ||
import { batchRequest } from '../batchFetch'; | ||
import { fetchOrders } from './fetchOrders'; | ||
|
||
export const fetchOrdersByIds = (httpClient) => { | ||
return (ids, options) => { | ||
const requestFn = ({ params }) => { | ||
return fetchOrders(httpClient)({ ...options, searchParams: params }); | ||
}; | ||
|
||
return batchRequest(requestFn, ids).then((responses) => { | ||
return responses.reduce((acc, response) => { | ||
return { | ||
purchaseOrders: acc.purchaseOrders.concat(response.purchaseOrders), | ||
totalRecords: acc.totalRecords + response.totalRecords, | ||
}; | ||
}, { purchaseOrders: [], totalRecords: 0 }); | ||
}); | ||
}; | ||
}; |
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,7 @@ | ||
import { VENDORS_API } from '../../constants'; | ||
|
||
export const fetchOrganizations = (httpClient) => { | ||
return async (options) => { | ||
return httpClient.get(VENDORS_API, options).json(); | ||
}; | ||
}; |
Oops, something went wrong.