-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests checkAddress and getRewards (#555)
- Loading branch information
1 parent
87eb1f0
commit f9e31e8
Showing
5 changed files
with
179 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const CosmosV0API = require('../../lib/source/cosmosV0-source') | ||
const networks = require('../../data/networks') | ||
|
||
describe('Cosmos V0 API', function() { | ||
describe('checkAddress()', function() { | ||
let api, cosmosNetworkConfig | ||
|
||
beforeEach(() => { | ||
cosmosNetworkConfig = networks.find( | ||
network => network.id === 'cosmos-hub-testnet' | ||
) | ||
api = new CosmosV0API(cosmosNetworkConfig, {}) | ||
}) | ||
|
||
it('When a valid prefix is passed, it should not throw an error', () => { | ||
expect(() => | ||
api.checkAddress(`${cosmosNetworkConfig.bech32_prefix}12345ABCDE`) | ||
).not.toThrow() | ||
}) | ||
|
||
it('When an invalid prefix is passed, it should throw an error', () => { | ||
expect(() => api.checkAddress('12345ABCDE')).toThrow() | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const CosmosV2API = require('../../lib/source/cosmosV2-source') | ||
const networks = require('../../data/networks') | ||
const { | ||
delegatorAddress, | ||
mockValidatorsDictionary, | ||
delegatorRewards | ||
} = require('./mock_data/delegators') | ||
|
||
let mockDelegatorRewards = { ...delegatorRewards } | ||
jest.mock('apollo-datasource-rest', () => { | ||
class MockRESTDataSource { | ||
constructor() { | ||
this.memoizedResults = { | ||
clear: jest.fn() | ||
} | ||
} | ||
|
||
get() { | ||
return mockDelegatorRewards | ||
} | ||
} | ||
|
||
return { | ||
RESTDataSource: MockRESTDataSource, | ||
HTTPCache: class MockHTTPCache {} | ||
} | ||
}) | ||
|
||
describe('Cosmos V2 API', function() { | ||
describe('getRewards()', function() { | ||
let api, cosmosNetworkConfig | ||
|
||
beforeEach(() => { | ||
cosmosNetworkConfig = networks.find( | ||
network => network.id === 'cosmos-hub-testnet' | ||
) | ||
api = new CosmosV2API(cosmosNetworkConfig, {}) | ||
mockDelegatorRewards = JSON.parse(JSON.stringify(delegatorRewards)) | ||
}) | ||
|
||
it('When an existing delegator address is passed, it should return the calculated rewards', async () => { | ||
//Act | ||
const result = await api.getRewards( | ||
delegatorAddress, | ||
mockValidatorsDictionary | ||
) | ||
|
||
//Assert | ||
expect(result[0]).toHaveProperty('amount') | ||
expect(result[0]).toHaveProperty('denom') | ||
expect(result[0].validator).toEqual( | ||
mockValidatorsDictionary[delegatorAddress] | ||
) | ||
}) | ||
|
||
it('When an existing delegator address has no rewards, it should return no rewards', async () => { | ||
//Arrange | ||
mockDelegatorRewards.result.rewards = [] | ||
|
||
//Act & Assert | ||
await expect( | ||
api.getRewards(delegatorAddress, mockValidatorsDictionary) | ||
).resolves.toEqual([]) | ||
}) | ||
|
||
it('When an existing delegator address is passed with a reward 49000000 (umuon), it should return amount 49 (muon)', async () => { | ||
//Arrange | ||
mockDelegatorRewards.result.rewards[0].reward[0].amount = 49000000 | ||
|
||
//Act & Assert | ||
await expect( | ||
api.getRewards(delegatorAddress, mockValidatorsDictionary) | ||
).resolves.toEqual([ | ||
{ | ||
amount: '49.000000', | ||
denom: 'MUON', | ||
validator: mockValidatorsDictionary[delegatorAddress] | ||
} | ||
]) | ||
}) | ||
|
||
it('When an existing delegator address is passed with a reward < 1 (umuon), it should return amount = 0 (muon)', async () => { | ||
//Arrange | ||
mockDelegatorRewards.result.rewards[0].reward[0].amount = 0.05 | ||
|
||
//Act & Assert | ||
await expect( | ||
api.getRewards(delegatorAddress, mockValidatorsDictionary) | ||
).resolves.toEqual([ | ||
{ | ||
amount: '0.000000', | ||
denom: 'MUON', | ||
validator: mockValidatorsDictionary[delegatorAddress] | ||
} | ||
]) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const delegatorAddress = 'cosmosvaloper1y3v65vhz5f93k8uk7vnz5v7yr7ks5gdcumgcqr' | ||
|
||
const mockValidatorsDictionary = { | ||
[delegatorAddress]: { | ||
networkId: 'cosmos-hub-testnet', | ||
operatorAddress: delegatorAddress, | ||
consensusPubkey: | ||
'cosmosvalconspub1zcjduepqndtvyf7ggaamy2qzafjm9fnn3us4fllv89wtl40nt586q5g90rnsaxtvlz', | ||
jailed: false, | ||
details: '', | ||
website: 'https://fissionlabs.io', | ||
identity: '', | ||
name: 'Fission Labs', | ||
votingPower: '0.084015352', | ||
startHeight: '0', | ||
uptimePercentage: 1, | ||
tokens: '60002.000010', | ||
commissionUpdateTime: '2019-12-20T17:00:00Z', | ||
commission: '0.500000000000000000', | ||
maxCommission: '1.000000000000000000', | ||
maxChangeCommission: '1.000000000000000000', | ||
status: 'ACTIVE', | ||
statusDetailed: 'active', | ||
delegatorShares: '60002000010.000000000000000000' | ||
} | ||
} | ||
|
||
const delegatorRewards = { | ||
result: { | ||
rewards: [ | ||
{ | ||
validator_address: delegatorAddress, | ||
reward: [ | ||
{ | ||
denom: 'umuon', | ||
amount: '0.107080042987452090' | ||
} | ||
] | ||
} | ||
], | ||
total: [ | ||
{ | ||
denom: 'umuon', | ||
amount: '0.107080042987452090' | ||
} | ||
] | ||
} | ||
} | ||
|
||
module.exports = { | ||
delegatorAddress, | ||
mockValidatorsDictionary, | ||
delegatorRewards | ||
} |