Changeset View
Changeset View
Standalone View
Standalone View
cashtab/src/wallet/__tests__/index.test.js
// Copyright (c) 2024 The Bitcoin developers | // Copyright (c) 2024 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
import { | import { | ||||
getBalanceSats, | getBalanceSats, | ||||
toXec, | toXec, | ||||
toSatoshis, | toSatoshis, | ||||
hasEnoughToken, | hasEnoughToken, | ||||
createCashtabWallet, | createCashtabWallet, | ||||
fiatToSatoshis, | fiatToSatoshis, | ||||
getLegacyPaths, | getLegacyPaths, | ||||
getWalletsForNewActiveWallet, | getWalletsForNewActiveWallet, | ||||
decimalizeTokenAmount, | |||||
undecimalizeTokenAmount, | |||||
removeLeadingZeros, | |||||
} from 'wallet'; | } from 'wallet'; | ||||
import { isValidCashtabWallet } from 'validation'; | import { isValidCashtabWallet } from 'validation'; | ||||
import vectors from '../fixtures/vectors'; | import vectors from '../fixtures/vectors'; | ||||
describe('Cashtab wallet methods', () => { | describe('Cashtab wallet methods', () => { | ||||
describe('Calculates total balance in satoshis from a valid set of chronik utxos', () => { | describe('Calculates total balance in satoshis from a valid set of chronik utxos', () => { | ||||
const { expectedReturns, expectedErrors } = | const { expectedReturns, expectedErrors } = | ||||
vectors.getBalanceSatsVectors; | vectors.getBalanceSatsVectors; | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | describe('Gets expected array when activating a new wallet', () => { | ||||
expectedError; | expectedError; | ||||
it(`getWalletsForNewActiveWallet throws error for: ${description}`, () => { | it(`getWalletsForNewActiveWallet throws error for: ${description}`, () => { | ||||
expect(() => | expect(() => | ||||
getWalletsForNewActiveWallet(walletToActivate, wallets), | getWalletsForNewActiveWallet(walletToActivate, wallets), | ||||
).toThrow(errorMsg); | ).toThrow(errorMsg); | ||||
}); | }); | ||||
}); | }); | ||||
}); | }); | ||||
describe('We can decimalize a token amount string and undecimalize it back', () => { | |||||
const { expectedReturns, expectedErrors } = | |||||
vectors.decimalizeTokenAmount; | |||||
expectedReturns.forEach(expectedReturn => { | |||||
const { description, amount, decimals, returned } = expectedReturn; | |||||
it(`decimalizeTokenAmount: ${description}`, () => { | |||||
expect(decimalizeTokenAmount(amount, decimals)).toBe(returned); | |||||
}); | |||||
it(`undecimalizeTokenAmount: ${description}`, () => { | |||||
expect(undecimalizeTokenAmount(returned, decimals)).toBe( | |||||
amount, | |||||
); | |||||
}); | |||||
}); | |||||
expectedErrors.forEach(expectedError => { | |||||
const { description, amount, decimals, error } = expectedError; | |||||
it(`decimalizeTokenAmount throws error for: ${description}`, () => { | |||||
expect(() => decimalizeTokenAmount(amount, decimals)).toThrow( | |||||
error, | |||||
); | |||||
}); | |||||
}); | |||||
}); | |||||
describe('We can undecimalize a decimalizedTokenAmount string, and we throw expected errors if undecimalizeTokenAmount is invalid', () => { | |||||
const { expectedReturns, expectedErrors } = | |||||
vectors.undecimalizeTokenAmount; | |||||
expectedReturns.forEach(expectedReturn => { | |||||
const { description, decimalizedAmount, decimals, returned } = | |||||
expectedReturn; | |||||
it(`undecimalizeTokenAmount: ${description}`, () => { | |||||
expect( | |||||
undecimalizeTokenAmount(decimalizedAmount, decimals), | |||||
).toBe(returned); | |||||
}); | |||||
// Note that we cannot round trip these tests, as decimalizeTokenAmount will | |||||
// always return exact precision, while undecimalizeTokenAmount tolerates underprecision | |||||
}); | |||||
expectedErrors.forEach(expectedError => { | |||||
const { description, decimalizedAmount, decimals, error } = | |||||
expectedError; | |||||
it(`undecimalizeTokenAmount throws error for: ${description}`, () => { | |||||
expect(() => | |||||
undecimalizeTokenAmount(decimalizedAmount, decimals), | |||||
).toThrow(error); | |||||
}); | |||||
}); | |||||
}); | |||||
describe('Removes leading zeros from a string', () => { | |||||
const { expectedReturns } = vectors.removeLeadingZeros; | |||||
expectedReturns.forEach(expectedReturn => { | |||||
const { description, givenString, returned } = expectedReturn; | |||||
it(`removeLeadingZeros: ${description}`, () => { | |||||
expect(removeLeadingZeros(givenString)).toBe(returned); | |||||
}); | |||||
}); | |||||
}); | |||||
}); | }); |