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

Commit

Permalink
✅ Add tests for get node status command
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed May 30, 2018
1 parent 2447b97 commit 28dfe83
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/commands/get_node_status.js
Expand Up @@ -53,12 +53,12 @@ export const actionCreator = () => async ({ options }) => {
});
};

const get = createCommand({
const getNodeStatus = createCommand({
command: 'get node status',
description,
actionCreator,
options: [['--forging', forgingDescription]],
errorPrefix: 'Could not get node status',
});

export default get;
export default getNodeStatus;
95 changes: 95 additions & 0 deletions test/specs/commands/get_node_status.js
@@ -0,0 +1,95 @@
/*
* LiskHQ/lisk-commander
* Copyright © 2017–2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
import { setUpCommandGetNodeStatus } from '../../steps/setup';
import * as given from '../../steps/1_given';
import * as when from '../../steps/2_when';
import * as then from '../../steps/3_then';

describe('get node status command', () => {
beforeEach(setUpCommandGetNodeStatus);
Given('an action "get node status"', given.anAction, () => {
Given('a Lisk API Instance', given.aLiskAPIInstance, () => {
Given('an options object', given.anOptionsObject, () => {
When(
'the action is called with the options',
when.theActionIsCalledWithTheOptions,
() => {
Then(
'it should call node getConstants',
then.itShouldCallNodeGetConstants,
);
Then(
'it should call node getStatus',
then.itShouldCallNodeGetStatus,
);
Then(
'it should not call node getForgingStatus',
then.itShouldNotCallNodeGetForgingStatus,
);
},
);
Given('a boolean option "forging"', given.aBooleanOption, () => {
Given(
'a Lisk API Instance getForgingStatus rejects with error',
given.aLiskAPIInstanceGetForgingStatusRejectsWithError,
() => {
When(
'the action is called with the options',
when.theActionIsCalledWithTheOptions,
() => {
Then(
'it should call node getConstants',
then.itShouldCallNodeGetConstants,
);
Then(
'it should call node getStatus',
then.itShouldCallNodeGetStatus,
);
Then(
'it should call node getForgingStatus',
then.itShouldCallNodeGetForgingStatus,
);
Then(
'it should call node getForgingStatus',
then.itShoulHaveErrorMessageForForgingStatus,
);
},
);
},
);
When(
'the action is called with the options',
when.theActionIsCalledWithTheOptions,
() => {
Then(
'it should call node getConstants',
then.itShouldCallNodeGetConstants,
);
Then(
'it should call node getStatus',
then.itShouldCallNodeGetStatus,
);
Then(
'it should call node getForgingStatus',
then.itShouldCallNodeGetForgingStatus,
);
},
);
});
});
});
});
});
8 changes: 8 additions & 0 deletions test/specs/utils/tablify.js
Expand Up @@ -38,6 +38,14 @@ describe('tablify util', () => {
);
});
});
Given('an object with object array', given.anObjectWithObjectArray, () => {
When('the object is tablified', when.theObjectIsTablified, () => {
Then(
'the returned table should have a row with the object key and stringified nested values',
then.theReturnedTableShouldHaveARowWithTheObjectKeyAndStringifiedNestedValues,
);
});
});
Given('a nested object', given.aNestedObject, () => {
When('the object is tablified', when.theObjectIsTablified, () => {
Then(
Expand Down
7 changes: 7 additions & 0 deletions test/steps/api/1_given.js
Expand Up @@ -18,3 +18,10 @@ import getAPIClient from '../../../src/utils/api';
export function aLiskAPIInstance() {
this.test.ctx.liskAPIInstance = getAPIClient();
}

export function aLiskAPIInstanceGetForgingStatusRejectsWithError() {
const { liskAPIInstance } = this.test.ctx;
const errorMessage = 'some error';
liskAPIInstance.node.getForgingStatus.rejects(new Error(errorMessage));
this.test.ctx.errorMessage = errorMessage;
}
7 changes: 7 additions & 0 deletions test/steps/general/1_given.js
Expand Up @@ -57,6 +57,13 @@ export function aNonEmptyObject() {
};
}

export function anObjectWithObjectArray() {
this.test.ctx.testObject = {
root: 'value',
objectArray: [{ sample: 1 }, { sample: 2 }],
};
}

export function aDeeplyNestedObject() {
this.test.ctx.testObject = {
root: 'value',
Expand Down
33 changes: 19 additions & 14 deletions test/steps/printing/3_then.js
Expand Up @@ -19,6 +19,7 @@ import {
shouldUsePrettyOutput,
} from '../../../src/utils/helpers';
import tablify from '../../../src/utils/tablify';
import { objectToKeyValueString } from '../utils';

export function consoleErrorShouldBeCalledWithTheFirstStringInRedAndTheOtherArguments() {
const { testArguments } = this.test.ctx;
Expand Down Expand Up @@ -141,18 +142,26 @@ export function theReturnedTableShouldHaveARowWithTheObjectKeyValues() {
});
}

export function theReturnedTableShouldHaveARowWithTheObjectKeyAndStringifiedNestedValues() {
export function theReturnedTableShouldHaveARow() {
const { returnValue, testObject } = this.test.ctx;
return Object.entries(testObject).forEach(([key, value], arrayKey) => {
const strValue =
typeof value === 'object'
? Object.entries(value)
.map(
([vKey, vValue]) =>
`${vKey}: ${JSON.stringify(vValue, null, ' ')}`,
)
.join('\n')
: value;
typeof value === 'object' ? objectToKeyValueString(value) : value;
expect({ [key]: strValue }).to.eql(returnValue[arrayKey]);
});
}

export function theReturnedTableShouldHaveARowWithTheObjectKeyAndStringifiedNestedValues() {
const { returnValue, testObject } = this.test.ctx;
return Object.entries(testObject).forEach(([key, value], arrayKey) => {
let strValue = value;
if (Array.isArray(value)) {
strValue = value
.map(element => objectToKeyValueString(element))
.join('\n');
} else if (typeof value === 'object') {
strValue = objectToKeyValueString(value);
}
expect({ [key]: strValue }).to.eql(returnValue[arrayKey]);
});
}
Expand Down Expand Up @@ -186,11 +195,7 @@ export function theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedNest
if (Array.isArray(values[key])) {
strValue = values[key].join('\n');
} else if (typeof values[key] === 'object') {
strValue = Object.entries(values[key])
.map(
([vKey, vValue]) => `${vKey}: ${JSON.stringify(vValue, null, ' ')}`,
)
.join('\n');
strValue = objectToKeyValueString(values[key]);
}
expect(returnValue[i * (innerObjectKeys.length + 1) + keyIndex + 1]).eql({
[key]: strValue,
Expand Down
26 changes: 25 additions & 1 deletion test/steps/queries/3_then.js
Expand Up @@ -13,7 +13,6 @@
* Removal or modification of this copyright notice is prohibited.
*
*/

export function itShouldResolveToTheDataOfTheResponse() {
const { returnValue, queryResult } = this.test.ctx;
return expect(returnValue).to.eventually.equal(queryResult.data);
Expand All @@ -34,3 +33,28 @@ export function itShouldResolveToAnArrayOfQueryResults() {
const arrayOfQueryResults = inputs.map(() => queryResult);
return expect(returnValue).to.eventually.eql(arrayOfQueryResults);
}

export function itShouldCallNodeGetConstants() {
const { liskAPIInstance } = this.test.ctx;
return expect(liskAPIInstance.node.getConstants).to.be.calledOnce;
}

export function itShouldCallNodeGetStatus() {
const { liskAPIInstance } = this.test.ctx;
return expect(liskAPIInstance.node.getStatus).to.be.calledOnce;
}

export function itShouldCallNodeGetForgingStatus() {
const { liskAPIInstance } = this.test.ctx;
return expect(liskAPIInstance.node.getForgingStatus).to.be.calledOnce;
}

export function itShouldNotCallNodeGetForgingStatus() {
const { liskAPIInstance } = this.test.ctx;
return expect(liskAPIInstance.node.getForgingStatus).not.to.be.called;
}

export function itShoulHaveErrorMessageForForgingStatus() {
const { returnValue, errorMessage } = this.test.ctx;
return expect(returnValue).to.eventually.eql({ forgingStatus: errorMessage });
}
9 changes: 9 additions & 0 deletions test/steps/setup.js
Expand Up @@ -118,6 +118,11 @@ function setUpLiskElementsAPIStubs() {
get: sandbox.stub().resolves(queryDefaultResult),
broadcast: sandbox.stub().resolves(broadcastSignaturesResponse),
},
node: {
getConstants: sandbox.stub().resolves(broadcastSignaturesResponse),
getStatus: sandbox.stub().resolves(broadcastSignaturesResponse),
getForgingStatus: sandbox.stub().resolves(broadcastSignaturesResponse),
},
});
}

Expand Down Expand Up @@ -287,6 +292,10 @@ export function setUpCommandList() {
setUpQueryStubs();
}

export function setUpCommandGetNodeStatus() {
setUpLiskElementsAPIStubs.call(this);
}

export function setUpCommandConfig() {
setUpConfigStubs();
}
Expand Down
7 changes: 7 additions & 0 deletions test/steps/utils.js
Expand Up @@ -27,6 +27,7 @@ import * as encryptMessage from '../../src/commands/encrypt_message';
import * as encryptPassphrase from '../../src/commands/encrypt_passphrase';
import * as config from '../../src/commands/config';
import * as get from '../../src/commands/get';
import * as getNodeStatus from '../../src/commands/get_node_status';
import * as list from '../../src/commands/list';
import * as set from '../../src/commands/set';
import * as showCopyright from '../../src/commands/show_copyright';
Expand Down Expand Up @@ -95,6 +96,7 @@ export const getActionCreator = actionName =>
'create transaction transfer': createTransactionTransfer.actionCreator,
config: config.actionCreator,
get: get.actionCreator,
'get node status': getNodeStatus.actionCreator,
list: list.actionCreator,
set: set.actionCreator,
'show copyright': showCopyright.actionCreator,
Expand Down Expand Up @@ -147,3 +149,8 @@ export const hasAncestorWithTitleMatching = (test, regExp) => {
if (!parent) return false;
return hasAncestorWithTitleMatching(parent, regExp);
};

export const objectToKeyValueString = value =>
Object.entries(value)
.map(([vKey, vValue]) => `${vKey}: ${JSON.stringify(vValue, null, ' ')}`)
.join('\n');
6 changes: 6 additions & 0 deletions test/steps/vorpal/1_given.js
Expand Up @@ -61,6 +61,12 @@ export function anActiveCommandThatCanLog() {
};
}

export function aBooleanOption() {
const { options } = this.test.ctx;
const option = getFirstQuotedString(this.test.parent.title);
options[option] = true;
}

export function anActionCreatorThatCreatesAnActionThatResolvesToAnObject() {
const testObject = {
lisk: 'js',
Expand Down

0 comments on commit 28dfe83

Please sign in to comment.