Skip to content

Commit efeec3d

Browse files
authored
Merge pull request #2905 from input-output-hk/fix/ddw-911-remove-warning-when-recommended-decimal-is-zero
2 parents f41548a + 8ae334e commit efeec3d

File tree

15 files changed

+243
-45
lines changed

15 files changed

+243
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
### Fixes
1717

18+
- Fix warning sign displayed when recommend decimals is zero ([PR 2905](https://github.com/input-output-hk/daedalus/pull/2905))
1819
- Fixed discrete tooltip being clipped by loading overlay when stake pools are adjusted ([PR 2902](https://github.com/input-output-hk/daedalus/pull/2902))
1920

2021
## 4.9.0

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module.exports = {
8989
// Jest does not support WASM imports from ESM modules
9090
// https://github.com/facebook/jest/issues/9430
9191
'^@iohk-jormungandr/wallet-js$': 'identity-obj-proxy',
92+
'tests/(.*)': '<rootDir>/tests/$1',
9293
},
9394

9495
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader

source/renderer/app/components/assets/Asset.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ class Asset extends Component<Props, State> {
209209
})}
210210
className={styles.warningIconWrapper}
211211
>
212-
<SVGInline className={styles.warningIcon} svg={warningIcon} />
212+
<span data-testid="warning-icon">
213+
<SVGInline className={styles.warningIcon} svg={warningIcon} />
214+
</span>
213215
</PopOver>
214216
</div>
215217
)}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import '@testing-library/jest-dom';
2+
3+
import React from 'react';
4+
import noop from 'lodash/noop';
5+
import { cleanup, screen, waitFor } from '@testing-library/react';
6+
7+
import createTestBed from 'tests/_utils/TestBed';
8+
import {
9+
withDecimalPlacesToken,
10+
zeroDecimalPlacesToken,
11+
} from 'tests/mocks/asset';
12+
13+
import AssetSettingsDialog from './AssetSettingsDialog';
14+
15+
describe('AssetSettingsDialog', () => {
16+
afterEach(() => cleanup());
17+
18+
it('should not show a warning when an asset is set to zero recommended decimal places', async () => {
19+
createTestBed(
20+
<AssetSettingsDialog
21+
asset={zeroDecimalPlacesToken}
22+
onSubmit={noop}
23+
onCancel={noop}
24+
/>
25+
);
26+
await waitFor(() => screen.getByText('Number of decimal places'));
27+
expect(screen.queryByTestId('warning-icon')).not.toBeInTheDocument();
28+
});
29+
30+
it('should show a warning when an asset is not set to the recommended decimal places', async () => {
31+
createTestBed(
32+
<AssetSettingsDialog
33+
asset={withDecimalPlacesToken}
34+
onSubmit={noop}
35+
onCancel={noop}
36+
/>
37+
);
38+
await waitFor(() => screen.getByText('Number of decimal places'));
39+
expect(screen.queryByTestId('warning-icon')).toBeInTheDocument();
40+
});
41+
});

source/renderer/app/components/assets/AssetSettingsDialog.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
MAX_DECIMAL_PRECISION,
1818
} from '../../config/assetsConfig';
1919
import { DiscreetTokenWalletAmount } from '../../features/discreet-mode';
20+
import { isRecommendedDecimal } from '../wallet/tokens/wallet-token/helpers';
2021

2122
const messages = defineMessages({
2223
title: {
@@ -135,7 +136,6 @@ class AssetSettingsDialog extends Component<Props, State> {
135136
const { decimals: savedDecimals, recommendedDecimals } = asset;
136137
const { decimals } = this.state;
137138
const hasSavedDecimals = typeof savedDecimals === 'number';
138-
const hasRecommendedDecimals = typeof recommendedDecimals === 'number';
139139
const options = range(MAX_DECIMAL_PRECISION + 1).map((value) => ({
140140
value,
141141
}));
@@ -153,8 +153,12 @@ class AssetSettingsDialog extends Component<Props, State> {
153153
onClick: () => onSubmit(asset, decimals),
154154
},
155155
];
156-
const hasWarning =
157-
hasRecommendedDecimals && savedDecimals !== recommendedDecimals;
156+
157+
const hasWarning = isRecommendedDecimal({
158+
recommendedDecimals,
159+
decimals: savedDecimals,
160+
});
161+
158162
let warningPopOverMessage;
159163

160164
if (hasWarning) {
@@ -209,10 +213,12 @@ class AssetSettingsDialog extends Component<Props, State> {
209213
recommendedDecimals,
210214
})}
211215
>
212-
<SVGInline
213-
className={styles.warningIcon}
214-
svg={warningIcon}
215-
/>
216+
<span data-testid="warning-icon">
217+
<SVGInline
218+
className={styles.warningIcon}
219+
svg={warningIcon}
220+
/>
221+
</span>
216222
</PopOver>
217223
)}
218224
</span>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import '@testing-library/jest-dom';
2+
3+
import React from 'react';
4+
import noop from 'lodash/noop';
5+
import { cleanup, screen, waitFor } from '@testing-library/react';
6+
7+
import createTestBed from 'tests/_utils/TestBed';
8+
import {
9+
withDecimalPlacesToken,
10+
zeroDecimalPlacesToken,
11+
} from 'tests/mocks/asset';
12+
13+
import WalletToken from './WalletToken';
14+
15+
const defaultWalletProps = {
16+
isFavorite: false,
17+
isInsertingAsset: false,
18+
isLoading: false,
19+
isRemovingAsset: false,
20+
anyAssetWasHovered: false,
21+
assetSettingsDialogWasOpened: false,
22+
onAssetSettings: noop,
23+
onCopyAssetParam: noop,
24+
onOpenAssetSend: noop,
25+
onToggleFavorite: noop,
26+
};
27+
28+
describe('WalletToken', () => {
29+
afterEach(() => cleanup());
30+
31+
it('should not show a warning when an asset is set to zero recommended decimal places', async () => {
32+
createTestBed(
33+
<WalletToken asset={zeroDecimalPlacesToken} {...defaultWalletProps} />
34+
);
35+
await waitFor(() => screen.getByText(zeroDecimalPlacesToken.policyId));
36+
expect(screen.queryByTestId('warning-icon')).not.toBeInTheDocument();
37+
});
38+
39+
it('should show a warning when an asset is not set to the recommended decimal places', async () => {
40+
createTestBed(
41+
<WalletToken asset={withDecimalPlacesToken} {...defaultWalletProps} />
42+
);
43+
await waitFor(() => screen.getByText(withDecimalPlacesToken.policyId));
44+
expect(screen.queryAllByTestId('warning-icon').length).toEqual(2);
45+
});
46+
});

source/renderer/app/components/wallet/tokens/wallet-token/WalletToken.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import AssetContent from '../../../assets/AssetContent';
66
import type { AssetToken } from '../../../../api/assets/types';
77
import WalletTokenFooter from './WalletTokenFooter';
88
import WalletTokenHeader from './WalletTokenHeader';
9+
import { isRecommendedDecimal } from './helpers';
910

1011
type Props = {
1112
anyAssetWasHovered: boolean;
@@ -43,9 +44,16 @@ const WalletToken = observer((props: Props) => {
4344
isRemovingAsset,
4445
} = props;
4546
const [isExpanded, setIsExpanded] = useState<boolean>(false);
47+
4648
const toggleIsExpanded = useCallback(() => {
4749
setIsExpanded(!isExpanded);
4850
}, [setIsExpanded, isExpanded]);
51+
52+
const hasWarning = isRecommendedDecimal({
53+
decimals: asset.decimals,
54+
recommendedDecimals: asset.recommendedDecimals,
55+
});
56+
4957
const componentStyles = useMemo(
5058
() =>
5159
classNames(
@@ -71,6 +79,7 @@ const WalletToken = observer((props: Props) => {
7179
onCopyAssetParam={onCopyAssetParam}
7280
onToggleFavorite={onToggleFavorite}
7381
assetSettingsDialogWasOpened={assetSettingsDialogWasOpened}
82+
hasWarning={hasWarning}
7483
/>
7584
<div className={styles.content}>
7685
<AssetContent
@@ -84,6 +93,7 @@ const WalletToken = observer((props: Props) => {
8493
isLoading={isLoading}
8594
onAssetSettings={onAssetSettings}
8695
onOpenAssetSend={onOpenAssetSend}
96+
hasWarning={hasWarning}
8797
/>
8898
</div>
8999
</div>

source/renderer/app/components/wallet/tokens/wallet-token/WalletTokenFooter.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import type { AssetToken } from '../../../../api/assets/types';
1111
// @ts-ignore ts-migrate(2307) FIXME: Cannot find module '../../../../assets/images/asse... Remove this comment to see the full error message
1212
import warningIcon from '../../../../assets/images/asset-token-warning-ic.inline.svg';
1313
import { messages } from './WalletToken.messages';
14-
import { isRecommendedDecimal } from './helpers';
1514

1615
type Props = {
1716
asset: AssetToken;
1817
className?: string;
1918
intl: intlShape.isRequired;
2019
isLoading: boolean;
20+
hasWarning: boolean;
2121
onAssetSettings?: (...args: Array<any>) => any;
2222
onOpenAssetSend?: (...args: Array<any>) => any;
2323
};
@@ -28,14 +28,12 @@ const WalletTokenFooter = (props: Props) => {
2828
className,
2929
intl,
3030
isLoading,
31+
hasWarning,
3132
onAssetSettings,
3233
onOpenAssetSend,
3334
} = props;
3435
const { recommendedDecimals, decimals } = asset;
35-
const hasWarning = isRecommendedDecimal({
36-
decimals,
37-
recommendedDecimals,
38-
});
36+
3937
const warningPopOverMessage =
4038
typeof decimals === 'number'
4139
? messages.settingsWarningPopOverNotUsing
@@ -74,7 +72,11 @@ const WalletTokenFooter = (props: Props) => {
7472
label={
7573
<>
7674
{intl.formatMessage(messages.settingsButtonLabel)}
77-
{hasWarning && <SVGInline svg={warningIcon} />}
75+
{hasWarning && (
76+
<span data-testid="warning-icon">
77+
<SVGInline svg={warningIcon} />
78+
</span>
79+
)}
7880
</>
7981
}
8082
onClick={() =>

source/renderer/app/components/wallet/tokens/wallet-token/WalletTokenHeader.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import arrow from '../../../../assets/images/collapse-arrow-small.inline.svg';
1313
import starNotFilledIcon from '../../../../assets/images/star-not-filled.inline.svg';
1414
// @ts-ignore ts-migrate(2307) FIXME: Cannot find module '../../../../assets/images/star... Remove this comment to see the full error message
1515
import starFilledIcon from '../../../../assets/images/star-filled.inline.svg';
16-
import { isRecommendedDecimal } from './helpers';
1716

1817
type Props = {
1918
anyAssetWasHovered: boolean;
@@ -24,6 +23,7 @@ type Props = {
2423
isExpanded: boolean;
2524
isFavorite: boolean;
2625
isLoading: boolean;
26+
hasWarning: boolean;
2727
onClick: (...args: Array<any>) => any;
2828
onCopyAssetParam: (...args: Array<any>) => any;
2929
onToggleFavorite?: (...args: Array<any>) => any;
@@ -39,16 +39,14 @@ const WalletTokenHeader = (props: Props) => {
3939
isExpanded,
4040
isFavorite,
4141
isLoading,
42+
hasWarning,
4243
onClick,
4344
onCopyAssetParam,
4445
onToggleFavorite,
4546
} = props;
46-
const { decimals, uniqueId, recommendedDecimals } = asset;
47+
const { uniqueId } = asset;
4748
const starIcon = isFavorite ? starFilledIcon : starNotFilledIcon;
48-
const hasWarning = isRecommendedDecimal({
49-
decimals,
50-
recommendedDecimals,
51-
});
49+
5250
const rootStyles = classNames(
5351
styles.root,
5452
isExpanded && styles.isExpanded,
@@ -58,6 +56,7 @@ const WalletTokenHeader = (props: Props) => {
5856
styles.favoriteIcon,
5957
isFavorite && styles.isFavorite
6058
);
59+
6160
return (
6261
<div className={rootStyles} onClick={onClick}>
6362
{onToggleFavorite && (

source/renderer/app/components/wallet/tokens/wallet-token/helpers.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ type IsRecommendedDecimal = {
22
decimals: number | null | undefined;
33
recommendedDecimals: number | null | undefined;
44
};
5+
56
export const isRecommendedDecimal = ({
67
recommendedDecimals,
78
decimals,
8-
}: IsRecommendedDecimal) =>
9-
typeof recommendedDecimals === 'number' && decimals !== recommendedDecimals;
9+
}: IsRecommendedDecimal) => {
10+
return (
11+
typeof recommendedDecimals === 'number' &&
12+
typeof decimals === 'number' &&
13+
decimals !== recommendedDecimals
14+
);
15+
};

0 commit comments

Comments
 (0)