From 0a8a0998562a3755b459a400ad40eca2d7773a3f Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 13:14:27 +0100 Subject: [PATCH 01/18] optimistic tx query; balance & rewards wait 10bcks --- app/src/renderer/components/common/TmBalance.vue | 13 +++++++++++++ .../components/staking/ModalWithdrawAllRewards.vue | 12 +++++++++++- .../renderer/components/staking/PageValidator.vue | 11 ----------- .../renderer/components/wallet/PageTransactions.vue | 8 -------- app/src/renderer/components/wallet/PageWallet.vue | 10 ---------- app/src/renderer/vuex/modules/connection.js | 6 +----- app/src/renderer/vuex/modules/delegation.js | 5 +++-- app/src/renderer/vuex/modules/distribution.js | 1 + .../renderer/vuex/modules/governance/deposits.js | 1 + .../renderer/vuex/modules/governance/proposals.js | 3 ++- app/src/renderer/vuex/modules/governance/votes.js | 1 + app/src/renderer/vuex/modules/wallet.js | 1 + package.json | 2 +- 13 files changed, 35 insertions(+), 39 deletions(-) diff --git a/app/src/renderer/components/common/TmBalance.vue b/app/src/renderer/components/common/TmBalance.vue index 2b91e440db..6cf61ef538 100644 --- a/app/src/renderer/components/common/TmBalance.vue +++ b/app/src/renderer/components/common/TmBalance.vue @@ -56,6 +56,7 @@ export default { `connected`, `session`, `liquidAtoms`, + `lastHeader`, `totalAtoms`, `bondDenom`, `distribution` @@ -70,6 +71,18 @@ export default { ) } }, + watch: { + lastHeader: { + immediate: true, + handler(newHeader) { + const waitTenBlocks = Number(newHeader.height) % 10 === 0 + if (this.session.signedIn && waitTenBlocks) { + this.$store.dispatch(`getTotalRewards`) + this.$store.dispatch(`queryWalletBalances`) + } + } + } + }, methods: { onWithdrawal() { this.$refs.modalWithdrawAllRewards.open() diff --git a/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue b/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue index c27fbfaa40..d66b6728ab 100644 --- a/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue +++ b/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue @@ -38,12 +38,22 @@ export default { TmFormGroup }, computed: { - ...mapGetters([`bondDenom`, `distribution`]), + ...mapGetters([`bondDenom`, `distribution`, `lastHeader`, `session`]), totalRewards({ bondDenom, distribution } = this) { const rewards = distribution.totalRewards[bondDenom] return (rewards && atoms(rewards)) || 0 } }, + watch: { + lastHeader: { + immediate: true, + handler() { + if (this.session.signedIn && this.$refs.actionModal.show) { + this.$store.dispatch(`getTotalRewards`) + } + } + } + }, methods: { open() { this.$refs.actionModal.open() diff --git a/app/src/renderer/components/staking/PageValidator.vue b/app/src/renderer/components/staking/PageValidator.vue index 696e813344..1b4a2bb009 100644 --- a/app/src/renderer/components/staking/PageValidator.vue +++ b/app/src/renderer/components/staking/PageValidator.vue @@ -315,17 +315,6 @@ export default { if (!validator) return this.$store.dispatch(`getSelfBond`, validator) } - }, - lastHeader: { - immediate: true, - handler() { - if (this.session.signedIn) { - this.$store.dispatch( - `getRewardsFromValidator`, - this.$route.params.validator - ) - } - } } }, mounted() { diff --git a/app/src/renderer/components/wallet/PageTransactions.vue b/app/src/renderer/components/wallet/PageTransactions.vue index a76a33d67d..e663a58eda 100644 --- a/app/src/renderer/components/wallet/PageTransactions.vue +++ b/app/src/renderer/components/wallet/PageTransactions.vue @@ -78,14 +78,6 @@ export default { return this.orderedTransactions.length === 0 } }, - watch: { - lastHeader: { - immediate: true, - handler() { - this.refreshTransactions() - } - } - }, mounted() { this.refreshTransactions() }, diff --git a/app/src/renderer/components/wallet/PageWallet.vue b/app/src/renderer/components/wallet/PageWallet.vue index 9122a168a2..cedd5e9b4e 100644 --- a/app/src/renderer/components/wallet/PageWallet.vue +++ b/app/src/renderer/components/wallet/PageWallet.vue @@ -75,16 +75,6 @@ export default { ) } }, - watch: { - lastHeader: { - immediate: true, - handler() { - if (this.session.signedIn) { - this.queryWalletBalances() - } - } - } - }, async mounted() { this.updateDelegates() await this.queryWalletBalances() diff --git a/app/src/renderer/vuex/modules/connection.js b/app/src/renderer/vuex/modules/connection.js index a215676c50..f64e62ee68 100644 --- a/app/src/renderer/vuex/modules/connection.js +++ b/app/src/renderer/vuex/modules/connection.js @@ -4,7 +4,7 @@ import config from "../../../config" const NODE_HALTED_TIMEOUT = config.node_halted_timeout -export default function({ node }) { +export default function ({ node }) { // get tendermint RPC client from basecoin client const state = { @@ -98,10 +98,6 @@ export default function({ node }) { }, ({ header }) => { dispatch(`setLastHeader`, header) - - if (rootState.session.signedIn) { - dispatch(`getTotalRewards`) - } } ) if (rootState.session.signedIn) { diff --git a/app/src/renderer/vuex/modules/delegation.js b/app/src/renderer/vuex/modules/delegation.js index 1501accd3c..5011b40f30 100644 --- a/app/src/renderer/vuex/modules/delegation.js +++ b/app/src/renderer/vuex/modules/delegation.js @@ -42,7 +42,7 @@ export default ({ node }) => { setUnbondingDelegations(state, unbondingDelegations) { state.unbondingDelegations = unbondingDelegations ? unbondingDelegations - // building a dict from the array and taking out the validators with no undelegations + // building a dict from the array and taking out the validators with no undelegations .reduce( (dict, { validator_address, entries }) => ({ ...dict, @@ -134,7 +134,7 @@ export default ({ node }) => { async updateDelegates({ dispatch, rootState }) { const candidates = await dispatch(`getDelegates`) - if(rootState.session.signedIn) { + if (rootState.session.signedIn) { dispatch(`getBondedDelegates`, candidates) } }, @@ -175,6 +175,7 @@ export default ({ node }) => { value: state.committedDelegates[validator_address] + Number(amount) }) + await dispatch(`getAllTxs`) // load delegates after delegation to get new atom distribution on validators dispatch(`updateDelegates`) }, diff --git a/app/src/renderer/vuex/modules/distribution.js b/app/src/renderer/vuex/modules/distribution.js index 9a5b9b1147..2b0ce81c1c 100644 --- a/app/src/renderer/vuex/modules/distribution.js +++ b/app/src/renderer/vuex/modules/distribution.js @@ -97,6 +97,7 @@ export default ({ node }) => { }) await dispatch(`getTotalRewards`) await dispatch(`queryWalletBalances`) + await dispatch(`getAllTxs`) }, async getRewardsFromAllValidators({ state, dispatch }, validators) { state.loading = true diff --git a/app/src/renderer/vuex/modules/governance/deposits.js b/app/src/renderer/vuex/modules/governance/deposits.js index 6f7155f78e..452ac0359b 100644 --- a/app/src/renderer/vuex/modules/governance/deposits.js +++ b/app/src/renderer/vuex/modules/governance/deposits.js @@ -61,6 +61,7 @@ export default ({ node }) => { await dispatch(`getProposalDeposits`, proposal_id) await dispatch(`getProposal`, proposal_id) + await dispatch(`getAllTxs`) } } return { diff --git a/app/src/renderer/vuex/modules/governance/proposals.js b/app/src/renderer/vuex/modules/governance/proposals.js index 3db3813c1c..fcd6a1fbdf 100644 --- a/app/src/renderer/vuex/modules/governance/proposals.js +++ b/app/src/renderer/vuex/modules/governance/proposals.js @@ -105,7 +105,7 @@ export default ({ node }) => { }) const latestId = Object.keys(state.proposals).reduce((latest, id) => { - return latest > Number(id) ? latest : Number(id) + return latest > Number(id) ? latest : Number(id) }, 0) commit(`setProposal`, { proposal_id: String(latestId + 1), @@ -115,6 +115,7 @@ export default ({ node }) => { }) await dispatch(`getProposals`) + await dispatch(`getAllTxs`) } } return { diff --git a/app/src/renderer/vuex/modules/governance/votes.js b/app/src/renderer/vuex/modules/governance/votes.js index ab4e06656d..065de6fe2a 100644 --- a/app/src/renderer/vuex/modules/governance/votes.js +++ b/app/src/renderer/vuex/modules/governance/votes.js @@ -46,6 +46,7 @@ export default ({ node }) => { }) await dispatch(`getProposalVotes`, proposal_id) await dispatch(`getProposal`, proposal_id) + await dispatch(`getAllTxs`) } } return { diff --git a/app/src/renderer/vuex/modules/wallet.js b/app/src/renderer/vuex/modules/wallet.js index 2e656740a0..bd5ac4f66b 100644 --- a/app/src/renderer/vuex/modules/wallet.js +++ b/app/src/renderer/vuex/modules/wallet.js @@ -108,6 +108,7 @@ export default ({ node }) => { denom, amount: oldBalance.amount - amount }) + await dispatch(`getAllTxs`) }, loadDenoms({ state, commit }) { commit(`setDenoms`, state.externals.config.denoms) diff --git a/package.json b/package.json index a2762302bb..cd284f0f29 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "frontend:fixed-https": "yarn frontend --cert 'server_dev.crt' --key 'server_dev.key'", "connect": "concurrently -k \"yarn stargate\" \"yarn proxy\"", "backend:new": "concurrently -k \"yarn connect\" \"yarn nodes 2\"", - "backend": "concurrently -k \"yarn yarn connect\" \"yarn nodes 2 skip-rebuild\"", + "backend": "concurrently -k \"yarn connect\" \"yarn nodes 2 skip-rebuild\"", "backend:fixed-https": "concurrently -k \"yarn nodes 2 skip-rebuild\" \"yarn stargate --ssl-certfile 'server_dev.crt' --ssl-keyfile 'server_dev.key'\"" }, "devDependencies": { From afad48f4ddd4a7c4a6ec0ab964e3aed3299e21a0 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 13:56:53 +0100 Subject: [PATCH 02/18] fix call --- .../components/staking/LiValidator.vue | 33 ++++++------------- .../staking/ModalWithdrawAllRewards.vue | 6 +++- .../components/staking/PageValidator.vue | 16 +++++++++ .../components/staking/TabMyDelegations.vue | 17 ++++++++++ .../components/staking/TableValidators.vue | 10 ++---- 5 files changed, 50 insertions(+), 32 deletions(-) diff --git a/app/src/renderer/components/staking/LiValidator.vue b/app/src/renderer/components/staking/LiValidator.vue index 11ae7ae6cb..d99d9b41ae 100644 --- a/app/src/renderer/components/staking/LiValidator.vue +++ b/app/src/renderer/components/staking/LiValidator.vue @@ -112,15 +112,15 @@ export default { yourVotes() { return this.committedDelegations[this.validator.operator_address] ? BigNumber( - num - .atoms( - calculateTokens( - this.validator, - this.committedDelegations[this.validator.operator_address] + num + .atoms( + calculateTokens( + this.validator, + this.committedDelegations[this.validator.operator_address] + ) ) - ) - .toString() - ) + .toString() + ) : BigNumber(0) }, styles() { @@ -132,8 +132,8 @@ export default { return this.validator.revoked ? `Revoked` : this.validator.isValidator - ? `Validator` - : `Candidate` + ? `Validator` + : `Candidate` }, powerRatio() { return ratToBigNumber(this.validator.tokens) @@ -171,19 +171,6 @@ export default { ? num.shortNumber(num.atoms(validatorRewards[this.bondDenom]) || 0) : null } - }, - watch: { - lastHeader: { - immediate: true, - handler() { - if (this.yourVotes > 0) { - this.$store.dispatch( - `getRewardsFromValidator`, - this.validator.operator_address - ) - } - } - } } } diff --git a/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue b/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue index d66b6728ab..40a824dcef 100644 --- a/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue +++ b/app/src/renderer/components/staking/ModalWithdrawAllRewards.vue @@ -48,7 +48,11 @@ export default { lastHeader: { immediate: true, handler() { - if (this.session.signedIn && this.$refs.actionModal.show) { + if ( + this.session.signedIn && + this.$refs.actionModal && + this.$refs.actionModal.show + ) { this.$store.dispatch(`getTotalRewards`) } } diff --git a/app/src/renderer/components/staking/PageValidator.vue b/app/src/renderer/components/staking/PageValidator.vue index 1b4a2bb009..39b6353529 100644 --- a/app/src/renderer/components/staking/PageValidator.vue +++ b/app/src/renderer/components/staking/PageValidator.vue @@ -315,6 +315,22 @@ export default { if (!validator) return this.$store.dispatch(`getSelfBond`, validator) } + }, + lastHeader: { + immediate: true, + handler(newHeader) { + const waitTwentyBlocks = Number(newHeader.height) % 20 === 0 + if ( + this.session.signedIn && + waitTwentyBlocks && + this.$route.name === `validator` + ) { + this.$store.dispatch( + `getRewardsFromValidator`, + this.$route.params.validator + ) + } + } } }, mounted() { diff --git a/app/src/renderer/components/staking/TabMyDelegations.vue b/app/src/renderer/components/staking/TabMyDelegations.vue index ffa7b03675..ca0ceb0bea 100644 --- a/app/src/renderer/components/staking/TabMyDelegations.vue +++ b/app/src/renderer/components/staking/TabMyDelegations.vue @@ -115,6 +115,23 @@ export default { watch: { "session.signedIn": function() { this.loadStakingTxs() + }, + lastHeader: { + immediate: true, + handler(newHeader) { + const waitTenBlocks = Number(newHeader.height) % 10 === 0 + if ( + waitTenBlocks && + this.session.signedIn && + this.$route.path.includes(`/staking/`) && + this.yourValidators + ) { + this.$store.dispatch( + `getRewardsFromAllValidators`, + this.yourValidators + ) + } + } } }, async mounted() { diff --git a/app/src/renderer/components/staking/TableValidators.vue b/app/src/renderer/components/staking/TableValidators.vue index b365ad1d50..e5ca172c48 100644 --- a/app/src/renderer/components/staking/TableValidators.vue +++ b/app/src/renderer/components/staking/TableValidators.vue @@ -51,7 +51,8 @@ export default { `liquidAtoms`, `connected`, `bondDenom`, - `keybase` + `keybase`, + `lastHeader` ]), vpTotal() { return this.validators @@ -138,13 +139,6 @@ export default { watch: { address: function() { this.session.address && this.$store.dispatch(`updateDelegates`) - }, - validators: function(validators) { - if (!validators || validators.length === 0 || !this.session.signedIn) { - return - } - - this.$store.dispatch(`getRewardsFromAllValidators`, validators) } } } From 0b1fabb7ad2570617339ae2dee4d97413b536456 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 13:57:13 +0100 Subject: [PATCH 03/18] lint --- .../components/staking/LiValidator.vue | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/renderer/components/staking/LiValidator.vue b/app/src/renderer/components/staking/LiValidator.vue index d99d9b41ae..dbbcb51324 100644 --- a/app/src/renderer/components/staking/LiValidator.vue +++ b/app/src/renderer/components/staking/LiValidator.vue @@ -112,15 +112,15 @@ export default { yourVotes() { return this.committedDelegations[this.validator.operator_address] ? BigNumber( - num - .atoms( - calculateTokens( - this.validator, - this.committedDelegations[this.validator.operator_address] - ) + num + .atoms( + calculateTokens( + this.validator, + this.committedDelegations[this.validator.operator_address] ) - .toString() - ) + ) + .toString() + ) : BigNumber(0) }, styles() { @@ -132,8 +132,8 @@ export default { return this.validator.revoked ? `Revoked` : this.validator.isValidator - ? `Validator` - : `Candidate` + ? `Validator` + : `Candidate` }, powerRatio() { return ratToBigNumber(this.validator.tokens) From 9ce1183540612e6e3a7c63e45ed683bc4cb9b9b6 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 15:20:35 +0100 Subject: [PATCH 04/18] some testing --- .../components/wallet/PageTransactions.vue | 3 +- app/src/renderer/vuex/modules/delegation.js | 3 + .../specs/components/common/TmBalance.spec.js | 6 +- .../staking/ModalWithdrawAllRewards.spec.js | 4 +- .../components/staking/PageValidator.spec.js | 12 ++- .../staking/TabMyDelegations.spec.js | 3 +- .../staking/TableValidators.spec.js | 98 ------------------- .../wallet/PageTransactions.spec.js | 6 -- .../components/wallet/PageWallet.spec.js | 2 +- .../__snapshots__/delegation.spec.js.snap | 47 --------- test/unit/specs/store/delegation.spec.js | 19 ++-- 11 files changed, 32 insertions(+), 171 deletions(-) delete mode 100644 test/unit/specs/store/__snapshots__/delegation.spec.js.snap diff --git a/app/src/renderer/components/wallet/PageTransactions.vue b/app/src/renderer/components/wallet/PageTransactions.vue index e663a58eda..e8a8189fec 100644 --- a/app/src/renderer/components/wallet/PageTransactions.vue +++ b/app/src/renderer/components/wallet/PageTransactions.vue @@ -61,8 +61,7 @@ export default { `session`, `bondDenom`, `delegation`, - `delegates`, - `lastHeader` + `delegates` ]), orderedTransactions() { return orderBy( diff --git a/app/src/renderer/vuex/modules/delegation.js b/app/src/renderer/vuex/modules/delegation.js index 5011b40f30..fa7eb2ea21 100644 --- a/app/src/renderer/vuex/modules/delegation.js +++ b/app/src/renderer/vuex/modules/delegation.js @@ -199,6 +199,7 @@ export default ({ node }) => { password, submitType }) + await dispatch(`getAllTxs`) }, async submitRedelegation( { @@ -221,6 +222,8 @@ export default ({ node }) => { password, submitType }) + + await dispatch(`getAllTxs`) } } diff --git a/test/unit/specs/components/common/TmBalance.spec.js b/test/unit/specs/components/common/TmBalance.spec.js index d35d67e8f2..c3a0d31e7f 100644 --- a/test/unit/specs/components/common/TmBalance.spec.js +++ b/test/unit/specs/components/common/TmBalance.spec.js @@ -19,8 +19,10 @@ describe(`TmBalance`, () => { totalRewards: { stake: 1000450000000 } - } - } + }, + lastHeader: { height: `10` } + }, + dispatch: jest.fn() } wrapper = shallowMount(TmBalance, { diff --git a/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js b/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js index 397cd27205..dec6706d61 100644 --- a/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js +++ b/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js @@ -12,7 +12,9 @@ describe(`ModalWithdrawAllRewards`, () => { dispatch: jest.fn(), getters: { bondDenom: `stake`, - distribution: { totalRewards: { stake: 10000000 } } + distribution: { totalRewards: { stake: 10000000 } }, + lastHeader: { height: `10` }, + session: { signedIn: true } } } wrapper = shallowMount(ModalWithdrawAllRewards, { diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index fb58997d24..592eab8ef9 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -56,7 +56,7 @@ const getters = { [validator.operator_address]: 0 }, lastHeader: { - height: 500 + height: `500` }, distribution: { rewards: { @@ -214,12 +214,13 @@ describe(`PageValidator`, () => { }) describe(`rewards`, () => { - let bondDenom, validator, session + let bondDenom, validator, session, lastHeader beforeEach(() => { bondDenom = `stake` validator = { operator_address: `cosmos1address` } session = { signedIn: true } + lastHeader = { height: `20` } }) it(`gets rewards from validator if it has some`, () => { const distribution = { @@ -230,7 +231,7 @@ describe(`PageValidator`, () => { } } const rewardsString = PageValidator.computed.rewards.call( - { session, bondDenom, distribution, validator } + { session, bondDenom, distribution, validator, lastHeader } ) expect(rewardsString).toBe(`100.000000 stake`) }) @@ -243,8 +244,9 @@ describe(`PageValidator`, () => { } } } + const rewardsString = PageValidator.computed.rewards.call( - { session, bondDenom, distribution, validator } + { session, bondDenom, distribution, validator, lastHeader } ) expect(rewardsString).toBe(`0.000000 stake`) }) @@ -252,7 +254,7 @@ describe(`PageValidator`, () => { it(`when user doesn't have any delegations`, () => { const distribution = { rewards: {} } const rewardsString = PageValidator.computed.rewards.call( - { session, bondDenom, distribution, validator } + { session, bondDenom, distribution, validator, lastHeader } ) expect(rewardsString).toBeNull() }) diff --git a/test/unit/specs/components/staking/TabMyDelegations.spec.js b/test/unit/specs/components/staking/TabMyDelegations.spec.js index 09e3a921df..308d0dd3bc 100644 --- a/test/unit/specs/components/staking/TabMyDelegations.spec.js +++ b/test/unit/specs/components/staking/TabMyDelegations.spec.js @@ -20,7 +20,8 @@ const getters = { }, connected: true, bondDenom: `stake`, - session: { signedIn: true } + session: { signedIn: true }, + lastHeader: { height: `20` } } // TODO: remove this dirty addition: the real cleanup will be done in a separate PR diff --git a/test/unit/specs/components/staking/TableValidators.spec.js b/test/unit/specs/components/staking/TableValidators.spec.js index b6e7e194eb..dd446f90c2 100644 --- a/test/unit/specs/components/staking/TableValidators.spec.js +++ b/test/unit/specs/components/staking/TableValidators.spec.js @@ -99,102 +99,4 @@ describe(`TableValidators`, () => { TableValidators.watch.address.call({ $store, session }) expect($store.dispatch).not.toHaveBeenCalledWith(`updateDelegates`) }) - - describe(`update validators rewards every block`, () => { - it(`updates if there are existing validators`, () => { - const validators = [ - { - operator_address: `cosmosvaloper1address`, - pub_key: `cosmosvalpub1234`, - revoked: false, - tokens: `14`, - delegator_shares: `14`, - description: { - website: `www.fede.cl`, - details: `Fede's validator`, - moniker: `fedekunze`, - country: `Chile` - }, - status: 2, - bond_height: `0`, - bond_intra_tx_counter: 6, - proposer_reward_pool: null, - commission: { - rate: `0`, - max_rate: `0`, - max_change_rate: `0`, - update_time: `1970-01-01T00:00:00Z` - }, - prev_bonded_shares: `0` - } - ] - const session = { signedIn: true } - const $store = { dispatch: jest.fn() } - TableValidators.watch.validators.call({ $store, session }, validators) - expect($store.dispatch).toHaveBeenCalledWith( - `getRewardsFromAllValidators`, - validators - ) - }) - - describe(`doesn't update rewards`, () => { - it(`if user hasn't signed in`, () => { - const validators = [ - { - operator_address: `cosmosvaloper1address`, - pub_key: `cosmosvalpub1234`, - revoked: false, - tokens: `14`, - delegator_shares: `14`, - description: { - website: `www.fede.cl`, - details: `Fede's validator`, - moniker: `fedekunze`, - country: `Chile` - }, - status: 2, - bond_height: `0`, - bond_intra_tx_counter: 6, - proposer_reward_pool: null, - commission: { - rate: `0`, - max_rate: `0`, - max_change_rate: `0`, - update_time: `1970-01-01T00:00:00Z` - }, - prev_bonded_shares: `0` - } - ] - const session = { signedIn: false } - const $store = { dispatch: jest.fn() } - TableValidators.watch.validators.call({ $store, session }, validators) - expect($store.dispatch).not.toHaveBeenCalledWith( - `getRewardsFromAllValidators`, - validators - ) - }) - - it(`if validator set is empty`, () => { - const validators = [] - const session = { signedIn: true } - const $store = { dispatch: jest.fn() } - TableValidators.watch.validators.call({ $store, session }, validators) - expect($store.dispatch).not.toHaveBeenCalledWith( - `getRewardsFromAllValidators`, - validators - ) - }) - - it(`if validator set is undefined`, () => { - const validators = undefined - const session = { signedIn: true } - const $store = { dispatch: jest.fn() } - TableValidators.watch.validators.call({ $store, session }, validators) - expect($store.dispatch).not.toHaveBeenCalledWith( - `getRewardsFromAllValidators`, - validators - ) - }) - }) - }) }) diff --git a/test/unit/specs/components/wallet/PageTransactions.spec.js b/test/unit/specs/components/wallet/PageTransactions.spec.js index 59d5a9d30d..5d95db0e6a 100644 --- a/test/unit/specs/components/wallet/PageTransactions.spec.js +++ b/test/unit/specs/components/wallet/PageTransactions.spec.js @@ -316,10 +316,4 @@ describe(`PageTransactions`, () => { 1 ]) }) - - it(`updates transactions every block`, () => { - const refreshTransactions = jest.fn() - PageTransactions.watch.lastHeader.handler.call({ refreshTransactions }) - expect(refreshTransactions).toHaveBeenCalled() - }) }) diff --git a/test/unit/specs/components/wallet/PageWallet.spec.js b/test/unit/specs/components/wallet/PageWallet.spec.js index 429483a343..b7bf444b31 100644 --- a/test/unit/specs/components/wallet/PageWallet.spec.js +++ b/test/unit/specs/components/wallet/PageWallet.spec.js @@ -14,7 +14,7 @@ describe(`PageWallet`, () => { balances: lcdClientMock.state.accounts[lcdClientMock.addresses[0]].coins, externals: { config: { faucet: `yo` } }, }, - lastHeader: ``, + lastHeader: { height: `20` }, connected: true, session: { signedIn: true } } diff --git a/test/unit/specs/store/__snapshots__/delegation.spec.js.snap b/test/unit/specs/store/__snapshots__/delegation.spec.js.snap deleted file mode 100644 index 96ad63c5e2..0000000000 --- a/test/unit/specs/store/__snapshots__/delegation.spec.js.snap +++ /dev/null @@ -1,47 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Module: Delegations submits delegation transaction 1`] = ` -Array [ - Array [ - "sendTx", - Object { - "delegation": Object { - "amount": "undefined", - "denom": "STAKE", - }, - "delegator_address": "cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9", - "password": undefined, - "submitType": undefined, - "to": "cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9", - "type": "postDelegation", - "validator_address": undefined, - }, - ], - Array [ - "updateDelegates", - ], -] -`; - -exports[`Module: Delegations submits undelegation transaction 1`] = ` -Array [ - Array [ - "sendTx", - Object { - "delegation": Object { - "amount": "undefined", - "denom": "STAKE", - }, - "delegator_address": "cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9", - "password": undefined, - "submitType": undefined, - "to": "cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9", - "type": "postDelegation", - "validator_address": undefined, - }, - ], - Array [ - "updateDelegates", - ], -] -`; diff --git a/test/unit/specs/store/delegation.spec.js b/test/unit/specs/store/delegation.spec.js index f901d07359..97c527e8e4 100644 --- a/test/unit/specs/store/delegation.spec.js +++ b/test/unit/specs/store/delegation.spec.js @@ -192,7 +192,7 @@ describe(`Module: Delegations`, () => { { stakingTransactions } ) - expect(dispatch.mock.calls).toMatchSnapshot() + expect(dispatch).toHaveBeenCalledWith(`getAllTxs`) }) it(`submits undelegation transaction`, async () => { @@ -217,12 +217,12 @@ describe(`Module: Delegations`, () => { }, state, dispatch, - commit: () => {} + commit: () => { } }, { stakingTransactions } ) - expect(dispatch.mock.calls).toMatchSnapshot() + expect(dispatch).toHaveBeenCalledWith(`getAllTxs`) }) describe(`queries the delegated atoms on reconnection`, () => { @@ -339,7 +339,7 @@ describe(`Module: Delegations`, () => { getters: { liquidAtoms: 1000 }, - dispatch: () => {}, + dispatch: () => { }, commit }, { @@ -389,6 +389,7 @@ describe(`Module: Delegations`, () => { ) jest.runAllTimers() expect(dispatch).toHaveBeenCalledWith(`updateDelegates`) + expect(dispatch).toHaveBeenCalledWith(`getAllTxs`) }) it(`should store an error if failed to load delegations`, async () => { @@ -442,11 +443,13 @@ describe(`Module: Delegations`, () => { const dispatch = jest.fn(() => []) - await actions.updateDelegates({ dispatch, rootState: { - session: { - signedIn: true + await actions.updateDelegates({ + dispatch, rootState: { + session: { + signedIn: true + } } - } }) + }) expect(dispatch).toHaveBeenCalledWith(`getDelegates`) expect(dispatch).toHaveBeenCalledWith(`getBondedDelegates`, []) From b0097b4e166e8ac2b894acb736a14ad8e864b141 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 17:10:00 +0100 Subject: [PATCH 05/18] fix tests --- .../components/staking/TabMyDelegations.vue | 3 +- .../components/staking/PageValidator.spec.js | 4 +- .../staking/TabMyDelegations.spec.js | 46 ++++++++++--------- .../TabMyDelegations.spec.js.snap | 6 +-- .../components/wallet/PageWallet.spec.js | 17 ------- 5 files changed, 33 insertions(+), 43 deletions(-) diff --git a/app/src/renderer/components/staking/TabMyDelegations.vue b/app/src/renderer/components/staking/TabMyDelegations.vue index ca0ceb0bea..e691cbdcea 100644 --- a/app/src/renderer/components/staking/TabMyDelegations.vue +++ b/app/src/renderer/components/staking/TabMyDelegations.vue @@ -81,7 +81,8 @@ export default { `committedDelegations`, `bondDenom`, `connected`, - `session` + `session`, + `lastHeader` ]), yourValidators({ committedDelegations, delegates: { delegates } } = this) { return delegates.filter( diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index 592eab8ef9..7c219b306a 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -275,7 +275,9 @@ describe(`PageValidator`, () => { const $store = { dispatch: jest.fn() } const session = { signedIn: false } const $route = { params: { validator: `cosmos1address` } } - PageValidator.watch.lastHeader.handler.call({ session, $store, $route }) + const newHeader = { height: `20` } + PageValidator.watch.lastHeader.handler.call({ session, $store, $route }, + newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator diff --git a/test/unit/specs/components/staking/TabMyDelegations.spec.js b/test/unit/specs/components/staking/TabMyDelegations.spec.js index 308d0dd3bc..4159c04b09 100644 --- a/test/unit/specs/components/staking/TabMyDelegations.spec.js +++ b/test/unit/specs/components/staking/TabMyDelegations.spec.js @@ -4,26 +4,6 @@ import validators from "../../store/json/validators.js" const delegator_address = `cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9` -const getters = { - transactions: { - staking: [] - }, - delegates: { - delegates: validators - }, - delegation: { - unbondingDelegations: { - }, - loaded: true - }, - committedDelegations: { - }, - connected: true, - bondDenom: `stake`, - session: { signedIn: true }, - lastHeader: { height: `20` } -} - // TODO: remove this dirty addition: the real cleanup will be done in a separate PR // the problem is mock VS real implementation have different keys: shares in mock, shares_amount in SDK // const unbondingTransactions = lcdClientMock.state.txs.slice(5).map(t => { @@ -32,6 +12,27 @@ const getters = { // }) describe(`Component: TabMyDelegations`, () => { + + const getters = { + transactions: { + staking: [] + }, + delegates: { + delegates: validators + }, + delegation: { + unbondingDelegations: { + }, + loaded: true + }, + committedDelegations: { + }, + connected: true, + bondDenom: `uatom`, + session: { signedIn: true }, + lastHeader: { height: `20` } + } + describe(`view`, () => { let wrapper, $store @@ -44,7 +45,10 @@ describe(`Component: TabMyDelegations`, () => { wrapper = shallowMount(TabMyDelegations, { mocks: { - $store + $store, + $route: { + path: `/staking/my-delegations` + } }, stubs: [`router-link`] }) diff --git a/test/unit/specs/components/staking/__snapshots__/TabMyDelegations.spec.js.snap b/test/unit/specs/components/staking/__snapshots__/TabMyDelegations.spec.js.snap index 17e66c9a88..fed36bceef 100644 --- a/test/unit/specs/components/staking/__snapshots__/TabMyDelegations.spec.js.snap +++ b/test/unit/specs/components/staking/__snapshots__/TabMyDelegations.spec.js.snap @@ -13,7 +13,7 @@ exports[`Component: TabMyDelegations view should show a message if not staked ye
- Looks like you haven't delegated any stakes yet. Head over to + Looks like you haven't delegated any uatoms yet. Head over to the - Looks like you haven't delegated any stakes yet. Head over to + Looks like you haven't delegated any uatoms yet. Head over to the { }) expect(wrapper.exists(`tm-data-loading`)).toBe(true) }) - - describe(`updates balances every block`, () => { - it(`should not update if the user hasn't signed in`, () => { - const queryWalletBalances = jest.fn() - const session = { signedIn: false } - PageWallet.watch.lastHeader.handler.call({ session, queryWalletBalances }) - expect(queryWalletBalances).not.toHaveBeenCalled() - }) - - it(`should update if the user has signed in`, () => { - const queryWalletBalances = jest.fn() - const session = { signedIn: true } - PageWallet.watch.lastHeader.handler.call({ session, queryWalletBalances }) - expect(queryWalletBalances).toHaveBeenCalled() - }) - }) - }) From 32c64dfadbe907c22c96b82573eb1e0dc8ecd34c Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 18:20:36 +0100 Subject: [PATCH 06/18] request on tabvalidators --- .../components/staking/TabMyDelegations.vue | 9 ++--- .../components/staking/TabValidators.vue | 34 ++++++++++++++++++- .../components/staking/TabValidators.spec.js | 34 +++++++++++++------ 3 files changed, 59 insertions(+), 18 deletions(-) diff --git a/app/src/renderer/components/staking/TabMyDelegations.vue b/app/src/renderer/components/staking/TabMyDelegations.vue index e691cbdcea..c468231cb5 100644 --- a/app/src/renderer/components/staking/TabMyDelegations.vue +++ b/app/src/renderer/components/staking/TabMyDelegations.vue @@ -120,13 +120,8 @@ export default { lastHeader: { immediate: true, handler(newHeader) { - const waitTenBlocks = Number(newHeader.height) % 10 === 0 - if ( - waitTenBlocks && - this.session.signedIn && - this.$route.path.includes(`/staking/`) && - this.yourValidators - ) { + const waitTenBlocks = Number(newHeader.height) % 20 === 0 + if (waitTenBlocks && this.session.signedIn && this.yourValidators) { this.$store.dispatch( `getRewardsFromAllValidators`, this.yourValidators diff --git a/app/src/renderer/components/staking/TabValidators.vue b/app/src/renderer/components/staking/TabValidators.vue index 6f415b4678..08355416be 100644 --- a/app/src/renderer/components/staking/TabValidators.vue +++ b/app/src/renderer/components/staking/TabValidators.vue @@ -25,11 +25,43 @@ export default { TmDataConnecting }, computed: { - ...mapGetters([`delegates`, `connected`, `session`]) + ...mapGetters([ + `lastHeader`, + `delegates`, + `committedDelegations`, + `connected`, + `session` + ]), + yourValidators( + { + committedDelegations, + delegates: { delegates }, + session: { signedIn } + } = this + ) { + return ( + signedIn && + delegates.filter( + ({ operator_address }) => operator_address in committedDelegations + ) + ) + } }, watch: { "session.signedIn": function(signedIn) { signedIn && this.$store.dispatch(`updateDelegates`) + }, + lastHeader: { + immediate: true, + handler(newHeader) { + const waitTenBlocks = Number(newHeader.height) % 20 === 0 + if (waitTenBlocks && this.session.signedIn && this.yourValidators) { + this.$store.dispatch( + `getRewardsFromAllValidators`, + this.yourValidators + ) + } + } } }, mounted() { diff --git a/test/unit/specs/components/staking/TabValidators.spec.js b/test/unit/specs/components/staking/TabValidators.spec.js index 82590725b2..797c2f5946 100644 --- a/test/unit/specs/components/staking/TabValidators.spec.js +++ b/test/unit/specs/components/staking/TabValidators.spec.js @@ -1,22 +1,24 @@ import { shallowMount } from "@vue/test-utils" import TabValidators from "renderer/components/staking/TabValidators" - -// we don't show data of a validator in this component so we just provide stubs here -const delegates = new Array(3).fill({}) +import validators from "../../store/json/validators.js" describe(`TabValidators`, () => { let wrapper, $store const getters = { delegates: { - delegates, + delegates: validators, loading: false, loaded: true }, + committedDelegations: { + [validators[0].operator_address]: 42 + }, session: { signedIn: true }, - connected: true + connected: true, + lastHeader: { height: 20 } } beforeEach(async () => { @@ -41,14 +43,18 @@ describe(`TabValidators`, () => { dispatch: jest.fn(), getters: { delegates: { - delegates, + delegates: validators, loading: false, loaded: false }, + committedDelegations: { + [validators[0].operator_address]: 42 + }, session: { signedIn: true }, - connected: false + connected: false, + lastHeader: { height: 20 } } } @@ -66,14 +72,18 @@ describe(`TabValidators`, () => { dispatch: jest.fn(), getters: { delegates: { - delegates, + delegates: validators, loading: true, loaded: false }, + committedDelegations: { + [validators[0].operator_address]: 42 + }, session: { signedIn: true }, - connected: true + connected: true, + lastHeader: { height: 20 } } } @@ -95,10 +105,14 @@ describe(`TabValidators`, () => { loading: false, loaded: true }, + committedDelegations: { + [validators[0].operator_address]: 42 + }, session: { signedIn: true }, - connected: true + connected: true, + lastHeader: { height: 20 } } } From fbfd47f238c50ee292d79c893e6cb1f2146d8307 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 18:32:09 +0100 Subject: [PATCH 07/18] minor refactor --- .../components/staking/PageValidator.vue | 5 +++-- .../components/staking/TabMyDelegations.vue | 17 +++++++++++++---- .../components/staking/TabValidators.vue | 5 ++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/renderer/components/staking/PageValidator.vue b/app/src/renderer/components/staking/PageValidator.vue index 39b6353529..f7a1d535ff 100644 --- a/app/src/renderer/components/staking/PageValidator.vue +++ b/app/src/renderer/components/staking/PageValidator.vue @@ -323,7 +323,8 @@ export default { if ( this.session.signedIn && waitTwentyBlocks && - this.$route.name === `validator` + this.$route.name === `validator` && + this.myDelegation !== `--` ) { this.$store.dispatch( `getRewardsFromValidator`, @@ -334,7 +335,7 @@ export default { } }, mounted() { - if (this.session.signedIn) { + if (this.session.signedIn && this.myDelegation !== `--`) { this.$store.dispatch( `getRewardsFromValidator`, this.$route.params.validator diff --git a/app/src/renderer/components/staking/TabMyDelegations.vue b/app/src/renderer/components/staking/TabMyDelegations.vue index c468231cb5..0c0445264a 100644 --- a/app/src/renderer/components/staking/TabMyDelegations.vue +++ b/app/src/renderer/components/staking/TabMyDelegations.vue @@ -84,9 +84,18 @@ export default { `session`, `lastHeader` ]), - yourValidators({ committedDelegations, delegates: { delegates } } = this) { - return delegates.filter( - ({ operator_address }) => operator_address in committedDelegations + yourValidators( + { + committedDelegations, + delegates: { delegates }, + session: { signedIn } + } = this + ) { + return ( + signedIn && + delegates.filter( + ({ operator_address }) => operator_address in committedDelegations + ) ) }, unbondingTransactions: ({ transactions, delegation } = this) => @@ -121,7 +130,7 @@ export default { immediate: true, handler(newHeader) { const waitTenBlocks = Number(newHeader.height) % 20 === 0 - if (waitTenBlocks && this.session.signedIn && this.yourValidators) { + if (waitTenBlocks && this.yourValidators) { this.$store.dispatch( `getRewardsFromAllValidators`, this.yourValidators diff --git a/app/src/renderer/components/staking/TabValidators.vue b/app/src/renderer/components/staking/TabValidators.vue index 08355416be..8a36a2aba2 100644 --- a/app/src/renderer/components/staking/TabValidators.vue +++ b/app/src/renderer/components/staking/TabValidators.vue @@ -55,7 +55,7 @@ export default { immediate: true, handler(newHeader) { const waitTenBlocks = Number(newHeader.height) % 20 === 0 - if (waitTenBlocks && this.session.signedIn && this.yourValidators) { + if (waitTenBlocks && this.yourValidators) { this.$store.dispatch( `getRewardsFromAllValidators`, this.yourValidators @@ -66,6 +66,9 @@ export default { }, mounted() { this.$store.dispatch(`updateDelegates`) + if (this.yourValidators) { + this.$store.dispatch(`getRewardsFromAllValidators`, this.yourValidators) + } } } From 298f8419ca3faa4ae7a3ef7687b18e4f13b7c360 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 15 Mar 2019 18:36:12 +0100 Subject: [PATCH 08/18] tests --- .../staking/TabMyDelegations.spec.js | 37 +++++++++++++------ .../components/staking/TabValidators.spec.js | 28 ++++++++++++++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/test/unit/specs/components/staking/TabMyDelegations.spec.js b/test/unit/specs/components/staking/TabMyDelegations.spec.js index 4159c04b09..79e21e5836 100644 --- a/test/unit/specs/components/staking/TabMyDelegations.spec.js +++ b/test/unit/specs/components/staking/TabMyDelegations.spec.js @@ -173,17 +173,32 @@ describe(`Component: TabMyDelegations`, () => { ).toHaveLength(1) }) - it(`yourValidators`, () => { - expect( - TabMyDelegations.computed.yourValidators({ - committedDelegations: { - [validators[0].operator_address]: 1, - [validators[2].operator_address]: 2 - }, - delegates: { delegates: validators } - }) - ).toEqual([validators[0], validators[2]]) + describe(`yourValidators`, () => { + it(`should return validators if signed in`, () => { + expect( + TabMyDelegations.computed.yourValidators({ + committedDelegations: { + [validators[0].operator_address]: 1, + [validators[2].operator_address]: 2 + }, + delegates: { delegates: validators }, + session: { signedIn: true } + }) + ).toEqual([validators[0], validators[2]]) + }) + + it(`should return false if not signed in`, () => { + expect( + TabMyDelegations.computed.yourValidators({ + committedDelegations: { + [validators[0].operator_address]: 1, + [validators[2].operator_address]: 2 + }, + delegates: { delegates: validators }, + session: { signedIn: false } + }) + ).toBe(false) + }) }) }) - }) diff --git a/test/unit/specs/components/staking/TabValidators.spec.js b/test/unit/specs/components/staking/TabValidators.spec.js index 797c2f5946..16b74fed97 100644 --- a/test/unit/specs/components/staking/TabValidators.spec.js +++ b/test/unit/specs/components/staking/TabValidators.spec.js @@ -144,4 +144,32 @@ describe(`TabValidators`, () => { }, true) expect(dispatch).toHaveBeenCalledWith(`updateDelegates`) }) + + describe(`yourValidators`, () => { + it(`should return validators if signed in`, () => { + expect( + TabValidators.computed.yourValidators({ + committedDelegations: { + [validators[0].operator_address]: 1, + [validators[2].operator_address]: 2 + }, + delegates: { delegates: validators }, + session: { signedIn: true } + }) + ).toEqual([validators[0], validators[2]]) + }) + + it(`should return false if not signed in`, () => { + expect( + TabValidators.computed.yourValidators({ + committedDelegations: { + [validators[0].operator_address]: 1, + [validators[2].operator_address]: 2 + }, + delegates: { delegates: validators }, + session: { signedIn: false } + }) + ).toBe(false) + }) + }) }) From bef3f8ef1916914de0225cc582caf5fb160f2c5b Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Sun, 17 Mar 2019 13:57:55 +0100 Subject: [PATCH 09/18] page validator tests --- .../components/staking/TableValidators.vue | 3 +- .../components/staking/PageValidator.spec.js | 105 +++++++++++++++++- 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/app/src/renderer/components/staking/TableValidators.vue b/app/src/renderer/components/staking/TableValidators.vue index e5ca172c48..a7f9d73c28 100644 --- a/app/src/renderer/components/staking/TableValidators.vue +++ b/app/src/renderer/components/staking/TableValidators.vue @@ -51,8 +51,7 @@ export default { `liquidAtoms`, `connected`, `bondDenom`, - `keybase`, - `lastHeader` + `keybase` ]), vpTotal() { return this.validators diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index 7c219b306a..00ea2b67ed 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -260,7 +260,7 @@ describe(`PageValidator`, () => { }) }) - it(`doesn't call user rewards if not signed in`, () => { + it(`shouldn't call user rewards if not signed in`, () => { const session = { signedIn: false } const $store = { dispatch: jest.fn() } const $route = { params: { validator: `cosmos1address` } } @@ -271,18 +271,111 @@ describe(`PageValidator`, () => { ) }) - it(`updates rewards if block hasn't updated`, () => { - const $store = { dispatch: jest.fn() } + it(`shouldn't call user rewards there're no delegations`, () => { const session = { signedIn: false } + const $store = { dispatch: jest.fn() } + const myDelegation = `--` const $route = { params: { validator: `cosmos1address` } } - const newHeader = { height: `20` } - PageValidator.watch.lastHeader.handler.call({ session, $store, $route }, - newHeader) + PageValidator.mounted.call({ session, $store, $route, myDelegation }) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator ) }) + + describe(`update rewards on new blocks`, () => { + describe(`shouldn't update`, () => { + + it(`if user is not signed in `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: false } + const $route = { + params: { validator: `cosmos1address` }, + name: `validator` + } + const myDelegation = `1 atom` + const newHeader = { height: `20` } + PageValidator.watch.lastHeader.handler.call( + { session, $store, $route, myDelegation }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromValidator`, + $route.params.validator + ) + }) + it(`if hasn't waited for 20 blocks `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const $route = { + params: { validator: `cosmos1address` }, + name: `validator` + } + const myDelegation = `1 atom` + const newHeader = { height: `30` } + PageValidator.watch.lastHeader.handler.call( + { session, $store, $route, myDelegation }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromValidator`, + $route.params.validator + ) + }) + + it(`if user is not watching page validator`, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const $route = { + params: { validator: `cosmos1address` }, + name: `my-delegations` + } + const myDelegation = `1 atom` + const newHeader = { height: `20` } + PageValidator.watch.lastHeader.handler.call( + { session, $store, $route, myDelegation }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromValidator`, + $route.params.validator + ) + }) + + it(`if user doesn't have any delegations `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const $route = { + params: { validator: `cosmos1address` }, + name: `validator` + } + const myDelegation = `--` + const newHeader = { height: `30` } + PageValidator.watch.lastHeader.handler.call( + { session, $store, $route, myDelegation }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromValidator`, + $route.params.validator + ) + }) + }) + + it(`should update rewards if waited for 20 blocks`, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const $route = { + params: { validator: `cosmos1address` }, + name: `validator` + } + const myDelegation = `1 atom` + const newHeader = { height: `20` } + PageValidator.watch.lastHeader.handler.call( + { session, $store, $route, myDelegation }, + newHeader) + expect($store.dispatch).toHaveBeenCalledWith( + `getRewardsFromValidator`, + $route.params.validator + ) + }) + }) }) describe(`delegationTargetOptions`, () => { From 0455579e7038e94057b9058bfdd74e03b3ca1676 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Sun, 17 Mar 2019 16:13:57 +0100 Subject: [PATCH 10/18] tests --- .../components/staking/TabMyDelegations.vue | 6 ++- .../components/staking/TabValidators.vue | 6 ++- .../specs/components/common/TmBalance.spec.js | 39 ++++++++++++++++ .../staking/ModalWithdrawAllRewards.spec.js | 38 ++++++++++++++++ .../components/staking/PageValidator.spec.js | 40 ++++++++++------- .../staking/TabMyDelegations.spec.js | 45 +++++++++++++++++++ .../components/staking/TabValidators.spec.js | 45 +++++++++++++++++++ 7 files changed, 200 insertions(+), 19 deletions(-) diff --git a/app/src/renderer/components/staking/TabMyDelegations.vue b/app/src/renderer/components/staking/TabMyDelegations.vue index 0c0445264a..4635d790ec 100644 --- a/app/src/renderer/components/staking/TabMyDelegations.vue +++ b/app/src/renderer/components/staking/TabMyDelegations.vue @@ -130,7 +130,11 @@ export default { immediate: true, handler(newHeader) { const waitTenBlocks = Number(newHeader.height) % 20 === 0 - if (waitTenBlocks && this.yourValidators) { + if ( + waitTenBlocks && + this.yourValidators && + this.yourValidators.length > 0 + ) { this.$store.dispatch( `getRewardsFromAllValidators`, this.yourValidators diff --git a/app/src/renderer/components/staking/TabValidators.vue b/app/src/renderer/components/staking/TabValidators.vue index 8a36a2aba2..df851eb4e8 100644 --- a/app/src/renderer/components/staking/TabValidators.vue +++ b/app/src/renderer/components/staking/TabValidators.vue @@ -55,7 +55,11 @@ export default { immediate: true, handler(newHeader) { const waitTenBlocks = Number(newHeader.height) % 20 === 0 - if (waitTenBlocks && this.yourValidators) { + if ( + waitTenBlocks && + this.yourValidators && + this.yourValidators.length > 0 + ) { this.$store.dispatch( `getRewardsFromAllValidators`, this.yourValidators diff --git a/test/unit/specs/components/common/TmBalance.spec.js b/test/unit/specs/components/common/TmBalance.spec.js index c3a0d31e7f..3b7ceb2dad 100644 --- a/test/unit/specs/components/common/TmBalance.spec.js +++ b/test/unit/specs/components/common/TmBalance.spec.js @@ -53,4 +53,43 @@ describe(`TmBalance`, () => { TmBalance.methods.onWithdrawal.call({ $refs }) expect($refs.modalWithdrawAllRewards.open).toHaveBeenCalled() }) + + describe(`update balance and total rewards on new blocks`, () => { + describe(`shouldn't update`, () => { + it(`if user is not signed in `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: false } + const newHeader = { height: `10` } + TmBalance.watch.lastHeader.handler.call( + { session, $store }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith(`getTotalRewards`) + expect($store.dispatch).not.toHaveBeenCalledWith(`queryWalletBalances`) + }) + + it(`if hasn't waited for 10 blocks `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const newHeader = { height: `12` } + TmBalance.watch.lastHeader.handler.call( + { session, $store }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith(`getTotalRewards`) + expect($store.dispatch).not.toHaveBeenCalledWith(`queryWalletBalances`) + }) + }) + + describe(`should update balance and rewards `, () => { + it(`if user is signed in and wait for 10 blocks`, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const newHeader = { height: `10` } + TmBalance.watch.lastHeader.handler.call( + { session, $store }, + newHeader) + expect($store.dispatch).toHaveBeenCalledWith(`getTotalRewards`) + expect($store.dispatch).toHaveBeenCalledWith(`queryWalletBalances`) + }) + }) + }) }) diff --git a/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js b/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js index dec6706d61..bd203d3efd 100644 --- a/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js +++ b/test/unit/specs/components/staking/ModalWithdrawAllRewards.spec.js @@ -60,4 +60,42 @@ describe(`ModalWithdrawAllRewards`, () => { ) }) }) + + describe(`update total rewards on new blocks`, () => { + describe(`shouldn't update total `, () => { + it(`if user is not signed in `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: false } + const $refs = { actionModal: { show: true } } + ModalWithdrawAllRewards.watch.lastHeader.handler.call( + { session, $store, $refs }) + expect($store.dispatch).not.toHaveBeenCalledWith(`getTotalRewards`) + }) + + it(`if user is not on watching the modal `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + let $refs = {} + ModalWithdrawAllRewards.watch.lastHeader.handler.call( + { session, $store, $refs }) + expect($store.dispatch).not.toHaveBeenCalledWith(`getTotalRewards`) + + $refs = { actionModal: { show: false } } + ModalWithdrawAllRewards.watch.lastHeader.handler.call( + { session, $store, $refs }) + expect($store.dispatch).not.toHaveBeenCalledWith(`getTotalRewards`) + }) + }) + + describe(`should update total rewards `, () => { + it(`if user is signed in and is watching the modal `, () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const $refs = { actionModal: { show: true } } + ModalWithdrawAllRewards.watch.lastHeader.handler.call( + { session, $store, $refs }) + expect($store.dispatch).toHaveBeenCalledWith(`getTotalRewards`) + }) + }) + }) }) diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index 00ea2b67ed..bb3d0b893c 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -285,7 +285,6 @@ describe(`PageValidator`, () => { describe(`update rewards on new blocks`, () => { describe(`shouldn't update`, () => { - it(`if user is not signed in `, () => { const $store = { dispatch: jest.fn() } const session = { signedIn: false } @@ -303,6 +302,7 @@ describe(`PageValidator`, () => { $route.params.validator ) }) + it(`if hasn't waited for 20 blocks `, () => { const $store = { dispatch: jest.fn() } const session = { signedIn: true } @@ -358,22 +358,28 @@ describe(`PageValidator`, () => { }) }) - it(`should update rewards if waited for 20 blocks`, () => { - const $store = { dispatch: jest.fn() } - const session = { signedIn: true } - const $route = { - params: { validator: `cosmos1address` }, - name: `validator` - } - const myDelegation = `1 atom` - const newHeader = { height: `20` } - PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myDelegation }, - newHeader) - expect($store.dispatch).toHaveBeenCalledWith( - `getRewardsFromValidator`, - $route.params.validator - ) + describe(`should update rewards `, () => { + it( + `if waited for 20 blocks, ` + + `user has signed in, ` + + `has delegations and is watching the validator page`, + () => { + const $store = { dispatch: jest.fn() } + const session = { signedIn: true } + const $route = { + params: { validator: `cosmos1address` }, + name: `validator` + } + const myDelegation = `1 atom` + const newHeader = { height: `20` } + PageValidator.watch.lastHeader.handler.call( + { session, $store, $route, myDelegation }, + newHeader) + expect($store.dispatch).toHaveBeenCalledWith( + `getRewardsFromValidator`, + $route.params.validator + ) + }) }) }) }) diff --git a/test/unit/specs/components/staking/TabMyDelegations.spec.js b/test/unit/specs/components/staking/TabMyDelegations.spec.js index 79e21e5836..6ba331df7c 100644 --- a/test/unit/specs/components/staking/TabMyDelegations.spec.js +++ b/test/unit/specs/components/staking/TabMyDelegations.spec.js @@ -200,5 +200,50 @@ describe(`Component: TabMyDelegations`, () => { ).toBe(false) }) }) + + describe(`update rewards on new blocks`, () => { + describe(`shouldn't update`, () => { + it(`if hasn't waited for 20 blocks `, () => { + const $store = { dispatch: jest.fn() } + const yourValidators = [{}] + const newHeader = { height: `30` } + TabMyDelegations.watch.lastHeader.handler.call( + { $store, yourValidators }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromAllValidators`, + yourValidators + ) + }) + + it(`if user doesn't have any delegations `, () => { + const $store = { dispatch: jest.fn() } + const yourValidators = [] + const newHeader = { height: `40` } + TabMyDelegations.watch.lastHeader.handler.call( + { $store, yourValidators }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromAllValidators`, + yourValidators + ) + }) + + describe(`should update rewards `, () => { + it(`if has waited for 20 blocks and has delegations`, () => { + const $store = { dispatch: jest.fn() } + const yourValidators = [{}] + const newHeader = { height: `40` } + TabMyDelegations.watch.lastHeader.handler.call( + { $store, yourValidators }, + newHeader) + expect($store.dispatch).toHaveBeenCalledWith( + `getRewardsFromAllValidators`, + yourValidators + ) + }) + }) + }) + }) }) }) diff --git a/test/unit/specs/components/staking/TabValidators.spec.js b/test/unit/specs/components/staking/TabValidators.spec.js index 16b74fed97..7cc6fa6849 100644 --- a/test/unit/specs/components/staking/TabValidators.spec.js +++ b/test/unit/specs/components/staking/TabValidators.spec.js @@ -172,4 +172,49 @@ describe(`TabValidators`, () => { ).toBe(false) }) }) + + describe(`update rewards on new blocks`, () => { + describe(`shouldn't update`, () => { + it(`if hasn't waited for 20 blocks `, () => { + const $store = { dispatch: jest.fn() } + const yourValidators = [{}] + const newHeader = { height: `30` } + TabValidators.watch.lastHeader.handler.call( + { $store, yourValidators }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromAllValidators`, + yourValidators + ) + }) + + it(`if user doesn't have any delegations `, () => { + const $store = { dispatch: jest.fn() } + const yourValidators = [] + const newHeader = { height: `40` } + TabValidators.watch.lastHeader.handler.call( + { $store, yourValidators }, + newHeader) + expect($store.dispatch).not.toHaveBeenCalledWith( + `getRewardsFromAllValidators`, + yourValidators + ) + }) + + describe(`should update rewards `, () => { + it(`if has waited for 20 blocks and has delegations`, () => { + const $store = { dispatch: jest.fn() } + const yourValidators = [{}] + const newHeader = { height: `40` } + TabValidators.watch.lastHeader.handler.call( + { $store, yourValidators }, + newHeader) + expect($store.dispatch).toHaveBeenCalledWith( + `getRewardsFromAllValidators`, + yourValidators + ) + }) + }) + }) + }) }) From b681c8e38d7a208c38e3d599887efd47e7319445 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 18 Mar 2019 18:44:46 +0100 Subject: [PATCH 11/18] coverage --- .../components/staking/PageValidator.spec.js | 2 +- test/unit/specs/store/delegation.spec.js | 60 ++++++++++++++----- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index bb3d0b893c..b41d23d2c2 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -272,7 +272,7 @@ describe(`PageValidator`, () => { }) it(`shouldn't call user rewards there're no delegations`, () => { - const session = { signedIn: false } + const session = { signedIn: true } const $store = { dispatch: jest.fn() } const myDelegation = `--` const $route = { params: { validator: `cosmos1address` } } diff --git a/test/unit/specs/store/delegation.spec.js b/test/unit/specs/store/delegation.spec.js index 97c527e8e4..a634d14163 100644 --- a/test/unit/specs/store/delegation.spec.js +++ b/test/unit/specs/store/delegation.spec.js @@ -196,32 +196,60 @@ describe(`Module: Delegations`, () => { }) it(`submits undelegation transaction`, async () => { - const stakingTransactions = {} - stakingTransactions.delegations = [ + const validator = lcdClientMock.state.candidates[0] + const amount = 10 + const password = `` + const submitType = `ledger` + const dispatch = jest.fn() + + await actions.submitUnbondingDelegation( { - validator: lcdClientMock.state.candidates[0], - atoms: -113 + rootState: mockRootState, + state, + dispatch }, - { - validator: lcdClientMock.state.candidates[1], - atoms: -356 - } - ] + { validator, amount, password, submitType } + ) + + expect(dispatch).toHaveBeenCalledWith(`sendTx`, { + type: `postUnbondingDelegation`, + to: `cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9`, + delegator_address: `cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9`, + validator_address: validator.operator_address, + shares: `10.0000000000`, + password, + submitType + }) + expect(dispatch).toHaveBeenCalledWith(`getAllTxs`) + }) + + it(`submits redelegation transaction`, async () => { + const validatorSrc = lcdClientMock.state.candidates[0] + const validatorDst = lcdClientMock.state.candidates[1] + const amount = 10 + const password = `` + const submitType = `ledger` const dispatch = jest.fn() - await actions.submitDelegation( + await actions.submitRedelegation( { rootState: mockRootState, - getters: { - liquidAtoms: 1000 - }, state, - dispatch, - commit: () => { } + dispatch }, - { stakingTransactions } + { validatorSrc, validatorDst, amount, password, submitType } ) + expect(dispatch).toHaveBeenCalledWith(`sendTx`, { + type: `postRedelegation`, + to: `cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9`, + delegator_address: `cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9`, + validator_src_address: validatorSrc.operator_address, + validator_dst_address: validatorDst.operator_address, + shares: `10.0000000000`, + password, + submitType + }) expect(dispatch).toHaveBeenCalledWith(`getAllTxs`) }) From 5fb87c6d0a22fef90ccfc189fefd6d4d807b9dd4 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 18 Mar 2019 18:46:08 +0100 Subject: [PATCH 12/18] pending --- PENDING.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PENDING.md b/PENDING.md index be9194d575..3668a2f4b1 100644 --- a/PENDING.md +++ b/PENDING.md @@ -1,4 +1,3 @@ ### Fixed -- [\#2257](https://github.com/cosmos/voyager/pull/2257) fixed console -- [\#2273](https://github.com/cosmos/voyager/issues/2273) fixed atoms and uatoms conversion @fedekunze \ No newline at end of file +- [\#2289](https://github.com/cosmos/voyager/pull/2289) reduced amount of requests to full node @fedekunze \ No newline at end of file From e7da93538ae62b872fa3d343fd67e9d1a33b9ce9 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 18 Mar 2019 19:08:52 +0100 Subject: [PATCH 13/18] cov --- .../specs/components/staking/PageValidator.spec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index b41d23d2c2..18f0e3bf5b 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -260,6 +260,18 @@ describe(`PageValidator`, () => { }) }) + it(`should call user rewards on mount`, () => { + const session = { signedIn: true } + const $store = { dispatch: jest.fn() } + const myDelegation = 1 + const $route = { params: { validator: `cosmos1address` } } + PageValidator.mounted.call({ session, $store, $route, myDelegation }) + expect($store.dispatch).toHaveBeenCalledWith( + `getRewardsFromValidator`, + $route.params.validator + ) + }) + it(`shouldn't call user rewards if not signed in`, () => { const session = { signedIn: false } const $store = { dispatch: jest.fn() } From 9b8489e2f60862d38a97c3bcbded7d0b6bceabff Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 18 Mar 2019 19:58:14 +0100 Subject: [PATCH 14/18] comments from review --- .../components/staking/PageValidator.vue | 4 +-- .../components/staking/TabMyDelegations.vue | 4 +-- .../components/staking/TabValidators.vue | 4 +-- .../components/staking/PageValidator.spec.js | 28 +++++++++---------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/src/renderer/components/staking/PageValidator.vue b/app/src/renderer/components/staking/PageValidator.vue index 42abebf9d3..c2bafda6c3 100644 --- a/app/src/renderer/components/staking/PageValidator.vue +++ b/app/src/renderer/components/staking/PageValidator.vue @@ -321,7 +321,7 @@ export default { this.session.signedIn && waitTwentyBlocks && this.$route.name === `validator` && - this.myDelegation !== `--` + Number(this.myBond) > 0 ) { this.$store.dispatch( `getRewardsFromValidator`, @@ -332,7 +332,7 @@ export default { } }, mounted() { - if (this.session.signedIn && this.myDelegation !== `--`) { + if (this.session.signedIn && Number(this.myBond) > 0) { this.$store.dispatch( `getRewardsFromValidator`, this.$route.params.validator diff --git a/app/src/renderer/components/staking/TabMyDelegations.vue b/app/src/renderer/components/staking/TabMyDelegations.vue index 7f2c215b9b..f24ab12cd7 100644 --- a/app/src/renderer/components/staking/TabMyDelegations.vue +++ b/app/src/renderer/components/staking/TabMyDelegations.vue @@ -131,9 +131,9 @@ export default { lastHeader: { immediate: true, handler(newHeader) { - const waitTenBlocks = Number(newHeader.height) % 20 === 0 + const waitTwentyBlocks = Number(newHeader.height) % 20 === 0 if ( - waitTenBlocks && + waitTwentyBlocks && this.yourValidators && this.yourValidators.length > 0 ) { diff --git a/app/src/renderer/components/staking/TabValidators.vue b/app/src/renderer/components/staking/TabValidators.vue index df851eb4e8..5df836c5d3 100644 --- a/app/src/renderer/components/staking/TabValidators.vue +++ b/app/src/renderer/components/staking/TabValidators.vue @@ -54,9 +54,9 @@ export default { lastHeader: { immediate: true, handler(newHeader) { - const waitTenBlocks = Number(newHeader.height) % 20 === 0 + const waitTwentyBlocks = Number(newHeader.height) % 20 === 0 if ( - waitTenBlocks && + waitTwentyBlocks && this.yourValidators && this.yourValidators.length > 0 ) { diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index 18f0e3bf5b..0a6d62105e 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -263,9 +263,9 @@ describe(`PageValidator`, () => { it(`should call user rewards on mount`, () => { const session = { signedIn: true } const $store = { dispatch: jest.fn() } - const myDelegation = 1 + const myBond = 1 const $route = { params: { validator: `cosmos1address` } } - PageValidator.mounted.call({ session, $store, $route, myDelegation }) + PageValidator.mounted.call({ session, $store, $route, myBond }) expect($store.dispatch).toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator @@ -286,9 +286,9 @@ describe(`PageValidator`, () => { it(`shouldn't call user rewards there're no delegations`, () => { const session = { signedIn: true } const $store = { dispatch: jest.fn() } - const myDelegation = `--` + const myBond = 0 const $route = { params: { validator: `cosmos1address` } } - PageValidator.mounted.call({ session, $store, $route, myDelegation }) + PageValidator.mounted.call({ session, $store, $route, myBond }) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator @@ -304,10 +304,10 @@ describe(`PageValidator`, () => { params: { validator: `cosmos1address` }, name: `validator` } - const myDelegation = `1 atom` + const myBond = 1 const newHeader = { height: `20` } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myDelegation }, + { session, $store, $route, myBond }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -322,10 +322,10 @@ describe(`PageValidator`, () => { params: { validator: `cosmos1address` }, name: `validator` } - const myDelegation = `1 atom` + const myBond = 1 const newHeader = { height: `30` } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myDelegation }, + { session, $store, $route, myBond }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -340,10 +340,10 @@ describe(`PageValidator`, () => { params: { validator: `cosmos1address` }, name: `my-delegations` } - const myDelegation = `1 atom` + const myBond = 1 const newHeader = { height: `20` } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myDelegation }, + { session, $store, $route, myBond }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -358,10 +358,10 @@ describe(`PageValidator`, () => { params: { validator: `cosmos1address` }, name: `validator` } - const myDelegation = `--` + const myBond = 0 const newHeader = { height: `30` } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myDelegation }, + { session, $store, $route, myBond }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -382,10 +382,10 @@ describe(`PageValidator`, () => { params: { validator: `cosmos1address` }, name: `validator` } - const myDelegation = `1 atom` + const myBond = 1 const newHeader = { height: `20` } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myDelegation }, + { session, $store, $route, myBond }, newHeader) expect($store.dispatch).toHaveBeenCalledWith( `getRewardsFromValidator`, From 188c4b4166a13f3ff505cbeaa40124989bd738dc Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 19 Mar 2019 12:26:48 +0100 Subject: [PATCH 15/18] ensure delegations are loaded --- .../components/staking/PageValidator.vue | 8 ++++++- .../components/staking/PageValidator.spec.js | 23 ++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/src/renderer/components/staking/PageValidator.vue b/app/src/renderer/components/staking/PageValidator.vue index c2bafda6c3..19cfb3353f 100644 --- a/app/src/renderer/components/staking/PageValidator.vue +++ b/app/src/renderer/components/staking/PageValidator.vue @@ -206,6 +206,7 @@ export default { `lastHeader`, `bondDenom`, `delegates`, + `delegation`, `distribution`, `committedDelegations`, `keybase`, @@ -321,6 +322,7 @@ export default { this.session.signedIn && waitTwentyBlocks && this.$route.name === `validator` && + this.delegation.loaded && Number(this.myBond) > 0 ) { this.$store.dispatch( @@ -332,7 +334,11 @@ export default { } }, mounted() { - if (this.session.signedIn && Number(this.myBond) > 0) { + if ( + this.session.signedIn && + this.delegation.loaded && + Number(this.myBond) > 0 + ) { this.$store.dispatch( `getRewardsFromValidator`, this.$route.params.validator diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index 0a6d62105e..609d6a0fa9 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -51,7 +51,7 @@ const getters = { globalPower: 4200, loaded: true }, - + delegation: { loaded: true }, committedDelegations: { [validator.operator_address]: 0 }, @@ -265,7 +265,8 @@ describe(`PageValidator`, () => { const $store = { dispatch: jest.fn() } const myBond = 1 const $route = { params: { validator: `cosmos1address` } } - PageValidator.mounted.call({ session, $store, $route, myBond }) + const delegation = { loaded: true } + PageValidator.mounted.call({ session, $store, $route, myBond, delegation }) expect($store.dispatch).toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator @@ -288,7 +289,8 @@ describe(`PageValidator`, () => { const $store = { dispatch: jest.fn() } const myBond = 0 const $route = { params: { validator: `cosmos1address` } } - PageValidator.mounted.call({ session, $store, $route, myBond }) + const delegation = { loaded: true } + PageValidator.mounted.call({ session, $store, $route, myBond, delegation }) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator @@ -306,8 +308,9 @@ describe(`PageValidator`, () => { } const myBond = 1 const newHeader = { height: `20` } + const delegation = { loaded: true } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myBond }, + { session, $store, $route, myBond, delegation }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -324,8 +327,9 @@ describe(`PageValidator`, () => { } const myBond = 1 const newHeader = { height: `30` } + const delegation = { loaded: true } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myBond }, + { session, $store, $route, myBond, delegation }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -342,8 +346,9 @@ describe(`PageValidator`, () => { } const myBond = 1 const newHeader = { height: `20` } + const delegation = { loaded: true } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myBond }, + { session, $store, $route, myBond, delegation }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -360,8 +365,9 @@ describe(`PageValidator`, () => { } const myBond = 0 const newHeader = { height: `30` } + const delegation = { loaded: true } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myBond }, + { session, $store, $route, myBond, delegation }, newHeader) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, @@ -384,8 +390,9 @@ describe(`PageValidator`, () => { } const myBond = 1 const newHeader = { height: `20` } + const delegation = { loaded: true } PageValidator.watch.lastHeader.handler.call( - { session, $store, $route, myBond }, + { session, $store, $route, myBond, delegation }, newHeader) expect($store.dispatch).toHaveBeenCalledWith( `getRewardsFromValidator`, From a45bb991ec14b1f052a9ecb7d9fd5ed0b80a84d1 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Wed, 20 Mar 2019 18:11:08 +0100 Subject: [PATCH 16/18] address Fabo's comments --- .../components/staking/PageValidator.vue | 23 +++++++++---------- .../components/staking/PageValidator.spec.js | 17 ++++++++------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/app/src/renderer/components/staking/PageValidator.vue b/app/src/renderer/components/staking/PageValidator.vue index 19cfb3353f..841cc2e925 100644 --- a/app/src/renderer/components/staking/PageValidator.vue +++ b/app/src/renderer/components/staking/PageValidator.vue @@ -307,6 +307,17 @@ export default { } }, watch: { + "session.signedIn": { + immediate: true, + handler(newSignedIn) { + if (newSignedIn && this.delegation.loaded && Number(this.myBond) > 0) { + this.$store.dispatch( + `getRewardsFromValidator`, + this.$route.params.validator + ) + } + } + }, validator: { immediate: true, handler(validator) { @@ -333,18 +344,6 @@ export default { } } }, - mounted() { - if ( - this.session.signedIn && - this.delegation.loaded && - Number(this.myBond) > 0 - ) { - this.$store.dispatch( - `getRewardsFromValidator`, - this.$route.params.validator - ) - } - }, methods: { onDelegation() { this.$refs.delegationModal.open() diff --git a/test/unit/specs/components/staking/PageValidator.spec.js b/test/unit/specs/components/staking/PageValidator.spec.js index 609d6a0fa9..eb34d2dc73 100644 --- a/test/unit/specs/components/staking/PageValidator.spec.js +++ b/test/unit/specs/components/staking/PageValidator.spec.js @@ -260,13 +260,14 @@ describe(`PageValidator`, () => { }) }) - it(`should call user rewards on mount`, () => { - const session = { signedIn: true } + it(`should call user rewards on signed in`, () => { const $store = { dispatch: jest.fn() } const myBond = 1 const $route = { params: { validator: `cosmos1address` } } const delegation = { loaded: true } - PageValidator.mounted.call({ session, $store, $route, myBond, delegation }) + PageValidator.watch[`session.signedIn`].handler.call( + { $store, $route, myBond, delegation }, true + ) expect($store.dispatch).toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator @@ -274,10 +275,11 @@ describe(`PageValidator`, () => { }) it(`shouldn't call user rewards if not signed in`, () => { - const session = { signedIn: false } const $store = { dispatch: jest.fn() } const $route = { params: { validator: `cosmos1address` } } - PageValidator.mounted.call({ session, $store, $route }) + PageValidator.watch[`session.signedIn`].handler.call( + { $store, $route }, false + ) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator @@ -285,12 +287,13 @@ describe(`PageValidator`, () => { }) it(`shouldn't call user rewards there're no delegations`, () => { - const session = { signedIn: true } const $store = { dispatch: jest.fn() } const myBond = 0 const $route = { params: { validator: `cosmos1address` } } const delegation = { loaded: true } - PageValidator.mounted.call({ session, $store, $route, myBond, delegation }) + PageValidator.watch[`session.signedIn`].handler.call( + { $store, $route, myBond, delegation }, true + ) expect($store.dispatch).not.toHaveBeenCalledWith( `getRewardsFromValidator`, $route.params.validator From 78ed8ed448353e056c3a12118c088d770623055c Mon Sep 17 00:00:00 2001 From: Fabian Weber Date: Wed, 20 Mar 2019 18:40:11 +0100 Subject: [PATCH 17/18] query rewards on refresh correctly --- .../components/staking/PageValidator.vue | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/app/src/renderer/components/staking/PageValidator.vue b/app/src/renderer/components/staking/PageValidator.vue index 5067983414..a8d3b21118 100644 --- a/app/src/renderer/components/staking/PageValidator.vue +++ b/app/src/renderer/components/staking/PageValidator.vue @@ -173,7 +173,6 @@