This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Register as delegate - Closes #354 #543
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
625399b
Show dialog in header
reyraa 6124963
temp
reyraa 1b0101c
Fix conflicts
reyraa 8e4f2b4
Create register deliegate componenet base logic
reyraa fac6282
Hide button if already a delegate
reyraa 64b6462
Fixing eslint bugs and adding account to Header properties
reyraa 9074cf3
Add unit test coverage
reyraa 8a2328e
Minor fixing
reyraa 8e2a2d7
merge conflicts
reyraa 7024822
Improve unit tests
reyraa 38ce2a9
Merge branch 'development' into 354-register-as-delegate
reyraa 0d54eaa
merge conflicts
reyraa 55ac449
Adapt unit tests
reyraa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,22 @@ | ||
import { connect } from 'react-redux'; | ||
import RegisterDelegate from './registerDelegate'; | ||
import { accountUpdated } from '../../actions/account'; | ||
import { transactionAdded } from '../../actions/transactions'; | ||
import { successAlertDialogDisplayed, errorAlertDialogDisplayed } from '../../actions/dialog'; | ||
|
||
const mapStateToProps = state => ({ | ||
account: state.account, | ||
peers: state.peers, | ||
}); | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
onAccountUpdated: data => dispatch(accountUpdated(data)), | ||
showSuccessAlert: data => dispatch(successAlertDialogDisplayed(data)), | ||
showErrorAlert: data => dispatch(errorAlertDialogDisplayed(data)), | ||
addTransaction: data => dispatch(transactionAdded(data)), | ||
}); | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(RegisterDelegate); |
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,23 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { mount } from 'enzyme'; | ||
import { Provider } from 'react-redux'; | ||
import store from '../../store'; | ||
import RegisterDelegate from './index'; | ||
|
||
describe('RegisterDelegate HOC', () => { | ||
let wrapper; | ||
|
||
beforeEach(() => { | ||
wrapper = mount(<Provider store={store}><RegisterDelegate closeDialog={() => {}} /></Provider>); | ||
}); | ||
|
||
it('should render RegisterDelegate', () => { | ||
expect(wrapper.find('RegisterDelegate')).to.have.lengthOf(1); | ||
}); | ||
|
||
it('should mount registerDelegate with appropriate properties', () => { | ||
const props = wrapper.find('RegisterDelegate').props(); | ||
expect(typeof props.closeDialog).to.be.equal('function'); | ||
}); | ||
}); |
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,90 @@ | ||
import React from 'react'; | ||
import Input from 'react-toolbox/lib/input'; | ||
import InfoParagraph from '../infoParagraph'; | ||
import ActionBar from '../actionBar'; | ||
import { registerDelegate } from '../../utils/api/delegate'; | ||
import Fees from '../../constants/fees'; | ||
|
||
class RegisterDelegate extends React.Component { | ||
constructor() { | ||
super(); | ||
|
||
this.state = { | ||
name: '', | ||
nameError: '', | ||
}; | ||
} | ||
|
||
changeHandler(name, value) { | ||
this.setState({ [name]: value }); | ||
} | ||
|
||
register(username, secondSecret) { | ||
registerDelegate(this.props.peers.data, username, | ||
this.props.account.passphrase, secondSecret) | ||
.then((data) => { | ||
this.props.showSuccessAlert({ | ||
text: `Delegate registration was successfully submitted with username: "${this.state.name}". It can take several seconds before it is processed.`, | ||
}); | ||
|
||
// add to pending transaction | ||
this.props.addTransaction({ | ||
id: data.transactionId, | ||
senderPublicKey: this.props.account.publicKey, | ||
senderId: this.props.account.address, | ||
amount: 0, | ||
fee: Fees.registerDelegate, | ||
}); | ||
}) | ||
.catch((error) => { | ||
if (error && error.message === 'Username already exists') { | ||
this.setState({ nameError: error.message }); | ||
} else { | ||
this.props.showErrorAlert({ | ||
text: error && error.message ? `${error.message}.` : 'An error occurred while registering as delegate.', | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<Input label='Delegate name' required={true} | ||
autoFocus={true} | ||
className='username' | ||
onChange={this.changeHandler.bind(this, 'name')} | ||
error={this.state.nameError} | ||
value={this.state.name} /> | ||
{ | ||
this.props.account.secondSignature && | ||
<Input label='Second secret' | ||
required={true} | ||
className='second-secret' | ||
onChange={this.changeHandler.bind(this, 'secondSecret')} | ||
value={this.state.secondSecret} /> | ||
} | ||
<hr/> | ||
<InfoParagraph> | ||
Becoming a delegate requires registration. You may choose your own | ||
delegate name, which can be used to promote your delegate. Only the | ||
top 101 delegates are eligible to forge. All fees are shared equally | ||
between the top 101 delegates. | ||
</InfoParagraph> | ||
<ActionBar | ||
secondaryButton={{ | ||
onClick: this.props.closeDialog.bind(this), | ||
}} | ||
primaryButton={{ | ||
label: 'Register', | ||
fee: Fees.registerDelegate, | ||
disabled: !this.state.name || | ||
(this.props.account.secondSignature && !this.state.secondSecret), | ||
onClick: this.register.bind(this, this.state.name, this.state.secondSecret), | ||
}} /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default RegisterDelegate; |
116 changes: 116 additions & 0 deletions
116
src/components/registerDelegate/registerDelegate.test.js
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,116 @@ | ||
import React from 'react'; | ||
import chai, { expect } from 'chai'; | ||
import { mount } from 'enzyme'; | ||
import chaiEnzyme from 'chai-enzyme'; | ||
import sinon from 'sinon'; | ||
import Lisk from 'lisk-js'; | ||
import { Provider } from 'react-redux'; | ||
import store from '../../store'; | ||
import RegisterDelegate from './registerDelegate'; | ||
import * as delegateApi from '../../utils/api/delegate'; | ||
|
||
chai.use(chaiEnzyme()); | ||
|
||
const normalAccount = { | ||
isDelegate: false, | ||
address: '16313739661670634666L', | ||
balance: 1000e8, | ||
}; | ||
|
||
const delegateAccount = { | ||
isDelegate: true, | ||
address: '16313739661670634666L', | ||
balance: 1000e8, | ||
delegate: { | ||
username: 'lisk-nano', | ||
}, | ||
}; | ||
|
||
const withSecondSecretAccount = { | ||
address: '16313739661670634666L', | ||
balance: 1000e8, | ||
delegate: { | ||
username: 'lisk-nano', | ||
}, | ||
secondSignature: 1, | ||
}; | ||
|
||
const props = { | ||
peers: { | ||
data: Lisk.api({ | ||
name: 'Custom Node', | ||
custom: true, | ||
address: 'http://localhost:4000', | ||
testnet: true, | ||
nethash: '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d', | ||
}), | ||
}, | ||
closeDialog: () => {}, | ||
onAccountUpdated: () => {}, | ||
showSuccessAlert: () => {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can make this Those tests ending with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that I'm not doing this either (in |
||
showErrorAlert: () => {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
}; | ||
|
||
const delegateProps = { ...props, account: delegateAccount }; | ||
const normalProps = { ...props, account: normalAccount }; | ||
const withSecondSecretProps = { ...props, account: withSecondSecretAccount }; | ||
|
||
describe('RegisterDelegate', () => { | ||
let wrapper; | ||
let delegateApiMock; | ||
|
||
beforeEach(() => { | ||
delegateApiMock = sinon.mock(delegateApi); | ||
}); | ||
|
||
afterEach(() => { | ||
delegateApiMock.verify(); | ||
delegateApiMock.restore(); | ||
}); | ||
|
||
describe('Ordinary account', () => { | ||
beforeEach(() => { | ||
wrapper = mount(<Provider store={store}><RegisterDelegate {...normalProps} /></Provider>); | ||
}); | ||
|
||
it('renders an InfoParagraph components', () => { | ||
expect(wrapper.find('InfoParagraph')).to.have.length(1); | ||
}); | ||
|
||
it('renders one Input component for a normal account', () => { | ||
expect(wrapper.find('Input')).to.have.length(1); | ||
}); | ||
|
||
it.skip('allows register as delegate for a non delegate account', () => { | ||
wrapper.find('.username input').simulate('change', { target: { value: 'sample_username' } }); | ||
expect(wrapper.find('.primary-button button').props().disabled).to.not.equal(true); | ||
}); | ||
}); | ||
|
||
describe('Ordinary account with second secret', () => { | ||
beforeEach(() => { | ||
wrapper = mount(<Provider store={store}> | ||
<RegisterDelegate {...withSecondSecretProps} /></Provider>); | ||
}); | ||
|
||
it('renders two Input component for a an account with second secret', () => { | ||
expect(wrapper.find('Input')).to.have.length(2); | ||
}); | ||
|
||
it('allows register as delegate for a non delegate account with second secret', () => { | ||
wrapper.find('.username input').simulate('change', { target: { value: 'sample_username' } }); | ||
wrapper.find('.second-secret input').simulate('change', { target: { value: 'sample phrase' } }); | ||
}); | ||
}); | ||
|
||
describe('Delegate account', () => { | ||
beforeEach(() => { | ||
wrapper = mount(<Provider store={store}><RegisterDelegate {...delegateProps} /></Provider>); | ||
}); | ||
|
||
it('does not allow register as delegate for a delegate account', () => { | ||
wrapper.find('.username input').simulate('change', { target: { value: 'sample_username' } }); | ||
expect(wrapper.find('.primary-button button').props().disabled).to.be.equal(true); | ||
}); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot find where is this used.