From 28dfe83618c5b22e08434cf55a3850de17c095e2 Mon Sep 17 00:00:00 2001 From: Shusetsu Toda Date: Wed, 30 May 2018 17:15:31 +0200 Subject: [PATCH] :white_check_mark: Add tests for get node status command --- src/commands/get_node_status.js | 4 +- test/specs/commands/get_node_status.js | 95 ++++++++++++++++++++++++++ test/specs/utils/tablify.js | 8 +++ test/steps/api/1_given.js | 7 ++ test/steps/general/1_given.js | 7 ++ test/steps/printing/3_then.js | 33 +++++---- test/steps/queries/3_then.js | 26 ++++++- test/steps/setup.js | 9 +++ test/steps/utils.js | 7 ++ test/steps/vorpal/1_given.js | 6 ++ 10 files changed, 185 insertions(+), 17 deletions(-) create mode 100644 test/specs/commands/get_node_status.js diff --git a/src/commands/get_node_status.js b/src/commands/get_node_status.js index 32084d39..fef5d70f 100644 --- a/src/commands/get_node_status.js +++ b/src/commands/get_node_status.js @@ -53,7 +53,7 @@ export const actionCreator = () => async ({ options }) => { }); }; -const get = createCommand({ +const getNodeStatus = createCommand({ command: 'get node status', description, actionCreator, @@ -61,4 +61,4 @@ const get = createCommand({ errorPrefix: 'Could not get node status', }); -export default get; +export default getNodeStatus; diff --git a/test/specs/commands/get_node_status.js b/test/specs/commands/get_node_status.js new file mode 100644 index 00000000..7d632c77 --- /dev/null +++ b/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, + ); + }, + ); + }); + }); + }); + }); +}); diff --git a/test/specs/utils/tablify.js b/test/specs/utils/tablify.js index c0f805b0..49a8e7a2 100644 --- a/test/specs/utils/tablify.js +++ b/test/specs/utils/tablify.js @@ -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( diff --git a/test/steps/api/1_given.js b/test/steps/api/1_given.js index 70eedcd5..db82c9a9 100644 --- a/test/steps/api/1_given.js +++ b/test/steps/api/1_given.js @@ -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; +} diff --git a/test/steps/general/1_given.js b/test/steps/general/1_given.js index 060d328b..cefc4b97 100644 --- a/test/steps/general/1_given.js +++ b/test/steps/general/1_given.js @@ -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', diff --git a/test/steps/printing/3_then.js b/test/steps/printing/3_then.js index f46cfa56..d85cc9e3 100644 --- a/test/steps/printing/3_then.js +++ b/test/steps/printing/3_then.js @@ -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; @@ -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]); }); } @@ -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, diff --git a/test/steps/queries/3_then.js b/test/steps/queries/3_then.js index 6d7d9f50..b7a4a144 100644 --- a/test/steps/queries/3_then.js +++ b/test/steps/queries/3_then.js @@ -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); @@ -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 }); +} diff --git a/test/steps/setup.js b/test/steps/setup.js index 7e33492b..e7f1be80 100644 --- a/test/steps/setup.js +++ b/test/steps/setup.js @@ -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), + }, }); } @@ -287,6 +292,10 @@ export function setUpCommandList() { setUpQueryStubs(); } +export function setUpCommandGetNodeStatus() { + setUpLiskElementsAPIStubs.call(this); +} + export function setUpCommandConfig() { setUpConfigStubs(); } diff --git a/test/steps/utils.js b/test/steps/utils.js index d58949cc..0b7eda14 100644 --- a/test/steps/utils.js +++ b/test/steps/utils.js @@ -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'; @@ -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, @@ -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'); diff --git a/test/steps/vorpal/1_given.js b/test/steps/vorpal/1_given.js index 4564dddb..da0d142c 100644 --- a/test/steps/vorpal/1_given.js +++ b/test/steps/vorpal/1_given.js @@ -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',