Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/__tests__/chronik.test.js
import { currency } from 'components/Common/Ticker'; | import { currency } from 'components/Common/Ticker'; | ||||
import { getUtxosChronik } from 'utils/chronik'; | import { getUtxosChronik, addTokenInfo } from 'utils/chronik'; | ||||
import { | import { | ||||
hash160AndAddressObjArray, | hash160AndAddressObjArray, | ||||
mockChronikUtxoResponsesByHash160, | mockChronikUtxoResponsesByHash160, | ||||
chronikUtxos, | chronikUtxos, | ||||
tokensWithoutGenesisInfo, | |||||
tokenInfoArray, | |||||
tokensWithGenesisInfo, | |||||
} from '../__mocks__/chronikUtxos'; | } from '../__mocks__/chronikUtxos'; | ||||
import { ChronikClient } from 'chronik-client'; | import { ChronikClient } from 'chronik-client'; | ||||
import { when } from 'jest-when'; | |||||
/* | /* | ||||
Note: | Note: | ||||
- getUtxosSingleHashChronik is only an API wrapper, no value in unit testing | - getUtxosSingleHashChronik is only an API wrapper, no value in unit testing | ||||
- returnGetUtxosChronikPromise adds the address key/value to the utxo object. | - returnGetUtxosChronikPromise adds the address key/value to the utxo object. | ||||
It exists as a distinct function because chronik cannot accept an array of hash160s | It exists as a distinct function because chronik cannot accept an array of hash160s | ||||
as input. The function returns a promise so cannot be tested on its own, but is tested | as input. The function returns a promise so cannot be tested on its own, but is tested | ||||
in the call to getUtxosChronik | in the call to getUtxosChronik | ||||
*/ | */ | ||||
/* | |||||
it(`getUtxosChronik successfully adds the address field to each utxo`, async () => { | it(`getUtxosChronik successfully adds the address field to each utxo`, async () => { | ||||
// Initialize chronik | // Initialize chronik | ||||
const chronik = new ChronikClient(currency.chronikUrl); | const chronik = new ChronikClient( | ||||
'https://FakeChronikUrlToEnsureMocksOnly.com', | |||||
); | |||||
//Mock the API response from | |||||
//utxos = await chronik.script('p2pkh', hash160).utxos(); | |||||
//in getUtxosSingleHashChronik -- for each hash160 used | |||||
//NOTE: The below implementation does not actually mock the call | |||||
/* | |||||
Mock the API response from | |||||
utxos = await chronik.script('p2pkh', hash160).utxos(); | |||||
in getUtxosSingleHashChronik -- for each hash160 used | |||||
*/ | |||||
for (let i = 0; i < hash160AndAddressObjArray.length; i += 1) { | for (let i = 0; i < hash160AndAddressObjArray.length; i += 1) { | ||||
chronik.script('p2pkh', hash160AndAddressObjArray[i].hash160).utxos = | chronik.script('p2pkh', hash160AndAddressObjArray[i].hash160).utxos = | ||||
jest | jest | ||||
.fn() | .fn() | ||||
.mockResolvedValue( | .mockResolvedValue( | ||||
mockChronikUtxoResponsesByHash160[ | mockChronikUtxoResponsesByHash160[ | ||||
hash160AndAddressObjArray[i].hash160 | hash160AndAddressObjArray[i].hash160 | ||||
], | ], | ||||
); | ); | ||||
} | } | ||||
expect( | expect( | ||||
await getUtxosChronik(chronik, hash160AndAddressObjArray), | await getUtxosChronik(chronik, hash160AndAddressObjArray), | ||||
).toStrictEqual(chronikUtxos); | ).toStrictEqual(chronikUtxos); | ||||
}); | }); | ||||
*/ | |||||
it(`addTokenInfo successfully populates the tokens object with token details from chronik`, async () => { | |||||
// Initialize chronik | |||||
const chronik = new ChronikClient( | |||||
'https://FakeChronikUrlToEnsureMocksOnly.com', | |||||
); | |||||
/* | |||||
Mock the API response from | |||||
tokenInfo = await chronik.tx('tokenId'); | |||||
in returnGetTokenInfoChronikPromise -- for each tokenId used | |||||
*/ | |||||
chronik.tx = jest.fn(); | |||||
for (let i = 0; i < tokenInfoArray.length; i += 1) { | |||||
when(chronik.tx) | |||||
.calledWith(tokenInfoArray[i].txid) | |||||
.mockReturnValue(tokenInfoArray[i]); | |||||
} | |||||
expect(await addTokenInfo(chronik, tokensWithoutGenesisInfo)).toStrictEqual( | |||||
tokensWithGenesisInfo, | |||||
); | |||||
}); |