Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/__tests__/cashMethods.test.js
Show All 18 Lines | import { | ||||
isExcludedUtxo, | isExcludedUtxo, | ||||
whichUtxosWereAdded, | whichUtxosWereAdded, | ||||
whichUtxosWereConsumed, | whichUtxosWereConsumed, | ||||
addNewHydratedUtxos, | addNewHydratedUtxos, | ||||
removeConsumedUtxos, | removeConsumedUtxos, | ||||
getUtxoCount, | getUtxoCount, | ||||
areAllUtxosIncludedInIncrementallyHydratedUtxos, | areAllUtxosIncludedInIncrementallyHydratedUtxos, | ||||
convertEcashtoEtokenAddr, | convertEcashtoEtokenAddr, | ||||
parseChronikTx, | |||||
getHashArrayFromWallet, | |||||
isActiveWebsocket, | |||||
} from 'utils/cashMethods'; | } from 'utils/cashMethods'; | ||||
import { currency } from 'components/Common/Ticker'; | import { currency } from 'components/Common/Ticker'; | ||||
import { | import { | ||||
unbatchedArray, | unbatchedArray, | ||||
arrayBatchedByThree, | arrayBatchedByThree, | ||||
} from '../__mocks__/mockBatchedArrays'; | } from '../__mocks__/mockBatchedArrays'; | ||||
import { | import { | ||||
validAddressArrayInput, | validAddressArrayInput, | ||||
Show All 11 Lines | |||||
import { | import { | ||||
cachedUtxos, | cachedUtxos, | ||||
utxosLoadedFromCache, | utxosLoadedFromCache, | ||||
} from '../__mocks__/mockCachedUtxos'; | } from '../__mocks__/mockCachedUtxos'; | ||||
import { | import { | ||||
validStoredWallet, | validStoredWallet, | ||||
invalidStoredWallet, | invalidStoredWallet, | ||||
} from '../__mocks__/mockStoredWallets'; | } from '../__mocks__/mockStoredWallets'; | ||||
import mockLegacyWallets from 'hooks/__mocks__/mockLegacyWallets'; | |||||
import BCHJS from '@psf/bch-js'; // TODO: should be removed when external lib not needed anymore | |||||
import { | import { | ||||
mockTxDataResults, | mockTxDataResults, | ||||
mockNonEtokenUtxos, | mockNonEtokenUtxos, | ||||
mockTxDataResultsWithEtoken, | mockTxDataResultsWithEtoken, | ||||
mockHydratedUtxosWithNullValues, | mockHydratedUtxosWithNullValues, | ||||
mockHydratedUtxosWithNullValuesSetToFalse, | mockHydratedUtxosWithNullValuesSetToFalse, | ||||
} from '../__mocks__/nullUtxoMocks'; | } from '../__mocks__/nullUtxoMocks'; | ||||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | import { | ||||
incrementalUtxosTemplate, | incrementalUtxosTemplate, | ||||
incrementallyHydratedUtxosTemplate, | incrementallyHydratedUtxosTemplate, | ||||
incrementallyHydratedUtxosTemplateMissing, | incrementallyHydratedUtxosTemplateMissing, | ||||
utxosAfterSentTxIncremental, | utxosAfterSentTxIncremental, | ||||
incrementallyHydratedUtxosAfterProcessing, | incrementallyHydratedUtxosAfterProcessing, | ||||
incrementallyHydratedUtxosAfterProcessingOneMissing, | incrementallyHydratedUtxosAfterProcessingOneMissing, | ||||
} from '../__mocks__/incrementalUtxoMocks'; | } from '../__mocks__/incrementalUtxoMocks'; | ||||
import { | |||||
lambdaHash160s, | |||||
lambdaIncomingXecTx, | |||||
lambdaIncomingEtokenTx, | |||||
lambdaOutgoingXecTx, | |||||
lambdaOutgoingEtokenTx, | |||||
activeWebsocketAlpha, | |||||
disconnectedWebsocketAlpha, | |||||
unsubscribedWebsocket, | |||||
} from '../__mocks__/chronikWebsocket'; | |||||
describe('Correctly executes cash utility functions', () => { | describe('Correctly executes cash utility functions', () => { | ||||
it(`Correctly converts smallest base unit to smallest decimal for cashDecimals = 2`, () => { | it(`Correctly converts smallest base unit to smallest decimal for cashDecimals = 2`, () => { | ||||
expect(fromSmallestDenomination(1, 2)).toBe(0.01); | expect(fromSmallestDenomination(1, 2)).toBe(0.01); | ||||
}); | }); | ||||
it(`Correctly converts largest base unit to smallest decimal for cashDecimals = 2`, () => { | it(`Correctly converts largest base unit to smallest decimal for cashDecimals = 2`, () => { | ||||
expect(fromSmallestDenomination(1000000012345678, 2)).toBe( | expect(fromSmallestDenomination(1000000012345678, 2)).toBe( | ||||
10000000123456.78, | 10000000123456.78, | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 704 Lines • ▼ Show 20 Lines | it(`flattenContactList flattens contactList array of length 1 by returning an array of 1 address`, () => { | ||||
name: 'Alpha', | name: 'Alpha', | ||||
}, | }, | ||||
]), | ]), | ||||
).toStrictEqual(['ecash:qpdkc5p7f25hwkxsr69m3evlj4h7wqq9xcgmjc8sxr']); | ).toStrictEqual(['ecash:qpdkc5p7f25hwkxsr69m3evlj4h7wqq9xcgmjc8sxr']); | ||||
}); | }); | ||||
it(`flattenContactList returns an empty array for invalid input`, () => { | it(`flattenContactList returns an empty array for invalid input`, () => { | ||||
expect(flattenContactList(false)).toStrictEqual([]); | expect(flattenContactList(false)).toStrictEqual([]); | ||||
}); | }); | ||||
it(`Successfully parses an incoming XEC tx`, () => { | |||||
expect( | |||||
parseChronikTx(lambdaIncomingXecTx, lambdaHash160s), | |||||
).toStrictEqual({ | |||||
incoming: true, | |||||
xecAmount: '42', | |||||
isEtokenTx: false, | |||||
}); | |||||
}); | |||||
it(`Successfully parses an incoming eToken tx`, () => { | |||||
expect( | |||||
parseChronikTx(lambdaIncomingEtokenTx, lambdaHash160s), | |||||
).toStrictEqual({ | |||||
incoming: true, | |||||
xecAmount: '5.46', | |||||
isEtokenTx: true, | |||||
slpMeta: { | |||||
tokenId: | |||||
'4bd147fc5d5ff26249a9299c46b80920c0b81f59a60e05428262160ebee0b0c3', | |||||
tokenType: 'FUNGIBLE', | |||||
txType: 'SEND', | |||||
}, | |||||
etokenAmount: '12', | |||||
}); | |||||
}); | |||||
it(`Successfully parses an outgoing XEC tx`, () => { | |||||
expect( | |||||
parseChronikTx(lambdaOutgoingXecTx, lambdaHash160s), | |||||
).toStrictEqual({ | |||||
incoming: false, | |||||
xecAmount: '222', | |||||
isEtokenTx: false, | |||||
}); | |||||
}); | |||||
it(`Successfully parses an outgoing eToken tx`, () => { | |||||
expect( | |||||
parseChronikTx(lambdaOutgoingEtokenTx, lambdaHash160s), | |||||
).toStrictEqual({ | |||||
incoming: false, | |||||
xecAmount: '5.46', | |||||
isEtokenTx: true, | |||||
slpMeta: { | |||||
tokenId: | |||||
'4bd147fc5d5ff26249a9299c46b80920c0b81f59a60e05428262160ebee0b0c3', | |||||
tokenType: 'FUNGIBLE', | |||||
txType: 'SEND', | |||||
}, | |||||
etokenAmount: '17', | |||||
}); | |||||
}); | |||||
it(`getHashArrayFromWallet returns false for a legacy wallet`, () => { | |||||
const BCH = new BCHJS(); | |||||
expect( | |||||
getHashArrayFromWallet(BCH, mockLegacyWallets.legacyAlphaMainnet), | |||||
).toBe(false); | |||||
}); | |||||
it(`Successfully extracts a hash160 array from a migrated wallet object`, () => { | |||||
const BCH = new BCHJS(); | |||||
expect( | |||||
getHashArrayFromWallet( | |||||
BCH, | |||||
mockLegacyWallets.migratedLegacyAlphaMainnet, | |||||
), | |||||
).toStrictEqual([ | |||||
'960c9ed561f1699f0c49974d50b3bb7cdc118625', | |||||
'2be0e0c999e7e77a443ea726f82c441912fca92b', | |||||
'ba8257db65f40359989c7b894c5e88ed7b6344f6', | |||||
]); | |||||
}); | |||||
it(`isActiveWebsocket returns true for an active chronik websocket connection`, () => { | |||||
expect(isActiveWebsocket(activeWebsocketAlpha)).toBe(true); | |||||
}); | |||||
it(`isActiveWebsocket returns false for a disconnected chronik websocket connection`, () => { | |||||
expect(isActiveWebsocket(disconnectedWebsocketAlpha)).toBe(false); | |||||
}); | |||||
it(`isActiveWebsocket returns false for a null chronik websocket connection`, () => { | |||||
expect(isActiveWebsocket(null)).toBe(false); | |||||
}); | |||||
it(`isActiveWebsocket returns false for an active websocket connection with no subscriptions`, () => { | |||||
expect(isActiveWebsocket(unsubscribedWebsocket)).toBe(false); | |||||
}); | |||||
}); | }); |