-
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.
UINV-522: display the exchanged calculated total amount when invoice …
…in foreign currency (#757) * UINV-522: display exchanged amount * tests: fix failing snapshot tests * add changelog * debounce api request and rename component name * tests: update failing snapshots * disable usecallback linter warning * tests: remove failing snapshot tests * tests: add missing snapshots * rename function name * update `yarn test` command to update snapshot tests * remove `--updateSnapshot` flag after fixing snapshot test issue * regenerate snapshot tests * refactor exchange calculation hook * refactor `useExchangeCalculation` hook * tests: update failing snapshot tests * inline `useExchangeRateValue` hook arguments in one line * fix sonar issue `Remove this unused import of 'useStripes'.`
- Loading branch information
1 parent
8e38736
commit 31a3fe9
Showing
22 changed files
with
377 additions
and
410 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
40 changes: 40 additions & 0 deletions
40
src/common/components/CalculatedExchangeAmount/CalculatedExchangeAmount.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 { FormattedMessage } from 'react-intl'; | ||
|
||
import { AmountWithCurrencyField } from '@folio/stripes-acq-components'; | ||
import { KeyValue } from '@folio/stripes/components'; | ||
import { useStripes } from '@folio/stripes/core'; | ||
|
||
import { useExchangeCalculation } from '../../hooks'; | ||
|
||
export const CalculatedExchangeAmount = ({ currency, exchangeRate, total }) => { | ||
const stripes = useStripes(); | ||
const systemCurrency = stripes.currency; | ||
const enabled = Boolean(systemCurrency !== currency && total); | ||
|
||
const { exchangedAmount } = useExchangeCalculation({ | ||
amount: +total, | ||
from: currency, | ||
rate: +exchangeRate, | ||
to: systemCurrency, | ||
}, { enabled }); | ||
|
||
if (!enabled) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<KeyValue label={<FormattedMessage id="ui-invoice.invoice.details.information.calculatedTotalExchangeAmount" />}> | ||
<AmountWithCurrencyField | ||
amount={exchangedAmount || total} | ||
currency={systemCurrency} | ||
/> | ||
</KeyValue> | ||
); | ||
}; | ||
|
||
CalculatedExchangeAmount.propTypes = { | ||
currency: PropTypes.string, | ||
exchangeRate: PropTypes.number, | ||
total: PropTypes.number, | ||
}; |
46 changes: 46 additions & 0 deletions
46
src/common/components/CalculatedExchangeAmount/CalculatedExchangeAmount.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,46 @@ | ||
import { render, screen } from '@folio/jest-config-stripes/testing-library/react'; | ||
|
||
import { useExchangeCalculation } from '../../hooks'; | ||
import { CalculatedExchangeAmount } from './CalculatedExchangeAmount'; | ||
|
||
jest.mock('@folio/stripes-acq-components', () => ({ | ||
...jest.requireActual('@folio/stripes-acq-components'), | ||
AmountWithCurrencyField: jest.fn(() => 'AmountWithCurrencyField'), | ||
})); | ||
jest.mock('../../hooks', () => ({ | ||
...jest.requireActual('../../hooks'), | ||
useExchangeCalculation: jest.fn(), | ||
})); | ||
|
||
const renderComponent = (props = {}) => render( | ||
<CalculatedExchangeAmount {...props} />, | ||
); | ||
|
||
describe('CalculatedExchangeAmount', () => { | ||
beforeEach(() => { | ||
useExchangeCalculation.mockClear().mockReturnValue({ | ||
isLoading: false, | ||
exchangedAmount: 30, | ||
}); | ||
}); | ||
|
||
it('should not render component', async () => { | ||
renderComponent({ | ||
currency: 'USD', | ||
exchangeRate: 1, | ||
total: 30, | ||
}); | ||
|
||
expect(screen.queryByText(/30/)).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render calculated exchange amount', async () => { | ||
renderComponent({ | ||
currency: 'EUR', | ||
exchangeRate: 1, | ||
total: 30, | ||
}); | ||
|
||
expect(screen.getByText('ui-invoice.invoice.details.information.calculatedTotalExchangeAmount')).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 { CalculatedExchangeAmount } from './CalculatedExchangeAmount'; |
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 { useExchangeCalculation } from './useExchangeCalculation'; |
71 changes: 71 additions & 0 deletions
71
src/common/hooks/useExchangeCalculation/useExchangeCalculation.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,71 @@ | ||
import { debounce } from 'lodash'; | ||
import { | ||
useCallback, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
import { useQuery } from 'react-query'; | ||
|
||
import { useExchangeRateValue } from '@folio/stripes-acq-components'; | ||
import { | ||
useOkapiKy, | ||
useNamespace, | ||
} from '@folio/stripes/core'; | ||
|
||
import { CALCULATE_EXCHANGE_API } from '../../constants'; | ||
|
||
const DEBOUNCE_DELAY = 500; | ||
|
||
export const useExchangeCalculation = ({ from, to, amount, rate }, options = {}) => { | ||
const { enabled = true, ...otherOptions } = options; | ||
const ky = useOkapiKy(); | ||
const [namespace] = useNamespace({ key: 'exchange-calculation' }); | ||
|
||
const { exchangeRate } = useExchangeRateValue(from, to, rate); | ||
|
||
const [searchParams, setSearchParams] = useState({ | ||
amount, | ||
from, | ||
rate: rate || exchangeRate, | ||
to, | ||
}); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const debounceSetSearchParams = useCallback(debounce(() => { | ||
setSearchParams({ | ||
amount, | ||
from, | ||
rate: rate || exchangeRate, | ||
to, | ||
}); | ||
}, DEBOUNCE_DELAY), [amount, from, rate, to, exchangeRate]); | ||
|
||
useEffect(() => { | ||
debounceSetSearchParams(); | ||
|
||
return () => debounceSetSearchParams.cancel(); | ||
}, [amount, debounceSetSearchParams, from, rate, to, exchangeRate]); | ||
|
||
const { | ||
amount: amountProp, | ||
from: fromProp, | ||
rate: rateProp, | ||
to: toProp, | ||
} = searchParams; | ||
|
||
const { data, isLoading, isFetching } = useQuery( | ||
[namespace, amountProp, fromProp, rateProp, toProp], | ||
({ signal }) => ky.get(`${CALCULATE_EXCHANGE_API}`, { searchParams, signal }).json(), | ||
{ | ||
keepPreviousData: true, | ||
...otherOptions, | ||
enabled: enabled && Boolean(amountProp && fromProp && rateProp && toProp), | ||
}, | ||
); | ||
|
||
return ({ | ||
exchangedAmount: data, | ||
isFetching, | ||
isLoading, | ||
}); | ||
}; |
53 changes: 53 additions & 0 deletions
53
src/common/hooks/useExchangeCalculation/useExchangeCalculation.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,53 @@ | ||
import { | ||
QueryClient, | ||
QueryClientProvider, | ||
} from 'react-query'; | ||
|
||
import { renderHook, waitFor } from '@folio/jest-config-stripes/testing-library/react'; | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { CALCULATE_EXCHANGE_API } from '../../constants'; | ||
import { useExchangeCalculation } from './useExchangeCalculation'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
// eslint-disable-next-line react/prop-types | ||
const wrapper = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
|
||
const kyMock = { | ||
get: jest.fn(() => ({ | ||
json: () => Promise.resolve(30), | ||
})), | ||
}; | ||
|
||
const searchParams = { | ||
from: 'USD', | ||
to: 'EUR', | ||
amount: 100, | ||
rate: 1.2, | ||
}; | ||
|
||
describe('useExchangeCalculation', () => { | ||
beforeEach(() => { | ||
kyMock.get.mockClear(); | ||
useOkapiKy.mockClear().mockReturnValue(kyMock); | ||
}); | ||
|
||
it('should return calculated exchange amount', async () => { | ||
const { result } = renderHook(() => useExchangeCalculation(searchParams), { wrapper }); | ||
|
||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
|
||
expect(kyMock.get).toHaveBeenCalledWith( | ||
`${CALCULATE_EXCHANGE_API}`, | ||
expect.objectContaining({ | ||
searchParams, | ||
}), | ||
); | ||
expect(result.current.exchangedAmount).toEqual(30); | ||
}); | ||
}); |
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.