|
| 1 | +import { useExchange } from '../useExchange'; |
| 2 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 3 | +import { exchangeRatesData } from 'shared/utils/mockData'; |
| 4 | +import { POCKET } from 'constants/currency'; |
| 5 | + |
| 6 | +describe('Exchange Hook', () => { |
| 7 | + it('get initIal source and destination pockets', () => { |
| 8 | + const { result } = renderHook(useExchange, { |
| 9 | + initialProps: { rates: exchangeRatesData.data.rates }, |
| 10 | + }); |
| 11 | + let { source, destination } = result.current; |
| 12 | + expect(source).toEqual({ currency: 'EUR', amount: 0 }); |
| 13 | + expect(destination).toEqual({ currency: 'GBP', amount: 0 }); |
| 14 | + }); |
| 15 | + |
| 16 | + it('allows to set values of source and destination pockets', () => { |
| 17 | + const { result } = renderHook(useExchange, { |
| 18 | + initialProps: { rates: exchangeRatesData.data.rates }, |
| 19 | + }); |
| 20 | + let { setDestination, setSource } = result.current; |
| 21 | + act(() => setDestination({ currency: 'GBP', amount: 10 })); |
| 22 | + act(() => setSource({ currency: 'USD', amount: 100 })); |
| 23 | + expect(result.current.destination).toEqual({ currency: 'GBP', amount: 10 }); |
| 24 | + expect(result.current.source).toEqual({ currency: 'USD', amount: 100 }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('interchange the source and destination transaction', () => { |
| 28 | + const { result } = renderHook(useExchange, { |
| 29 | + initialProps: { rates: exchangeRatesData.data.rates }, |
| 30 | + }); |
| 31 | + let { setDestination, setSource } = result.current; |
| 32 | + act(() => setDestination({ currency: 'GBP', amount: 10 })); |
| 33 | + act(() => setSource({ currency: 'USD', amount: 100 })); |
| 34 | + act(() => result.current.onSwitch()); |
| 35 | + expect(result.current.destination).toEqual({ |
| 36 | + currency: 'USD', |
| 37 | + amount: 100, |
| 38 | + }); |
| 39 | + expect(result.current.source).toEqual({ currency: 'GBP', amount: 10 }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('onAmount change in source and destination pocket', () => { |
| 43 | + const { result } = renderHook(useExchange, { |
| 44 | + initialProps: { rates: exchangeRatesData.data.rates }, |
| 45 | + }); |
| 46 | + const { onAmountChange } = result.current; |
| 47 | + act(() => onAmountChange(POCKET.SOURCE, 150)); |
| 48 | + expect(result.current.destination).toEqual({ |
| 49 | + currency: 'GBP', |
| 50 | + amount: -150, |
| 51 | + }); |
| 52 | + expect(result.current.source).toEqual({ currency: 'EUR', amount: 150 }); |
| 53 | + act(() => onAmountChange(POCKET.DESTINATION, 300)); |
| 54 | + expect(result.current.destination).toEqual({ |
| 55 | + currency: 'GBP', |
| 56 | + amount: 300, |
| 57 | + }); |
| 58 | + expect(result.current.source).toEqual({ currency: 'EUR', amount: -300 }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('onCurrencyChange in source pocket', () => { |
| 62 | + const { result } = renderHook(useExchange, { |
| 63 | + initialProps: { rates: exchangeRatesData.data.rates }, |
| 64 | + }); |
| 65 | + const { onCurrencyChange, setSource } = result.current; |
| 66 | + act(() => setSource({ currency: 'USD', amount: 100 })); |
| 67 | + act(() => onCurrencyChange(POCKET.SOURCE, 'USD')); |
| 68 | + expect(result.current.source).toEqual({ currency: 'USD', amount: 100 }); |
| 69 | + act(() => onCurrencyChange(POCKET.DESTINATION, 'EUR')); |
| 70 | + expect(result.current.destination).toEqual({ currency: 'EUR', amount: 0 }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments