-
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.
- Loading branch information
1 parent
cfffaa0
commit 9336bbf
Showing
19 changed files
with
307 additions
and
93 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/common/components/DisplayExchangedAmount/DisplayExchangedAmount.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 @@ | ||
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 DisplayExchangedAmount = ({ currency, exchangeRate, total }) => { | ||
const stripes = useStripes(); | ||
|
||
const systemCurrency = stripes.currency; | ||
const enabled = systemCurrency !== currency; | ||
|
||
const { exchangedAmount } = useExchangeCalculation({ | ||
to: systemCurrency, | ||
from: currency, | ||
amount: +total, | ||
rate: +exchangeRate, | ||
}, | ||
{ enabled }); | ||
|
||
if (!enabled) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<KeyValue label={<FormattedMessage id="ui-invoice.invoice.details.information.calculatedTotalExchangeAmount" />}> | ||
<AmountWithCurrencyField | ||
amount={exchangedAmount || total} | ||
currency={systemCurrency} | ||
/> | ||
</KeyValue> | ||
); | ||
}; | ||
|
||
DisplayExchangedAmount.propTypes = { | ||
currency: PropTypes.string, | ||
exchangeRate: PropTypes.number, | ||
total: PropTypes.number, | ||
}; |
46 changes: 46 additions & 0 deletions
46
src/common/components/DisplayExchangedAmount/DisplayExchangedAmount.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 { DisplayExchangedAmount } from './DisplayExchangedAmount'; | ||
|
||
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( | ||
<DisplayExchangedAmount {...props} />, | ||
); | ||
|
||
describe('DisplayExchangedAmount', () => { | ||
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 { DisplayExchangedAmount } from './DisplayExchangedAmount'; |
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'; |
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
import { | ||
useOkapiKy, | ||
useNamespace, | ||
} from '@folio/stripes/core'; | ||
|
||
import { CALCULATE_EXCHANGE_API } from '../../constants'; | ||
|
||
export const useExchangeCalculation = ({ from, to, amount, rate }, options = {}) => { | ||
const { enabled = true, ...otherOptions } = options; | ||
const ky = useOkapiKy(); | ||
const [namespace] = useNamespace({ key: 'exchange-calculation' }); | ||
|
||
const searchParams = { | ||
from, | ||
to, | ||
amount, | ||
rate, | ||
}; | ||
|
||
const { data, isLoading } = useQuery( | ||
[namespace, amount, rate, from], | ||
({ signal }) => ky.get(`${CALCULATE_EXCHANGE_API}`, { searchParams, signal }).json(), | ||
{ | ||
keepPreviousData: true, | ||
...otherOptions, | ||
enabled: enabled && Boolean(amount && from && to), | ||
}, | ||
); | ||
|
||
return ({ | ||
exchangedAmount: data, | ||
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.