From 60ba9b76f556da32bc8cc49f74e4009193bd9404 Mon Sep 17 00:00:00 2001 From: simsbluebox Date: Thu, 18 Jul 2024 18:35:42 -0300 Subject: [PATCH] tests: coverage on fungible token balances and null token cases --- test/alexSDK.mock-exceptions.test.ts | 8 ++++++++ test/alexSDK.mock-helpers.test.ts | 24 +++++++++++++++++------- test/alexSDK.test.ts | 6 ++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/test/alexSDK.mock-exceptions.test.ts b/test/alexSDK.mock-exceptions.test.ts index 9953cb0..4af00d9 100644 --- a/test/alexSDK.mock-exceptions.test.ts +++ b/test/alexSDK.mock-exceptions.test.ts @@ -1,5 +1,6 @@ import { AlexSDK, Currency } from '../src'; import * as ammRouteResolver from '../src/utils/ammRouteResolver'; +import { assertNever } from '../src/utils/utils'; import { configs } from '../src/config'; const sdk = new AlexSDK(); @@ -37,4 +38,11 @@ describe('AlexSDK - mock exceptions', () => { ) ).rejects.toThrow('Too many AMM pools in route'); }, 10000); + + it('Attempt assertNever to throw unexpected object', () => { + const unexpectedObject = '' as never; + expect(() => assertNever(unexpectedObject)).toThrowError( + 'Unexpected object: ' + unexpectedObject + ); + }); }); diff --git a/test/alexSDK.mock-helpers.test.ts b/test/alexSDK.mock-helpers.test.ts index 034d701..a1ad1e4 100644 --- a/test/alexSDK.mock-helpers.test.ts +++ b/test/alexSDK.mock-helpers.test.ts @@ -13,7 +13,6 @@ import { dummyBalances, dummyCurrencies, dummyFee, - dummyPrices, dummyRate, dummyTx, parsedDummyPrices, @@ -21,7 +20,8 @@ import { dummyTokenB, dummyFactorA, dummyFactorB, - dummyTokenC, DUMMY_DEPLOYER, + dummyTokenC, + DUMMY_DEPLOYER, } from './mock-data/alexSDKMockResponses'; import { cvToValue, FungibleConditionCode } from '@stacks/transactions'; @@ -53,10 +53,16 @@ jest.mock('../src/helpers/SwapHelper', () => { }); jest.mock('../src/utils/fetchData', () => { const originalModule = jest.requireActual('../src/utils/fetchData'); + const { dummyPrices, dummyCurrencies } = jest.requireActual( + './mock-data/alexSDKMockResponses' + ); return { __esModule: true, ...originalModule, - getPrices: jest.fn(async () => dummyPrices), + getPrices: jest + .fn() + .mockReturnValueOnce(dummyPrices) + .mockReturnValueOnce(originalModule.getPrices(dummyCurrencies)), fetchBalanceForAccount: jest.fn(async () => dummyBalances), getAlexSDKData: jest.fn(async () => dummyAlexSDKData), }; @@ -70,7 +76,7 @@ jest.mock('../src/utils/ammRouteResolver', () => { } return originalModule.resolveAmmRoute(tokenX, ...args); }), - } + }; }); describe('AlexSDK - mock helpers', () => { @@ -146,9 +152,7 @@ describe('AlexSDK - mock helpers', () => { amount, BigInt(0) ) - ).rejects.toThrow( - 'Can\'t find AMM route' - ); + ).rejects.toThrow("Can't find AMM route"); }); it('Verify response value of getLatestPrices function', async () => { @@ -157,6 +161,12 @@ describe('AlexSDK - mock helpers', () => { expect(result).toStrictEqual(parsedDummyPrices); }); + it('Verify response value of getLatestPrices function (null token cases)', async () => { + expect(jest.isMockFunction(fetchData.getPrices)).toBeTruthy(); + const result = await sdk.getLatestPrices(); + expect(result).toBeDefined(); + }); + it('Verify response value of getBalances function', async () => { expect(jest.isMockFunction(fetchData.fetchBalanceForAccount)).toBeTruthy(); const stxAddress = 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT'; diff --git a/test/alexSDK.test.ts b/test/alexSDK.test.ts index d1bc213..d17cdfb 100644 --- a/test/alexSDK.test.ts +++ b/test/alexSDK.test.ts @@ -255,6 +255,12 @@ describe('AlexSDK', () => { }); }, 10000); + it('Verify response of getBalances function (with fungible token balance)', async () => { + const stxAddress = 'SP3ANPTPEQE72PNE31WE8BEV4VCKB2C38P48TPH0Q'; + const balances = await sdk.getBalances(stxAddress); + expect(balances).toBeDefined(); + }, 10000); + it('Attempt to Get Tx with an invalid stx address (checksum mismatch)', async () => { await expect( sdk.runSwap(