forked from MetaMask/metamask-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Redesign Signature Decoding Simulation (MetaMask#13026)
## **Description** Create Additional Tests for new Decoding Simulation feat ## **Related issues** Fixes: MetaMask#13023 Relates to: MetaMask#12994 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
Showing
11 changed files
with
300 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
.../Views/confirmations/components/Confirm/Info/TypedSignV3V4/Simulation/Simulation.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import React from 'react'; | ||
import cloneDeep from 'lodash/cloneDeep'; | ||
import { | ||
DecodingData, | ||
DecodingDataChangeType, | ||
DecodingDataStateChanges, | ||
SignatureRequest, | ||
} from '@metamask/signature-controller'; | ||
import useGetTokenStandardAndDetails from '../../../../../hooks/useGetTokenStandardAndDetails'; | ||
import { typedSignV4ConfirmationState } from '../../../../../../../../util/test/confirm-data-helpers'; | ||
import renderWithProvider from '../../../../../../../../util/test/renderWithProvider'; | ||
import { memoizedGetTokenStandardAndDetails } from '../../../../../utils/token'; | ||
import TypedSignV3V4Simulation from './Simulation'; | ||
|
||
jest.mock('../../../../../hooks/useGetTokenStandardAndDetails'); | ||
|
||
jest.mock('../../../../../../../../core/Engine', () => ({ | ||
context: { | ||
NetworkController: { | ||
findNetworkClientIdByChainId: () => 'mainnet', | ||
}, | ||
}, | ||
})); | ||
|
||
const stateChangesApprove = [ | ||
{ | ||
assetType: 'ERC20', | ||
changeType: DecodingDataChangeType.Approve, | ||
address: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', | ||
amount: '12345', | ||
contractAddress: '0x6b175474e89094c44da98b954eedeac495271d0f', | ||
}, | ||
]; | ||
|
||
const mockState = ( | ||
mockStateChanges: DecodingDataStateChanges, | ||
{ | ||
mockDecodingDataProps, | ||
stubDecodingLoading = false, | ||
}: { | ||
mockDecodingDataProps?: Partial<DecodingData>; | ||
stubDecodingLoading?: boolean; | ||
} = { | ||
mockDecodingDataProps: {}, | ||
stubDecodingLoading: false, | ||
}, | ||
) => { | ||
const clonedMockState = cloneDeep(typedSignV4ConfirmationState); | ||
const request = clonedMockState.engine.backgroundState.SignatureController | ||
.signatureRequests[ | ||
'fb2029e1-b0ab-11ef-9227-05a11087c334' | ||
] as SignatureRequest; | ||
|
||
request.decodingLoading = stubDecodingLoading; | ||
request.decodingData = { | ||
...mockDecodingDataProps, | ||
stateChanges: mockStateChanges, | ||
}; | ||
|
||
return clonedMockState; | ||
}; | ||
|
||
describe('PermitSimulation', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
/** Reset memoized function using getTokenStandardAndDetails for each test */ | ||
memoizedGetTokenStandardAndDetails?.cache?.clear?.(); | ||
}); | ||
|
||
it('renders DecodedSimulation loader if decodingLoading is true', async () => { | ||
const { queryByTestId } = renderWithProvider(<TypedSignV3V4Simulation />, { | ||
state: mockState(stateChangesApprove, { | ||
stubDecodingLoading: true, | ||
}), | ||
}); | ||
|
||
expect(await queryByTestId('confirm-v3v4-simulation-loader')).toBeDefined(); | ||
}); | ||
|
||
it('renders DecodingSimulation with "Unavailable" if decoding data is empty', async () => { | ||
const { getByText } = renderWithProvider(<TypedSignV3V4Simulation />, { | ||
state: mockState([]), | ||
}); | ||
|
||
expect(await getByText('Estimated changes')).toBeDefined(); | ||
expect(await getByText('Unavailable')).toBeDefined(); | ||
}); | ||
|
||
it('renders DecodingSimulation for permits', async () => { | ||
( | ||
useGetTokenStandardAndDetails as jest.MockedFn< | ||
typeof useGetTokenStandardAndDetails | ||
> | ||
).mockReturnValue({ | ||
symbol: 'TST', | ||
decimals: '4', | ||
balance: undefined, | ||
standard: 'ERC20', | ||
decimalsNumber: 4, | ||
}); | ||
|
||
const { getByText } = renderWithProvider(<TypedSignV3V4Simulation />, { | ||
state: mockState(stateChangesApprove), | ||
}); | ||
|
||
expect(await getByText('Estimated changes')).toBeDefined(); | ||
expect(await getByText('Spending cap')).toBeDefined(); | ||
expect(await getByText('1.235')).toBeDefined(); | ||
}); | ||
|
||
it('renders PermitSimulation if decoding api returns error', async () => { | ||
( | ||
useGetTokenStandardAndDetails as jest.MockedFn< | ||
typeof useGetTokenStandardAndDetails | ||
> | ||
).mockReturnValue({ | ||
symbol: 'TST', | ||
decimals: '2', | ||
balance: undefined, | ||
standard: 'ERC20', | ||
decimalsNumber: 4, | ||
}); | ||
|
||
const { getByText } = renderWithProvider(<TypedSignV3V4Simulation />, { | ||
state: mockState([], { | ||
mockDecodingDataProps: { | ||
error: { message: 'some error', type: 'SOME_ERROR' }, | ||
} as Partial<DecodingData>, | ||
}), | ||
}); | ||
|
||
expect(await getByText('Estimated changes')).toBeDefined(); | ||
expect(await getByText('Spending cap')).toBeDefined(); | ||
expect(await getByText('0.3')).toBeDefined(); | ||
expect( | ||
await getByText( | ||
"You're giving the spender permission to spend this many tokens from your account.", | ||
), | ||
).toBeDefined(); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
...ews/confirmations/components/Confirm/Info/TypedSignV3V4/Simulation/Static/Static.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import { Text } from 'react-native'; | ||
import { render } from '@testing-library/react-native'; | ||
import StaticSimulation from './Static'; | ||
|
||
const mockProps = { | ||
title: 'Test Title', | ||
titleTooltip: 'Test Tooltip', | ||
description: 'Test Description', | ||
simulationElements: <></>, | ||
}; | ||
|
||
describe('StaticSimulation', () => { | ||
it('renders correctly with basic props', () => { | ||
const { getByText } = render(<StaticSimulation {...mockProps} />); | ||
|
||
expect(getByText('Test Title')).toBeDefined(); | ||
expect(getByText('Test Description')).toBeDefined(); | ||
}); | ||
|
||
it('shows loader when isLoading is true', () => { | ||
const { queryByTestId } = render( | ||
<StaticSimulation {...mockProps} isLoading />, | ||
); | ||
|
||
expect(queryByTestId('confirm-v3v4-simulation-loader')).toBeDefined(); | ||
}); | ||
|
||
it('shows simulation elements when not loading', () => { | ||
const simulationElements = <Text>Test Simulation</Text>; | ||
const { getByText } = render( | ||
<StaticSimulation | ||
{...mockProps} | ||
simulationElements={simulationElements} | ||
isLoading={false} | ||
/>, | ||
); | ||
|
||
expect(getByText('Test Simulation')).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...m/Info/TypedSignV3V4/Simulation/components/NativeValueDisplay/NativeValueDisplay.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import NativeValueDisplay from './NativeValueDisplay'; | ||
import { backgroundState } from '../../../../../../../../../../util/test/initial-root-state'; | ||
import renderWithProvider from '../../../../../../../../../../util/test/renderWithProvider'; | ||
import { fireEvent } from '@testing-library/react-native'; | ||
|
||
const mockInitialState = { | ||
engine: { | ||
backgroundState, | ||
}, | ||
}; | ||
|
||
describe('NativeValueDisplay', () => { | ||
it('renders component correctly', async () => { | ||
const { findByText } = renderWithProvider( | ||
<NativeValueDisplay | ||
labelChangeType={'Spending Cap'} | ||
value={'4321'} | ||
chainId={'0x1'} | ||
/>, | ||
{ state: mockInitialState }, | ||
); | ||
|
||
expect(await findByText('< 0.000001')).toBeDefined(); | ||
expect(await findByText('ETH')).toBeDefined(); | ||
}); | ||
|
||
it('displays modal when clicking on the value', async () => { | ||
const { findByText } = renderWithProvider( | ||
<NativeValueDisplay | ||
labelChangeType={'Spending Cap'} | ||
value={'4321'} | ||
chainId={'0x1'} | ||
/>, | ||
{ state: mockInitialState }, | ||
); | ||
|
||
const button = await findByText('< 0.000001'); | ||
fireEvent.press(button); | ||
|
||
expect(await findByText('Spending Cap')).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.