Skip to content

Commit b51d598

Browse files
chore(runway): cherry-pick fix (cp-7.47.0): Ensure we pass the correct type when rendering the price overview (#15747)
- fix (cp-7.47.0): Ensure we pass the correct type when rendering the price overview (#15711)
1 parent 23ea568 commit b51d598

4 files changed

Lines changed: 44 additions & 4 deletions

File tree

app/components/UI/AssetOverview/Price/Price.test.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render } from '@testing-library/react-native';
2+
import { render, userEvent } from '@testing-library/react-native';
33
import Price from './Price';
44
import { TokenI } from '../../Tokens/types';
55
import {
@@ -8,6 +8,16 @@ import {
88
} from '../../../../components/hooks/useTokenHistoricalPrices';
99
import { AssetConversion } from '@metamask/snaps-sdk';
1010
import { CaipAssetId } from '@metamask/utils';
11+
import PriceChart from '../PriceChart/PriceChart';
12+
import Button, {
13+
ButtonVariants,
14+
} from '../../../../component-library/components/Buttons/Button';
15+
16+
jest.mock('../PriceChart/PriceChart', () => ({
17+
...jest.requireActual('../PriceChart/PriceChart'),
18+
__esModule: true,
19+
default: jest.fn().mockImplementation(() => null),
20+
}));
1121

1222
const mockAsset: TokenI = {
1323
name: 'Ethereum',
@@ -102,4 +112,28 @@ describe('Price Component', () => {
102112

103113
expect(getByTestId('loading-price-diff')).toBeTruthy();
104114
});
115+
116+
it('renders price at selected date', async () => {
117+
jest
118+
.mocked(PriceChart)
119+
.mockImplementation(({ onChartIndexChange }) => (
120+
<Button
121+
testID="mock-price-chart"
122+
variant={ButtonVariants.Primary}
123+
label="TEST BUTTON"
124+
onPress={() => onChartIndexChange(1)}
125+
/>
126+
));
127+
128+
const { getByTestId } = render(<Price {...{ ...mockProps }} />);
129+
130+
// No item selected - assert label
131+
expect(getByTestId('price-label')).toHaveTextContent('Today');
132+
133+
// Act - click mock button to change chart index
134+
await userEvent.press(getByTestId('mock-price-chart'));
135+
136+
// A date has been selected, show correct mock date
137+
expect(getByTestId('price-label')).toHaveTextContent('Jan 13 at 4:40 am');
138+
});
105139
});

app/components/UI/AssetOverview/Price/Price.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import styleSheet from './Price.styles';
2020
import { TokenOverviewSelectorsIDs } from '../../../../../e2e/selectors/wallet/TokenOverview.selectors';
2121
import { TokenI } from '../../Tokens/types';
2222
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
23-
import { CaipAssetType, } from '@metamask/utils';
23+
import { CaipAssetType } from '@metamask/utils';
2424
import { AssetConversion } from '@metamask/snaps-sdk';
2525
///: END:ONLY_INCLUDE_IF
2626

@@ -90,9 +90,8 @@ const Price = ({
9090
? distributedPriceData[activeChartIndex]?.[1] || currentPrice
9191
: Number(multichainAssetRates?.rate);
9292

93-
9493
const date: string | undefined = distributedPriceData[activeChartIndex]?.[0]
95-
? toDateFormat(distributedPriceData[activeChartIndex]?.[0])
94+
? toDateFormat(Number(distributedPriceData[activeChartIndex]?.[0]))
9695
: timePeriodTextDict[timePeriod];
9796

9897
const diff: number | undefined = distributedPriceData[activeChartIndex]?.[1]
@@ -165,6 +164,7 @@ const Price = ({
165164
{diff === 0 ? '0' : ((diff / comparePrice) * 100).toFixed(2)}
166165
%){' '}
167166
<Text
167+
testID="price-label"
168168
color={TextColor.Alternative}
169169
variant={TextVariant.BodyMDMedium}
170170
>

app/components/UI/AssetOverview/__snapshots__/AssetOverview.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,7 @@ exports[`AssetOverview should render native balances when non evm network is sel
24782478
"lineHeight": 24,
24792479
}
24802480
}
2481+
testID="price-label"
24812482
>
24822483
Today
24832484
</Text>

app/util/date/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export function toLocaleDateTime(timestamp) {
88
return `${date} ${time}`;
99
}
1010

11+
/**
12+
* Formats a given timestamp (number | Date)
13+
* @param {number | Date} timestamp
14+
* @returns string
15+
*/
1116
export function toDateFormat(timestamp) {
1217
const date = new Date(timestamp);
1318
const month = strings(`date.months.${date.getMonth()}`);

0 commit comments

Comments
 (0)