Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/__tests__/migrations.test.js
import { currency } from '../../components/Common/Ticker'; | import { currency } from '../../components/Common/Ticker'; | ||||
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import BCHJS from '@psf/bch-js'; | import BCHJS from '@psf/bch-js'; | ||||
import useBCH from '../useBCH'; | |||||
import { | import { | ||||
fromSmallestDenomination, | fromSmallestDenomination, | ||||
toSmallestDenomination, | toSmallestDenomination, | ||||
} from '@utils/cashMethods'; | } from '@utils/cashMethods'; | ||||
describe('Testing functions for upgrading Cashtab', () => { | describe('Testing functions for upgrading Cashtab', () => { | ||||
it('Replacement currency.dust parameter parsing matches legacy DUST parameter', () => { | it('Replacement currency.dustSats parameter parsing matches legacy DUST parameter', () => { | ||||
expect(parseFloat(new BigNumber(currency.dust).toFixed(8))).toBe( | expect( | ||||
0.00000546, | parseFloat( | ||||
); | new BigNumber( | ||||
fromSmallestDenomination(currency.dustSats).toString(), | |||||
).toFixed(8), | |||||
), | |||||
).toBe(0.00000546); | |||||
}); | }); | ||||
it('Replicate 8-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | it('Replicate 8-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const testSendAmount = '0.12345678'; | const testSendAmount = '0.12345678'; | ||||
expect( | expect( | ||||
parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | ||||
).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ||||
}); | }); | ||||
it('Replicate 2-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | it('Replicate 2-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const testSendAmount = '0.12'; | const testSendAmount = '0.12'; | ||||
expect( | expect( | ||||
parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | ||||
).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ||||
}); | }); | ||||
it('Replicate 8-decimal return value from instance of toSatoshi in remainder comparison with toSmallestDenomination', () => { | it('Replicate 8-decimal return value from instance of toSatoshi in remainder comparison with toSmallestDenomination', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
// note: do not specify 8 decimals as this test SHOULD fail when currency.dust changes or cashDecimals changes if not updated | |||||
expect( | expect( | ||||
parseFloat(toSmallestDenomination(new BigNumber(currency.dust))), | parseFloat(toSmallestDenomination(new BigNumber('0.00000546'), 8)), | ||||
).toBe( | ).toBe( | ||||
BCH.BitcoinCash.toSatoshi( | BCH.BitcoinCash.toSatoshi( | ||||
parseFloat(new BigNumber(currency.dust).toFixed(8)), | parseFloat(new BigNumber('0.00000546').toFixed(8)), | ||||
), | ), | ||||
); | ); | ||||
}); | }); | ||||
it('toSmallestDenomination() returns false if input is not a BigNumber', () => { | it('toSmallestDenomination() returns false if input is not a BigNumber', () => { | ||||
const testInput = 132.12345678; | const testInput = 132.12345678; | ||||
expect(toSmallestDenomination(testInput)).toBe(false); | expect(toSmallestDenomination(testInput)).toBe(false); | ||||
}); | }); | ||||
it(`toSmallestDenomination() returns false if input is a BigNumber with more decimals than specified by cashDecimals parameter`, () => { | it(`toSmallestDenomination() returns false if input is a BigNumber with more decimals than specified by cashDecimals parameter`, () => { | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | describe('Testing functions for upgrading Cashtab', () => { | ||||
it('Replicates smallest unit value return value from instance of toBitcoinCash with fromSmallestDenomination and cashDecimals = 8', () => { | it('Replicates smallest unit value return value from instance of toBitcoinCash with fromSmallestDenomination and cashDecimals = 8', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const testSendAmount = '1'; | const testSendAmount = '1'; | ||||
expect(fromSmallestDenomination(testSendAmount, 8)).toBe( | expect(fromSmallestDenomination(testSendAmount, 8)).toBe( | ||||
BCH.BitcoinCash.toBitcoinCash(testSendAmount), | BCH.BitcoinCash.toBitcoinCash(testSendAmount), | ||||
); | ); | ||||
}); | }); | ||||
it(`Converts dust limit in satoshis to dust limit in current app setting`, () => { | |||||
expect(fromSmallestDenomination(currency.dustSats).toString()).toBe( | |||||
'0.00000546', | |||||
); | |||||
}); | |||||
}); | }); |