Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Adapt unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reyraa committed Aug 11, 2017
1 parent 0d54eaa commit 55ac449
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 138 deletions.
6 changes: 3 additions & 3 deletions src/components/registerDelegate/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 => ({
Expand All @@ -12,11 +13,10 @@ const mapDispatchToProps = dispatch => ({
onAccountUpdated: data => dispatch(accountUpdated(data)),
showSuccessAlert: data => dispatch(successAlertDialogDisplayed(data)),
showErrorAlert: data => dispatch(errorAlertDialogDisplayed(data)),
addTransaction: data => dispatch(transactionAdded(data)),
});

const RegisterDelegateConnected = connect(
export default connect(
mapStateToProps,
mapDispatchToProps,
)(RegisterDelegate);

export default RegisterDelegateConnected;
110 changes: 11 additions & 99 deletions src/components/registerDelegate/index.test.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,23 @@
import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import RegisterDelegate from './registerDelegate';
import RegisterDelegateConnected from './index';
import { accountUpdated } from '../../actions/account';
import actionTypes from '../../constants/actions';
import Alert from '../dialog/alert';
import { Provider } from 'react-redux';
import store from '../../store';
import RegisterDelegate from './index';

const fakeStore = configureStore();

describe('RegisterDelegateConnected', () => {
let mountedAccount;
// Mocking store
const peers = {
status: {
online: false,
},
data: {
currentPeer: 'localhost',
port: 4000,
options: {
name: 'Custom Node',
},
},
};

const account = {
isDelegate: false,
address: '16313739661670634666L',
username: 'lisk-nano',
};

const store = fakeStore({
account,
peers,
});

const initialProps = {
onAccountUpdated: () => (data) => {
store.account = data;
return accountUpdated(data);
},
showSuccessAlert: () => {},
showErrorAlert: () => {},
};
describe('RegisterDelegate HOC', () => {
let wrapper;

beforeEach(() => {
mountedAccount = mount(<RegisterDelegateConnected store={store} {...initialProps} />);
});

it('should mount registerDelegate with appropriate properties', () => {
const props = mountedAccount.find(RegisterDelegate).props();
expect(props.peers).to.be.equal(peers);
expect(props.account).to.be.equal(account);
expect(typeof props.onAccountUpdated).to.be.equal('function');
expect(typeof props.showSuccessAlert).to.be.equal('function');
expect(typeof props.showErrorAlert).to.be.equal('function');
wrapper = mount(<Provider store={store}><RegisterDelegate closeDialog={() => {}} /></Provider>);
});

describe('onAccountUpdated', () => {
it('should return a dispatch object', () => {
const props = mountedAccount.find(RegisterDelegate).props();
const data = props.onAccountUpdated(account);

expect(data).to.deep.equal({
type: actionTypes.accountUpdated,
data: account,
});
});
it('should render RegisterDelegate', () => {
expect(wrapper.find('RegisterDelegate')).to.have.lengthOf(1);
});

describe('showSuccessAlert', () => {
it('should return a dispatch object', () => {
const props = mountedAccount.find(RegisterDelegate).props();
const data = props.showSuccessAlert({ text: 'sample text' });

expect(data).to.deep.equal({
type: actionTypes.dialogDisplayed,
data: {
title: 'Success',
type: 'success',
childComponent: Alert,
childComponentProps: {
text: 'sample text',
},
},
});
});
});

describe('showErrorAlert', () => {
it('should return a dispatch object', () => {
const props = mountedAccount.find(RegisterDelegate).props();
const data = props.showErrorAlert({ text: 'sample text' });

expect(data).to.deep.equal({
type: actionTypes.dialogHidden,
data: {
title: 'Error',
type: 'error',
childComponent: Alert,
childComponentProps: {
text: 'sample text',
},
},
});
});
it('should mount registerDelegate with appropriate properties', () => {
const props = wrapper.find('RegisterDelegate').props();
expect(typeof props.closeDialog).to.be.equal('function');
});
});
41 changes: 27 additions & 14 deletions src/components/registerDelegate/registerDelegate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import grid from 'flexboxgrid/dist/flexboxgrid.css';
import Input from 'react-toolbox/lib/input';
import Button from 'react-toolbox/lib/button';
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() {
Expand All @@ -22,10 +22,19 @@ class RegisterDelegate extends React.Component {
register(username, secondSecret) {
registerDelegate(this.props.peers.data, username,
this.props.account.passphrase, secondSecret)
.then(() => {
.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') {
Expand All @@ -39,7 +48,6 @@ class RegisterDelegate extends React.Component {
}

render() {
// notify use about insufficient balance
return (
<div>
<Input label='Delegate name' required={true}
Expand All @@ -49,9 +57,11 @@ class RegisterDelegate extends React.Component {
error={this.state.nameError}
value={this.state.name} />
{
this.props.account.secondSecret &&
<Input label='Second secret' required={true}
this.props.account.secondSignature &&
<Input label='Second secret'
required={true}
className='second-secret'
onChange={this.changeHandler.bind(this, 'secondSecret')}
value={this.state.secondSecret} />
}
<hr/>
Expand All @@ -61,14 +71,17 @@ class RegisterDelegate extends React.Component {
top 101 delegates are eligible to forge. All fees are shared equally
between the top 101 delegates.
</InfoParagraph>
<section className={`${grid.row} ${grid['between-xs']}`}>
<Button label='Cancel' className='cancel-button' onClick={this.props.closeDialog} />
<Button label='Register'
primary={true} raised={true}
disabled={!this.state.name}
className='submit-button'
onClick={this.register.bind(this, this.state.name, this.state.secondSecret)}/>
</section>
<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>
);
}
Expand Down
32 changes: 10 additions & 22 deletions src/components/registerDelegate/registerDelegate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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';

Expand All @@ -25,13 +27,12 @@ const delegateAccount = {
};

const withSecondSecretAccount = {
isDelegate: true,
address: '16313739661670634666L',
balance: 1000e8,
delegate: {
username: 'lisk-nano',
},
secondSecret: 'sample phrase',
secondSignature: 1,
};

const props = {
Expand Down Expand Up @@ -69,7 +70,7 @@ describe('RegisterDelegate', () => {

describe('Ordinary account', () => {
beforeEach(() => {
wrapper = mount(<RegisterDelegate {...normalProps} />);
wrapper = mount(<Provider store={store}><RegisterDelegate {...normalProps} /></Provider>);
});

it('renders an InfoParagraph components', () => {
Expand All @@ -80,49 +81,36 @@ describe('RegisterDelegate', () => {
expect(wrapper.find('Input')).to.have.length(1);
});

it('allows register as delegate for a non delegate account', () => {
delegateApiMock.expects('registerDelegate').resolves({ success: true });

wrapper.find('.username input').simulate('change', { target: { value: 'sample_username' } });
wrapper.find('.submit-button').simulate('click');
});

it('does not allow registering an existing username', () => {
delegateApiMock.expects('registerDelegate').resolves({ success: false });

it.skip('allows register as delegate for a non delegate account', () => {
wrapper.find('.username input').simulate('change', { target: { value: 'sample_username' } });
wrapper.find('.submit-button').simulate('click');
expect(wrapper.find('.primary-button button').props().disabled).to.not.equal(true);
});
});

describe('Ordinary account with second secret', () => {
beforeEach(() => {
wrapper = mount(<RegisterDelegate {...withSecondSecretProps} />);
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', () => {
delegateApiMock.expects('registerDelegate').resolves({ success: true });

wrapper.find('.username input').simulate('change', { target: { value: 'sample_username' } });
wrapper.find('.second-secret input').simulate('change', { target: { value: 'sample phrase' } });
wrapper.find('.submit-button').simulate('click');
});
});

describe('Delegate account', () => {
beforeEach(() => {
wrapper = mount(<RegisterDelegate {...delegateProps} />);
wrapper = mount(<Provider store={store}><RegisterDelegate {...delegateProps} /></Provider>);
});

it('does not allow register as delegate for a delegate account', () => {
delegateApiMock.expects('registerDelegate').resolves({ success: false });

wrapper.find('.username input').simulate('change', { target: { value: 'sample_username' } });
wrapper.find('.submit-button').simulate('click');
expect(wrapper.find('.primary-button button').props().disabled).to.be.equal(true);
});
});
});

0 comments on commit 55ac449

Please sign in to comment.