-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathPrice.legacy.test.tsx
More file actions
264 lines (225 loc) · 8.39 KB
/
Copy pathPrice.legacy.test.tsx
File metadata and controls
264 lines (225 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import React from 'react';
import { View } from 'react-native';
import { userEvent, fireEvent } from '@testing-library/react-native';
import { useSelector } from 'react-redux';
import renderWithProvider from '../../../../util/test/renderWithProvider';
import PriceLegacy, { type PriceLegacyProps } from './Price.legacy';
import PriceChart from '../PriceChart/PriceChart';
import { distributeDataPoints } from '../PriceChart/utils';
import { Button, ButtonVariant } from '@metamask/design-system-react-native';
import { TokenOverviewSelectorsIDs } from '../TokenOverview.testIds';
import type { TokenPrice } from '../../../../components/hooks/useTokenHistoricalPrices';
import { selectTokenDetailsTechnicalIndicatorsEnabled } from '../../../../selectors/featureFlagController/tokenDetailsTechnicalIndicators';
import {
CHART_DATA_THRESHOLD,
TIME_PERIOD_MS,
} from './tokenOverviewChart.constants';
const mockSelectTechnicalIndicatorsEnabled = jest.fn(() => false);
jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: jest.fn(),
}));
jest.mock(
'../../../../selectors/featureFlagController/tokenDetailsTechnicalIndicators',
() => ({
selectTokenDetailsTechnicalIndicatorsEnabled: jest.fn(),
}),
);
jest.mock('../ChartNavigationButton', () => {
const { Pressable } = jest.requireActual('react-native');
return {
__esModule: true,
default: ({ onPress }: { onPress: () => void }) => (
<Pressable testID="chart-navigation-button" onPress={onPress} />
),
};
});
const mockUseSelector = useSelector as jest.MockedFunction<typeof useSelector>;
/** Matches CHART_DATA_THRESHOLD (tokenOverviewChart.constants) — enough points for a non-empty line chart. */
const mockPricesAtLeast5: TokenPrice[] = Array.from({ length: 5 }, (_, i) => [
String(1736761237983 + i),
100 + i,
]);
jest.mock('../PriceChart/PriceChart', () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => null),
}));
const baseProps: PriceLegacyProps = {
prices: mockPricesAtLeast5,
priceDiff: 5,
currentPrice: 105,
currentCurrency: 'USD',
comparePrice: 100,
isLoading: false,
timePeriod: '1d',
};
describe('PriceLegacy', () => {
beforeEach(() => {
jest.clearAllMocks();
mockSelectTechnicalIndicatorsEnabled.mockReturnValue(false);
mockUseSelector.mockImplementation((selector) => {
if (selector === selectTokenDetailsTechnicalIndicatorsEnabled) {
return mockSelectTechnicalIndicatorsEnabled();
}
return undefined;
});
jest.mocked(PriceChart).mockImplementation(() => <></>);
});
it('renders the token price when not loading', () => {
const { getByTestId } = renderWithProvider(<PriceLegacy {...baseProps} />);
expect(
getByTestId(TokenOverviewSelectorsIDs.TOKEN_PRICE),
).toBeOnTheScreen();
});
it('does not render token name in price header', () => {
const { queryByText } = renderWithProvider(<PriceLegacy {...baseProps} />);
expect(queryByText('Ethereum')).toBeNull();
});
it('shows loading skeletons when isLoading is true', () => {
const { getByTestId, queryByTestId } = renderWithProvider(
<PriceLegacy {...baseProps} isLoading />,
);
expect(getByTestId('loading-price-diff')).toBeOnTheScreen();
expect(queryByTestId('price-label')).not.toBeOnTheScreen();
});
it('renders price diff with positive color and sign', () => {
const { getByTestId } = renderWithProvider(<PriceLegacy {...baseProps} />);
const label = getByTestId('price-label');
expect(label).toBeOnTheScreen();
});
it('shows price diff text even when prices array is empty', () => {
const { getByTestId } = renderWithProvider(
<PriceLegacy {...baseProps} prices={[]} />,
);
expect(getByTestId('price-label')).toBeOnTheScreen();
});
it('does not render token price when currentPrice is NaN', () => {
const { queryByTestId } = renderWithProvider(
<PriceLegacy {...baseProps} currentPrice={NaN} />,
);
expect(
queryByTestId(TokenOverviewSelectorsIDs.TOKEN_PRICE),
).not.toBeOnTheScreen();
});
it('renders price at selected chart index via onChartIndexChange', async () => {
jest.mocked(PriceChart).mockImplementation(({ onChartIndexChange }) => (
<Button
testID="mock-price-chart"
variant={ButtonVariant.Primary}
onPress={() => onChartIndexChange(1)}
>
Select Point
</Button>
));
const { getByTestId } = renderWithProvider(<PriceLegacy {...baseProps} />);
expect(getByTestId('price-label')).toHaveTextContent('Today');
await userEvent.press(getByTestId('mock-price-chart'));
expect(getByTestId('price-label')).toHaveTextContent('Jan 13 at 4:40 am');
});
it('passes PriceChart the correct props', () => {
renderWithProvider(<PriceLegacy {...baseProps} />);
expect(PriceChart).toHaveBeenCalledWith(
expect.objectContaining({
priceDiff: 5,
isLoading: false,
}),
undefined,
);
});
it('handles zero priceDiff without sign prefix', () => {
const { getByTestId } = renderWithProvider(
<PriceLegacy {...baseProps} priceDiff={0} comparePrice={100} />,
);
expect(getByTestId('price-label')).toBeOnTheScreen();
});
it('handles negative priceDiff', () => {
const { getByTestId } = renderWithProvider(
<PriceLegacy
{...baseProps}
priceDiff={-10}
currentPrice={90}
comparePrice={100}
/>,
);
expect(getByTestId('price-label')).toBeOnTheScreen();
});
it('passes sparse prices to PriceChart so empty state uses the same threshold', () => {
jest
.mocked(PriceChart)
.mockImplementationOnce(({ prices }) =>
prices.length < CHART_DATA_THRESHOLD ? (
<View testID="price-chart-insufficient-data" />
) : (
<></>
),
);
const fourPrices: TokenPrice[] = Array.from({ length: 4 }, (_, i) => [
String(1736761237983 + i),
100 + i,
]);
const { getByTestId } = renderWithProvider(
<PriceLegacy {...baseProps} prices={fourPrices} />,
);
expect(PriceChart).toHaveBeenCalledWith(
expect.objectContaining({
prices: distributeDataPoints(fourPrices),
}),
undefined,
);
expect(getByTestId('price-chart-insufficient-data')).toBeOnTheScreen();
});
describe('timePeriodMs pass-through', () => {
it('passes the correct timePeriodMs for a known time period', () => {
renderWithProvider(<PriceLegacy {...baseProps} timePeriod="1d" />);
expect(PriceChart).toHaveBeenCalledWith(
expect.objectContaining({
timePeriodMs: TIME_PERIOD_MS['1d'],
}),
undefined,
);
});
it('passes undefined timePeriodMs for the "all" time period', () => {
renderWithProvider(<PriceLegacy {...baseProps} timePeriod="all" />);
expect(PriceChart).toHaveBeenCalledWith(
expect.objectContaining({
timePeriodMs: undefined,
}),
undefined,
);
});
});
describe('chart navigation buttons', () => {
it('calls onTimePeriodChange when a button is pressed with flag OFF (below chart)', () => {
mockSelectTechnicalIndicatorsEnabled.mockReturnValue(false);
const onTimePeriodChange = jest.fn();
const { getAllByTestId } = renderWithProvider(
<PriceLegacy
{...baseProps}
chartNavigationButtons={['1d', '7d']}
onTimePeriodChange={onTimePeriodChange}
/>,
);
fireEvent.press(getAllByTestId('chart-navigation-button')[0]);
expect(onTimePeriodChange).toHaveBeenCalledWith('1d');
});
it('calls onTimePeriodChange when a button is pressed with flag ON (above chart)', () => {
mockSelectTechnicalIndicatorsEnabled.mockReturnValue(true);
const onTimePeriodChange = jest.fn();
const { getAllByTestId } = renderWithProvider(
<PriceLegacy
{...baseProps}
chartNavigationButtons={['1d', '7d']}
onTimePeriodChange={onTimePeriodChange}
/>,
);
fireEvent.press(getAllByTestId('chart-navigation-button')[1]);
expect(onTimePeriodChange).toHaveBeenCalledWith('7d');
});
it('does not render navigation buttons when onTimePeriodChange is omitted', () => {
const { queryAllByTestId } = renderWithProvider(
<PriceLegacy {...baseProps} chartNavigationButtons={['1d', '7d']} />,
);
expect(queryAllByTestId('chart-navigation-button')).toHaveLength(0);
});
});
});