Skip to content

Commit

Permalink
Make all unit tests pass for unstaking.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Braun authored and David Braun committed Sep 27, 2018
1 parent 4e47112 commit 4bff2d2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 110 deletions.
45 changes: 22 additions & 23 deletions app/src/renderer/components/staking/PageValidator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ tm-page
//- TODO replace with address component when ready
anchor-copy.validator-profile__header__name__address(:value="validator.owner" :label="shortAddress(validator.owner)")
.column.validator-profile__header__actions
tm-btn(id="stake-btn" value="Stake" color="primary" @click.native="onStake()")
tm-btn(id="stake-btn" value="Stake" color="primary" @click.native="onStake")

tm-btn(
id="unstake-btn"
value="Unstake"
color="secondary"
@click.native="onUnstake()"
@click.native="onUnstake"
)

.row.validator-profile__header__data
Expand Down Expand Up @@ -112,7 +112,7 @@ tm-page
div(slot='footer')
tmBtn(
id="no-atoms-modal__btn"
@click.native="closeCannotStake()"
@click.native="closeCannotStake"
value="OK"
)

Expand All @@ -126,12 +126,14 @@ tm-page
div(slot='footer')
tmBtn(
id="no-bond-modal__btn"
@click.native="closeCannotUnstake()"
@click.native="closeCannotUnstake"
value="OK"
)
</template>

<script>
import BigNumber from "bignumber.js"
import { calculateTokens } from "scripts/common"
import { mapGetters } from "vuex"
import { TmBtn, TmListItem, TmPage, TmPart, TmToolBar } from "@tendermint/ui"
import { TmDataError } from "common/TmDataError"
Expand All @@ -141,6 +143,7 @@ import ModalUnstake from "staking/ModalUnstake"
import numeral from "numeral"
import AnchorCopy from "common/AnchorCopy"
import TmBalance from "common/TmBalance"
import TmModal from "common/TmModal"
export default {
name: "page-validator",
components: {
Expand All @@ -149,7 +152,7 @@ export default {
ModalUnstake,
TmBtn,
TmListItem,
TmBalance,
TmModal,
TmPage,
TmPart,
TmToolBar,
Expand All @@ -158,6 +161,7 @@ export default {
},
data: () => ({
showCannotStake: false,
showCannotUnstake: false,
showModalStake: false,
showModalUnstake: false,
shortAddress,
Expand Down Expand Up @@ -241,6 +245,9 @@ export default {
closeCannotStake() {
this.showCannotStake = false
},
closeCannotUnstake() {
this.showCannotUnstake = false
},
onStake() {
if (this.availableAtoms > 0) {
this.showModalStake = true
Expand All @@ -249,7 +256,7 @@ export default {
}
},
onUnstake() {
if (this.myBond === BigNumber(0)) {
if (this.myBond.isEqualTo(BigNumber(0))) {
this.showCannotUnstake = true
} else {
this.showModalUnstake = true
Expand Down Expand Up @@ -290,27 +297,19 @@ export default {
}
},
async submitUndelegation({ amount }) {
const candidateId = this.validator.owner
const currentlyDelegated = calculateTokens(
this.validator,
this.delegation.committedDelegates[candidateId] || 0
)
const delegation = [
{
atoms: currentlyDelegated.minus(amount),
delegate: this.validator
}
]
console.log(delegation)
try {
await this.$store.dispatch("submitDelegation", delegation)
await this.$store.dispatch("submitDelegation", {
unbondings: [
{
atoms: -amount,
delegate: this.validator
}
]
})
this.$store.commit("notify", {
title: "Successful Unstaking!",
body: `You have successfully unstaked your ${amount} ${
body: `You have successfully unstaked ${amount} ${
this.bondingDenom
}s.`
})
Expand Down
123 changes: 36 additions & 87 deletions test/unit/specs/components/staking/PageValidator.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from "bignumber.js"
import Delegation from "renderer/vuex/modules/delegation"
import ModalStake from "staking/ModalStake"
import ModalUnstake from "staking/ModalUnstake"
Expand Down Expand Up @@ -422,34 +423,7 @@ describe(`onStake`, () => {
delegations: [
{
atoms: 10,
delegate: {
bond_height: "0",
bond_intra_tx_counter: 6,
commission: "0.05",
commission_change_rate: "0.01",
commission_change_today: "0.005",
commission_max: "0.1",
delegator_shares: "19",
description: {
country: "DE",
details: "Herr Schmidt",
moniker: "herr_schmidt_revoked",
website: "www.schmidt.de"
},
keybase: undefined,
owner: "1a2b3c",
prev_bonded_shares: "0",
proposer_reward_pool: null,
pub_key: {
data: "dlN5SLqeT3LT9WsUK5iuVq1eLQV2Q1JQAuyN0VwSWK0=",
type: "AC26791624DE60"
},
revoked: false,
selfBond: 0.01,
status: 2,
tokens: "19",
voting_power: "10"
}
delegate
}
]
}
Expand Down Expand Up @@ -488,12 +462,14 @@ describe(`onStake`, () => {
})

describe(`onUnstake`, () => {
describe(`make sure we have enough atoms to unstake`, () => {
describe(`make sure there are enough atoms to unstake`, () => {
it(`is enough`, () => {
const $store = {
commit: jest.fn(),
dispatch: jest.fn(),
getters: getterValues
getters: Object.assign({}, getterValues, {
delegation: { committedDelegates: { "1a2b3c": 10 } }
})
}

const wrapper = mount(PageValidator, {
Expand Down Expand Up @@ -525,8 +501,6 @@ describe(`onUnstake`, () => {

wrapper.find(`#unstake-btn`).trigger(`click`)

expect(wrapper.vm.myBond).toEqual(BigNumber(0))

expect(
wrapper.text().includes(`You have no atoms staked with this validator.`)
).toEqual(true)
Expand All @@ -539,7 +513,7 @@ describe(`onUnstake`, () => {
})
})

describe(`submitDelegation`, () => {
describe(`submitUndelegation`, () => {
describe(`unit`, () => {
it(`success`, async () => {
const $store = {
Expand All @@ -549,26 +523,26 @@ describe(`onUnstake`, () => {
}

const {
vm: { submitDelegation }
vm: { submitUndelegation }
} = mount(PageValidator, {
mocks: {
$route: { params: { validator: `1a2b3c` } },
$store
}
})

await submitDelegation({ amount: 10 })
await submitUndelegation({ amount: 10 })

expect($store.dispatch.mock.calls).toEqual([
[`submitDelegation`, [{ atoms: BigNumber(10), delegate }]]
[`submitDelegation`, { unbondings: [{ atoms: -10, delegate }] }]
])

expect($store.commit.mock.calls).toEqual([
[
`notify`,
{
body: `You have successfully staked your atoms.`,
title: `Successful Staking!`
body: `You have successfully unstaked 10 atoms.`,
title: `Successful Unstaking!`
}
]
])
Expand All @@ -584,26 +558,26 @@ describe(`onUnstake`, () => {
}

const {
vm: { submitDelegation }
vm: { submitUndelegation }
} = mount(PageValidator, {
mocks: {
$route: { params: { validator: `1a2b3c` } },
$store
}
})

await submitDelegation({ amount: 10 })
await submitUndelegation({ amount: 10 })

expect($store.dispatch.mock.calls).toEqual([
[`submitDelegation`, [{ atoms: BigNumber(10), delegate }]]
[`submitDelegation`, { unbondings: [{ atoms: -10, delegate }] }]
])

expect($store.commit.mock.calls).toEqual([
[
`notifyError`,
{
body: `message`,
title: `Error While Staking atoms`
title: `Error While Unstaking atoms`
}
]
])
Expand All @@ -619,26 +593,26 @@ describe(`onUnstake`, () => {
}

const {
vm: { submitDelegation }
vm: { submitUndelegation }
} = mount(PageValidator, {
mocks: {
$route: { params: { validator: `1a2b3c` } },
$store
}
})

await submitDelegation({ amount: 10 })
await submitUndelegation({ amount: 10 })

expect($store.dispatch.mock.calls).toEqual([
[`submitDelegation`, [{ atoms: BigNumber(10), delegate }]]
[`submitDelegation`, { unbondings: [{ atoms: -10, delegate }] }]
])

expect($store.commit.mock.calls).toEqual([
[
`notifyError`,
{
body: `Seven`,
title: `Error While Staking atoms`
title: `Error While Unstaking atoms`
}
]
])
Expand All @@ -661,84 +635,59 @@ describe(`onUnstake`, () => {
getters: getterValues,
rootState: getterValues,
state: {
committedDelegates: { "1a2b3c": 0 },
committedDelegates: { "1a2b3c": 10 },
unbondingDelegations: {}
}
}

const {
vm: { submitDelegation }
vm: { submitUndelegation }
} = mount(PageValidator, {
mocks: {
$route: { params: { validator: `1a2b3c` } },
$store
}
})

await submitDelegation({ amount: 10 })
await submitUndelegation({ amount: 10 })

expect($store.dispatch.mock.calls).toEqual([
[
"submitDelegation",
[
{
atoms: BigNumber(10),
delegate: {
bond_height: "0",
bond_intra_tx_counter: 6,
commission: "0.05",
commission_change_rate: "0.01",
commission_change_today: "0.005",
commission_max: "0.1",
delegator_shares: "19",
description: {
country: "DE",
details: "Herr Schmidt",
moniker: "herr_schmidt_revoked",
website: "www.schmidt.de"
},
keybase: undefined,
owner: "1a2b3c",
prev_bonded_shares: "0",
proposer_reward_pool: null,
pub_key: {
data: "dlN5SLqeT3LT9WsUK5iuVq1eLQV2Q1JQAuyN0VwSWK0=",
type: "AC26791624DE60"
},
revoked: false,
selfBond: 0.01,
status: 2,
tokens: "19",
voting_power: "10"
{
unbondings: [
{
atoms: -10,
delegate
}
}
]
]
}
],
[
"sendTx",
{
begin_unbondings: [],
delegations: [
begin_unbondings: [
{
delegation: { amount: "10", denom: "atom" },
delegator_addr:
"cosmosaccaddr15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9",
shares: "10.00000000",
validator_addr: "1a2b3c"
}
],
delegations: [],
to: "cosmosaccaddr15ky9du8a2wlstz6fpx3p4mqpjyrm5ctpesxxn9",
type: "updateDelegations"
}
]
])

expect($store.commit.mock.calls).toEqual([
["setAtoms", 32],
["setAtoms", 42],
[
"notify",
{
body: "You have successfully staked your atoms.",
title: "Successful Staking!"
body: "You have successfully unstaked 10 atoms.",
title: "Successful Unstaking!"
}
]
])
Expand Down

0 comments on commit 4bff2d2

Please sign in to comment.