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

Commit

Permalink
Improve encryptMessage test coverageImprove encryptMessage test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed Nov 23, 2017
1 parent 77a5e75 commit 9806446
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/components/encryptMessage/encryptMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import sinon from 'sinon';
import { Provider } from 'react-redux';
import { I18nextProvider } from 'react-i18next';
import PropTypes from 'prop-types';
import Lisk from 'lisk-js';
import i18n from '../../i18n';
import store from '../../store';
import Authenticate from '../authenticate';
import EncryptMessage from './encryptMessage';


describe('EncryptMessage', () => {
let wrapper;
let successToastSpy;
let copyMock;
let encryptMessageSpy;
let props;
const recipientPublicKey = '164a0580cd2b430bc3496f82adf51b799546a3a4658bb9dca550a0e20cb579c8';
const message = 'Hello world';
const publicKey = '164a0580cd2b430bc3496f82adf51b799546a3a4658bb9dca550a0e20cb579c8';
Expand All @@ -24,42 +23,64 @@ describe('EncryptMessage', () => {
};

beforeEach(() => {
successToastSpy = sinon.spy();
copyMock = sinon.mock();
encryptMessageSpy = sinon.spy(Lisk.crypto, 'encryptMessageWithSecret');
const props = {
props = {
account,
successToast: successToastSpy,
copyToClipboard: copyMock,
successToast: sinon.spy(),
errorToast: sinon.spy(),
copyToClipboard: sinon.mock(),
t: key => key,
};

wrapper = mount(<Provider store={store}>
<I18nextProvider i18n={ i18n }>
<EncryptMessage {...props} />
</I18nextProvider>
</Provider>);
const options = {
context: { store, i18n },
childContextTypes: {
store: PropTypes.object.isRequired,
i18n: PropTypes.object.isRequired,
},
};

wrapper = mount(<EncryptMessage {...props} />, options);
});

afterEach(() => {
encryptMessageSpy.restore();
});

it('shows Authenticate step if passphrase is not available', () => {
wrapper.setProps({
account: { publicKey },
});
expect(wrapper.find(Authenticate)).to.have.length(1);
});

it('allows to encrypt a message, copies encrypted message result to clipboard and shows success toast', () => {
copyMock.returns(true);
props.copyToClipboard.returns(true);
wrapper.find('.message textarea').simulate('change', { target: { value: message } });
wrapper.find('.recipientPublicKey input').simulate('change', { target: { value: recipientPublicKey } });
wrapper.find('form').simulate('submit');
expect(encryptMessageSpy).to.have.been
.calledWith(message, account.passphrase, recipientPublicKey);
expect(successToastSpy).to.have.been.calledWith({ label: 'Result copied to clipboard' });
expect(props.successToast).to.have.been.calledWith({ label: 'Result copied to clipboard' });
});

it('shows error toast if encryp message failed', () => {
const invalidPublicKey = 'INVALID';
props.copyToClipboard.returns(true);
wrapper.find('.message textarea').simulate('change', { target: { value: message } });
wrapper.find('.recipientPublicKey input').simulate('change', { target: { value: invalidPublicKey } });
wrapper.find('form').simulate('submit');
expect(encryptMessageSpy).to.have.been.calledWith(
message, account.passphrase, invalidPublicKey);
expect(props.errorToast).to.have.been.calledWith(
{ label: 'nacl_raw._crypto_box signalled an error' });
});

it('does not show success toast if copy-to-clipboard failed', () => {
copyMock.returns(false);
props.copyToClipboard.returns(false);
wrapper.find('.message textarea').simulate('change', { target: { value: message } });
wrapper.find('.recipientPublicKey input').simulate('change', { target: { value: recipientPublicKey } });
wrapper.find('.primary-button').simulate('click');
expect(successToastSpy).to.have.not.been.calledWith();
wrapper.find('form').simulate('submit');
expect(props.successToast).to.have.not.been.calledWith();
});
});

0 comments on commit 9806446

Please sign in to comment.