Skip to content

Commit

Permalink
test: add mocks to unit tests
Browse files Browse the repository at this point in the history
Adds mocking of Tatum library functions to create real unit tests by not calling the Tatum API and using mocks instead.
  • Loading branch information
martinkyselak committed Jan 6, 2024
1 parent cf94533 commit e97456c
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions src/api/getWallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,76 @@
import { Status } from '@tatumio/tatum';
import { Address, AddressBalance, AddressTransaction, ResponseDto, Status } from '@tatumio/tatum';
import { getWalletData } from './getWallet';

// TODO: mock Tatum library to get real unit tests
afterEach(() => {
jest.clearAllMocks();
});

test('should return error for non-existing address', async () => {
const result = await getWalletData('non-existing-address');
expect(result).toStrictEqual([Status.ERROR, null, null]);
}, 10000);
const WalletAddressWithTransactions = '0xb794f5ea0ba39494ce839613fffba74279579268';

const createTransaction = () => {
return {
chain: '',
blockNumber: 1,
hash: '1',
transactionType: 'incoming' as const,
amount: '1',
timestamp: 1,
address: WalletAddressWithTransactions,
};
};

test('should return valid response for existing address', async () => {
const result = await getWalletData('0xb794f5ea0ba39494ce839613fffba74279579268');
const mockedBalanceResponse: ResponseDto<AddressBalance[]> = {
data: [
{
address: WalletAddressWithTransactions,
balance: '1',
type: 'native',
},
],
status: Status.SUCCESS,
};

const mockedTransactionsResponse: ResponseDto<AddressTransaction[]> = {
data: new Array<AddressTransaction>(10).fill(createTransaction()).map(createTransaction),
status: Status.SUCCESS,
};

jest
.spyOn(Address.prototype, 'getBalance')
.mockReturnValueOnce(Promise.resolve(mockedBalanceResponse));
jest
.spyOn(Address.prototype, 'getTransactions')
.mockReturnValueOnce(Promise.resolve(mockedTransactionsResponse));

const result = await getWalletData(WalletAddressWithTransactions);
expect(result[0]).toBe(Status.SUCCESS);

expect(result[1]).toBeDefined();
expect(result[1][0].balance.length).toBeGreaterThan(0);

expect(result[2]).toBeDefined();
expect(result[2].length).toBe(10);
}, 10000);
});

test('should return error for non-existing address', async () => {
const mockedBalanceResponse: ResponseDto<AddressBalance[]> = {
data: null!,
status: Status.ERROR,
};

const mockedTransactionsResponse: ResponseDto<AddressTransaction[]> = {
data: null!,
status: Status.ERROR,
};

jest
.spyOn(Address.prototype, 'getBalance')
.mockReturnValueOnce(Promise.resolve(mockedBalanceResponse));
jest
.spyOn(Address.prototype, 'getTransactions')
.mockReturnValueOnce(Promise.resolve(mockedTransactionsResponse));

const result = await getWalletData('non-existing-address');
expect(result).toStrictEqual([Status.ERROR, null, null]);
});

0 comments on commit e97456c

Please sign in to comment.