|
1 | 1 | import React from 'react'; |
2 | | -import useFiatFormatter from './useFiatFormatter'; |
3 | | - |
| 2 | +import { merge } from 'lodash'; |
4 | 3 | import renderWithProvider from '../../../../util/test/renderWithProvider'; |
5 | 4 | import initialBackgroundState from '../../../../util/test/initial-background-state.json'; |
6 | | - |
7 | | -import { IndividualFiatDisplay, TotalFiatDisplay } from './FiatDisplay'; |
8 | 5 | import { FIAT_UNAVAILABLE } from '../types'; |
| 6 | +import { IndividualFiatDisplay, TotalFiatDisplay } from './FiatDisplay'; |
| 7 | +import useFiatFormatter from './useFiatFormatter'; |
| 8 | +import { NETWORKS_CHAIN_ID } from '../../../../constants/network'; |
9 | 9 |
|
10 | 10 | jest.mock('./useFiatFormatter'); |
11 | | -(useFiatFormatter as jest.Mock).mockReturnValue((value: number) => `$${value}`); |
12 | 11 |
|
13 | 12 | const mockInitialState = { |
14 | 13 | engine: { |
15 | 14 | backgroundState: initialBackgroundState, |
16 | 15 | }, |
17 | 16 | }; |
18 | 17 |
|
19 | | -describe('IndividualFiatDisplay', () => { |
20 | | - it.each([ |
21 | | - [FIAT_UNAVAILABLE, 'Not Available'], |
22 | | - [100, '$100'], |
23 | | - [-100, '$100'], |
24 | | - ])('when fiatAmount is %s it renders %s', (fiatAmount, expected) => { |
25 | | - const { getByText } = renderWithProvider( |
26 | | - <IndividualFiatDisplay fiatAmount={fiatAmount} />, |
27 | | - { state: mockInitialState }, |
28 | | - ); |
29 | | - expect(getByText(expected)).toBeDefined(); |
30 | | - }); |
| 18 | +const mockStateWithTestnet = merge({}, mockInitialState, { |
| 19 | + engine: { |
| 20 | + backgroundState: { |
| 21 | + NetworkController: { |
| 22 | + providerConfig: { |
| 23 | + chainId: NETWORKS_CHAIN_ID.SEPOLIA, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | +}); |
| 29 | + |
| 30 | +const mockStateWithShowingFiatOnTestnets = merge({}, mockStateWithTestnet, { |
| 31 | + engine: { |
| 32 | + backgroundState: { |
| 33 | + PreferencesController: { |
| 34 | + showFiatInTestnets: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | +}); |
| 39 | + |
| 40 | +const mockStateWithHidingFiatOnTestnets = merge({}, mockStateWithTestnet, { |
| 41 | + engine: { |
| 42 | + backgroundState: { |
| 43 | + PreferencesController: { |
| 44 | + showFiatInTestnets: false, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
31 | 48 | }); |
32 | 49 |
|
33 | | -describe('TotalFiatDisplay', () => { |
34 | | - it.each([ |
35 | | - [[FIAT_UNAVAILABLE, FIAT_UNAVAILABLE], 'Not Available'], |
36 | | - [[], 'Not Available'], |
37 | | - [[100, 200, FIAT_UNAVAILABLE, 300], 'Total = $600'], |
38 | | - [[-100, -200, FIAT_UNAVAILABLE, -300], 'Total = $600'], |
39 | | - ])('when fiatAmounts is %s it renders %s', (fiatAmounts, expected) => { |
40 | | - const { getByText } = renderWithProvider( |
41 | | - <TotalFiatDisplay fiatAmounts={fiatAmounts} />, |
42 | | - { state: mockInitialState }, |
43 | | - ); |
44 | | - expect(getByText(expected)).toBeDefined(); |
| 50 | +describe('FiatDisplay', () => { |
| 51 | + const mockUseFiatFormatter = jest.mocked(useFiatFormatter); |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + jest.resetAllMocks(); |
| 55 | + mockUseFiatFormatter.mockReturnValue((value: number) => `$${value}`); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('IndividualFiatDisplay', () => { |
| 59 | + it.each([ |
| 60 | + [FIAT_UNAVAILABLE, 'Not Available'], |
| 61 | + [100, '$100'], |
| 62 | + [-100, '$100'], |
| 63 | + ])('when fiatAmount is %s it renders %s', (fiatAmount, expected) => { |
| 64 | + const { queryByText } = renderWithProvider( |
| 65 | + <IndividualFiatDisplay fiatAmount={fiatAmount} />, |
| 66 | + { state: mockStateWithShowingFiatOnTestnets }, |
| 67 | + ); |
| 68 | + expect(queryByText(expected)).toBeDefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('does not render anything if hideFiatForTestnet is true', () => { |
| 72 | + const { queryByText } = renderWithProvider( |
| 73 | + <IndividualFiatDisplay fiatAmount={100} />, |
| 74 | + { state: mockStateWithHidingFiatOnTestnets }, |
| 75 | + ); |
| 76 | + expect(queryByText('100')).toBe(null); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('TotalFiatDisplay', () => { |
| 81 | + it.each([ |
| 82 | + [[FIAT_UNAVAILABLE, FIAT_UNAVAILABLE], 'Not Available'], |
| 83 | + [[], 'Not Available'], |
| 84 | + [[100, 200, FIAT_UNAVAILABLE, 300], 'Total = $600'], |
| 85 | + [[-100, -200, FIAT_UNAVAILABLE, -300], 'Total = $600'], |
| 86 | + ])('when fiatAmounts is %s it renders %s', (fiatAmounts, expected) => { |
| 87 | + const { queryByText } = renderWithProvider( |
| 88 | + <TotalFiatDisplay fiatAmounts={fiatAmounts} />, |
| 89 | + { state: mockStateWithShowingFiatOnTestnets }, |
| 90 | + ); |
| 91 | + expect(queryByText(expected)).toBeDefined(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('does not render anything if hideFiatForTestnet is true', () => { |
| 95 | + const { queryByText } = renderWithProvider( |
| 96 | + <TotalFiatDisplay fiatAmounts={[100, 200, 300]} />, |
| 97 | + { state: mockStateWithHidingFiatOnTestnets }, |
| 98 | + ); |
| 99 | + expect(queryByText('600')).toBe(null); |
| 100 | + }); |
45 | 101 | }); |
46 | 102 | }); |
0 commit comments