-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds mocking of Tatum library functions to create real unit tests by not calling the Tatum API and using mocks instead.
- Loading branch information
1 parent
cf94533
commit e97456c
Showing
1 changed file
with
64 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); |