Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/__tests__/cashMethods.test.js
import { currency } from '@components/Common/Ticker'; | |||||
import { | import { | ||||
fromSmallestDenomination, | fromSmallestDenomination, | ||||
formatBalance, | formatBalance, | ||||
batchArray, | batchArray, | ||||
flattenBatchedHydratedUtxos, | flattenBatchedHydratedUtxos, | ||||
loadStoredWallet, | loadStoredWallet, | ||||
isValidStoredWallet, | isValidStoredWallet, | ||||
whichUtxosChanged, | |||||
parseChangedUtxos, | |||||
isUtxoArrayEmpty, | |||||
mergeHydratedUtxoDetails, | |||||
organizeHydratedUtxoDetailsByAddress, | |||||
addHydratedUtxoDetailsToWalletStateIfNew, | |||||
whichUtxosWereConsumed, | |||||
removeConsumedUtxosFromHydratedUtxoDetails, | |||||
} from '@utils/cashMethods'; | } from '@utils/cashMethods'; | ||||
import { | import { | ||||
unbatchedArray, | unbatchedArray, | ||||
arrayBatchedByThree, | arrayBatchedByThree, | ||||
} from '../__mocks__/mockBatchedArrays'; | } from '../__mocks__/mockBatchedArrays'; | ||||
import { | |||||
previousUtxos, | |||||
utxosReceivingBcha, | |||||
utxosReceivingBchaDelta, | |||||
utxosReceivingSlpa, | |||||
utxosReceivingSlpaDelta, | |||||
parsedChangedUtxosBoth, | |||||
parsedChangedUtxosHydrateOnly, | |||||
unchangedUtxosSameAddresses, | |||||
templatePreviouslyHydratedUtxos, | |||||
templateNewHydratedUtxos, | |||||
templateMergedHydratedUtxos, | |||||
legacyMergedHydratedUtxoDetails, | |||||
organizedMergedHydratedUtxoDetails, | |||||
unorganizedHydratedUtxoDetailsWithDuplicateAddresses, | |||||
organizedHydratedUtxoDetails, | |||||
unorganizedHydratedUtxoDetailsWithTwoDuplicateAddresses, | |||||
organizedHydratedUtxoDetailsFromTwoDuplicateAddresses, | |||||
organizedHydratedUtxoDetailsHdWalletMock, | |||||
templateAddedHydratedUtxos, | |||||
templateNewHydratedUtxosWithDuplicates, | |||||
utxoSetBeforeSendingTx, | |||||
utxoSetAfterSendingTx, | |||||
utxosConsumedByTransaction, | |||||
confirmedUtxosFromSentTxDelta, | |||||
hydratedUtxoDetailsBeforeSendingTx, | |||||
hydratedUtxoDetailsAfterConsumedUtxosRemoved, | |||||
} from '../__mocks__/mockChangingUtxos'; | |||||
import { | import { | ||||
unflattenedHydrateUtxosResponse, | unflattenedHydrateUtxosResponse, | ||||
flattenedHydrateUtxosResponse, | flattenedHydrateUtxosResponse, | ||||
} from '../__mocks__/flattenBatchedHydratedUtxosMocks'; | } from '../__mocks__/flattenBatchedHydratedUtxosMocks'; | ||||
import { | import { | ||||
cachedUtxos, | cachedUtxos, | ||||
utxosLoadedFromCache, | utxosLoadedFromCache, | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | it(`Accepts a cachedWalletState that has not preserved BigNumber object types, and returns the same wallet state with BigNumber type re-instituted`, () => { | ||||
); | ); | ||||
}); | }); | ||||
it(`Recognizes a stored wallet as valid if it has all required fields`, () => { | it(`Recognizes a stored wallet as valid if it has all required fields`, () => { | ||||
expect(isValidStoredWallet(validStoredWallet)).toBe(true); | expect(isValidStoredWallet(validStoredWallet)).toBe(true); | ||||
}); | }); | ||||
it(`Recognizes a stored wallet as invalid if it is missing required fields`, () => { | it(`Recognizes a stored wallet as invalid if it is missing required fields`, () => { | ||||
expect(isValidStoredWallet(invalidStoredWallet)).toBe(false); | expect(isValidStoredWallet(invalidStoredWallet)).toBe(false); | ||||
}); | }); | ||||
it(`whichUtxosChanged() returns an empty array if compared utxo sets are identical`, () => { | |||||
expect( | |||||
whichUtxosChanged(utxosReceivingBcha, utxosReceivingBcha), | |||||
).toStrictEqual(unchangedUtxosSameAddresses); | |||||
}); | |||||
it(`whichUtxosChanged() returns an empty array if compared utxo sets are identical`, () => { | |||||
expect( | |||||
whichUtxosChanged(utxosReceivingBchaDelta, utxosReceivingBchaDelta), | |||||
).toStrictEqual(unchangedUtxosSameAddresses); | |||||
}); | |||||
it(`whichUtxosChanged identifies new utxo from received ${currency.ticker} transaction`, () => { | |||||
expect( | |||||
whichUtxosChanged(utxosReceivingBcha, previousUtxos), | |||||
).toStrictEqual(utxosReceivingBchaDelta); | |||||
}); | |||||
it(`whichUtxosChanged identifies new utxo from received ${currency.tokenTicker} transaction`, () => { | |||||
expect( | |||||
whichUtxosChanged(utxosReceivingSlpa, utxosReceivingBcha), | |||||
).toStrictEqual(utxosReceivingSlpaDelta); | |||||
}); | |||||
it(`parseChangedUtxos correctly flags a new utxo as hydrationRequired `, () => { | |||||
expect( | |||||
parseChangedUtxos(previousUtxos, utxosReceivingBchaDelta), | |||||
).toStrictEqual(parsedChangedUtxosHydrateOnly); | |||||
}); | |||||
it(`parseChangedUtxos correctly flags a new utxo as hydrationRequired and an existing utxo with new blockheight as txConfirmed`, () => { | |||||
expect( | |||||
parseChangedUtxos(utxosReceivingBcha, utxosReceivingSlpaDelta), | |||||
).toStrictEqual(parsedChangedUtxosBoth); | |||||
}); | |||||
it(`Correctly identifies a utxosToBeHydrated array with 1 utxo at 1 address as not empty`, () => { | |||||
expect( | |||||
isUtxoArrayEmpty(parsedChangedUtxosBoth.utxosToHydrate), | |||||
).toStrictEqual(false); | |||||
}); | |||||
it(`Correctly identifies a utxosToBeHydrated array with no utxos at any address as empty`, () => { | |||||
expect( | |||||
isUtxoArrayEmpty(parsedChangedUtxosHydrateOnly.utxosConfirmed), | |||||
).toStrictEqual(true); | |||||
}); | |||||
it(`Correctly organizes existing hydratedUtxoDetails object with duplicate address entries; template data`, () => { | |||||
expect( | |||||
organizeHydratedUtxoDetailsByAddress( | |||||
unorganizedHydratedUtxoDetailsWithDuplicateAddresses, | |||||
), | |||||
).toStrictEqual(organizedHydratedUtxoDetails); | |||||
}); | |||||
it(`Correctly organizes existing hydratedUtxoDetails object with duplicate address entries; utxo data`, () => { | |||||
expect( | |||||
organizeHydratedUtxoDetailsByAddress( | |||||
legacyMergedHydratedUtxoDetails, | |||||
), | |||||
).toStrictEqual(organizedMergedHydratedUtxoDetails); | |||||
}); | |||||
it(`Correctly organizes existing hydratedUtxoDetails object with two duplicate address entries`, () => { | |||||
expect( | |||||
organizeHydratedUtxoDetailsByAddress( | |||||
unorganizedHydratedUtxoDetailsWithTwoDuplicateAddresses, | |||||
), | |||||
).toStrictEqual(organizedHydratedUtxoDetailsFromTwoDuplicateAddresses); | |||||
}); | |||||
it(`Does nothing with existing hydratedUtxoDetails object with multiple duplicate address entries if none are duplicated`, () => { | |||||
expect( | |||||
organizeHydratedUtxoDetailsByAddress( | |||||
organizedHydratedUtxoDetailsHdWalletMock, | |||||
), | |||||
).toStrictEqual(organizedHydratedUtxoDetailsHdWalletMock); | |||||
}); | |||||
it(`Correctly combines existing hydratedUtxoDetails object with one of only the changed utxos`, () => { | |||||
expect( | |||||
mergeHydratedUtxoDetails( | |||||
templatePreviouslyHydratedUtxos, | |||||
templateNewHydratedUtxos, | |||||
), | |||||
).toStrictEqual(templateMergedHydratedUtxos); | |||||
}); | |||||
it(`Correctly adds hydratedUtxoDetails of new utxos to existing previousHydratedUtxos object`, () => { | |||||
expect( | |||||
addHydratedUtxoDetailsToWalletStateIfNew( | |||||
templatePreviouslyHydratedUtxos, | |||||
templateNewHydratedUtxos, | |||||
), | |||||
).toStrictEqual(templateAddedHydratedUtxos); | |||||
}); | |||||
it(`Correctly adds hydratedUtxoDetails of new utxos to existing previousHydratedUtxos object without adding duplicates`, () => { | |||||
expect( | |||||
addHydratedUtxoDetailsToWalletStateIfNew( | |||||
templatePreviouslyHydratedUtxos, | |||||
templateNewHydratedUtxosWithDuplicates, | |||||
), | |||||
).toStrictEqual(templateAddedHydratedUtxos); | |||||
}); | |||||
it(`Determines which utxos have been consumed`, () => { | |||||
expect( | |||||
whichUtxosWereConsumed( | |||||
utxoSetAfterSendingTx, | |||||
utxoSetBeforeSendingTx, | |||||
confirmedUtxosFromSentTxDelta, | |||||
), | |||||
).toStrictEqual(utxosConsumedByTransaction); | |||||
}); | |||||
it(`Removes consumed utxos from hydratedUtxoDetails`, () => { | |||||
expect( | |||||
removeConsumedUtxosFromHydratedUtxoDetails( | |||||
hydratedUtxoDetailsBeforeSendingTx, | |||||
utxosConsumedByTransaction, | |||||
), | |||||
).toStrictEqual(hydratedUtxoDetailsAfterConsumedUtxosRemoved); | |||||
}); | |||||
it(`Determines which utxos have been consumed and which utxos are new`, () => { | |||||
const changedUtxos = whichUtxosChanged( | |||||
utxoSetAfterSendingTx, | |||||
utxoSetBeforeSendingTx, | |||||
); | |||||
const parsedChangedUtxosResult = parseChangedUtxos( | |||||
utxoSetAfterSendingTx, | |||||
changedUtxos, | |||||
); | |||||
const consumedUtxos = whichUtxosWereConsumed( | |||||
utxoSetAfterSendingTx, | |||||
utxoSetBeforeSendingTx, | |||||
parsedChangedUtxosResult.utxosConfirmed, | |||||
); | |||||
expect(consumedUtxos).toStrictEqual(utxosConsumedByTransaction); | |||||
}); | |||||
}); | }); |