|
| 1 | +import { ApprovalType } from '@metamask/controller-utils'; |
| 2 | +import { |
| 3 | + TransactionMeta, |
| 4 | + TransactionStatus, |
| 5 | + TransactionType, |
| 6 | +} from '@metamask/transaction-controller'; |
| 7 | + |
| 8 | +import { getMockConfirmState } from '../../../../../../test/data/confirmations/helper'; |
| 9 | +import { renderHookWithConfirmContextProvider } from '../../../../../../test/lib/confirmations/render-helpers'; |
| 10 | +import { Severity } from '../../../../../helpers/constants/design-system'; |
| 11 | +import { RowAlertKey } from '../../../../../components/app/confirm/info/row/constants'; |
| 12 | +import { genUnapprovedContractInteractionConfirmation } from '../../../../../../test/data/confirmations/contract-interaction'; |
| 13 | +import { useFirstTimeInteractionAlert } from './useFirstTimeInteractionAlert'; |
| 14 | + |
| 15 | +const ACCOUNT_ADDRESS = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc'; |
| 16 | +const TRANSACTION_ID_MOCK = '123-456'; |
| 17 | + |
| 18 | +const CONFIRMATION_MOCK = genUnapprovedContractInteractionConfirmation({ |
| 19 | + chainId: '0x5', |
| 20 | +}) as TransactionMeta; |
| 21 | + |
| 22 | +const TRANSACTION_META_MOCK = { |
| 23 | + id: TRANSACTION_ID_MOCK, |
| 24 | + chainId: '0x5', |
| 25 | + status: TransactionStatus.submitted, |
| 26 | + type: TransactionType.contractInteraction, |
| 27 | + txParams: { |
| 28 | + from: ACCOUNT_ADDRESS, |
| 29 | + }, |
| 30 | + time: new Date().getTime() - 10000, |
| 31 | + firstTimeInteraction: true, |
| 32 | +} as TransactionMeta; |
| 33 | + |
| 34 | +function runHook({ |
| 35 | + currentConfirmation, |
| 36 | + transactions = [], |
| 37 | +}: { |
| 38 | + currentConfirmation?: TransactionMeta; |
| 39 | + transactions?: TransactionMeta[]; |
| 40 | +} = {}) { |
| 41 | + let pendingApprovals = {}; |
| 42 | + if (currentConfirmation) { |
| 43 | + pendingApprovals = { |
| 44 | + [currentConfirmation.id as string]: { |
| 45 | + id: currentConfirmation.id, |
| 46 | + type: ApprovalType.Transaction, |
| 47 | + }, |
| 48 | + }; |
| 49 | + transactions.push(currentConfirmation); |
| 50 | + } |
| 51 | + const state = getMockConfirmState({ |
| 52 | + metamask: { |
| 53 | + pendingApprovals, |
| 54 | + transactions, |
| 55 | + }, |
| 56 | + }); |
| 57 | + const response = renderHookWithConfirmContextProvider( |
| 58 | + useFirstTimeInteractionAlert, |
| 59 | + state, |
| 60 | + ); |
| 61 | + |
| 62 | + return response.result.current; |
| 63 | +} |
| 64 | + |
| 65 | +describe('useFirstTimeInteractionAlert', () => { |
| 66 | + beforeEach(() => { |
| 67 | + jest.resetAllMocks(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns no alerts if no confirmation', () => { |
| 71 | + expect(runHook()).toEqual([]); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns no alerts if no transactions', () => { |
| 75 | + expect( |
| 76 | + runHook({ |
| 77 | + currentConfirmation: CONFIRMATION_MOCK, |
| 78 | + transactions: [], |
| 79 | + }), |
| 80 | + ).toEqual([]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns no alerts if firstTimeInteraction is false', () => { |
| 84 | + const notFirstTimeConfirmation = { |
| 85 | + ...TRANSACTION_META_MOCK, |
| 86 | + firstTimeInteraction: false, |
| 87 | + }; |
| 88 | + expect( |
| 89 | + runHook({ |
| 90 | + currentConfirmation: notFirstTimeConfirmation, |
| 91 | + }), |
| 92 | + ).toEqual([]); |
| 93 | + }); |
| 94 | + |
| 95 | + it('returns no alerts if firstTimeInteraction is undefined', () => { |
| 96 | + const notFirstTimeConfirmation = { |
| 97 | + ...TRANSACTION_META_MOCK, |
| 98 | + firstTimeInteraction: undefined, |
| 99 | + }; |
| 100 | + expect( |
| 101 | + runHook({ |
| 102 | + currentConfirmation: notFirstTimeConfirmation, |
| 103 | + }), |
| 104 | + ).toEqual([]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('returns alert if isFirstTimeInteraction is true', () => { |
| 108 | + const firstTimeConfirmation = { |
| 109 | + ...CONFIRMATION_MOCK, |
| 110 | + isFirstTimeInteraction: true, |
| 111 | + }; |
| 112 | + const alerts = runHook({ |
| 113 | + currentConfirmation: firstTimeConfirmation, |
| 114 | + }); |
| 115 | + |
| 116 | + expect(alerts).toEqual([ |
| 117 | + { |
| 118 | + actions: [], |
| 119 | + field: RowAlertKey.FirstTimeInteraction, |
| 120 | + isBlocking: false, |
| 121 | + key: 'firstTimeInteractionTitle', |
| 122 | + message: |
| 123 | + "You're interacting with this address for the first time. Make sure that it's correct before you continue.", |
| 124 | + reason: '1st interaction', |
| 125 | + severity: Severity.Warning, |
| 126 | + }, |
| 127 | + ]); |
| 128 | + }); |
| 129 | +}); |
0 commit comments