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

Commit

Permalink
🌱 Add query placeholder and fix test phrases
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Jul 31, 2018
1 parent 9fa74d6 commit 9f572d0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 41 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
"@oclif/plugin-help"
],
"topics": {
"help": { "description": "Displays help for Lisk Commander." },
"warranty": { "description": "Displays warranty notice for Lisk Commander." },
"copyright": { "description": "Displays copyright notice for Lisk Commander." },
"account": { "description": "Manages Lisk accounts." },
"config": { "description": "Manages Lisk Commander configuration." },
"account": { "description": "Manages Lisk accounts." }
"copyright": { "description": "Displays copyright notice." },
"help": { "description": "Displays help." },
"warranty": { "description": "Displays warranty notice." }
}
},
"files": [
Expand Down
10 changes: 8 additions & 2 deletions src/commands/account/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ export default class GetCommand extends BaseCommand {
async run() {
const { args: { addresses } } = this.parse(GetCommand);
const req = addresses.map(address => ({
limit: 1,
address,
query: {
limit: 1,
address,
},
placeholder: {
address,
message: 'No data was returned.',
},
}));
const client = getAPIClient(this.userConfig.api);
const results = await query(client, 'accounts', req);
Expand Down
13 changes: 8 additions & 5 deletions src/utils/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
*
*/

const handleResponse = (endpoint, res) => {
const handleResponse = (endpoint, res, placeholder) => {
// Get endpoints with 2xx status code should always return with data key.
if (!res.data) {
throw new Error('No data was returned.');
}
if (Array.isArray(res.data)) {
if (res.data.length === 0) {
if (placeholder) {
return placeholder;
}
throw new Error(`No ${endpoint} found using specified parameters.`);
}
return res.data[0];
Expand All @@ -33,10 +36,10 @@ export default async (client, endpoint, parameters) =>
? Promise.all(
parameters.map(param =>
client[endpoint]
.get(param)
.then(res => handleResponse(endpoint, res)),
.get(param.query)
.then(res => handleResponse(endpoint, res, param.placeholder)),
),
)
: client[endpoint]
.get(parameters)
.then(res => handleResponse(endpoint, res));
.get(parameters.query)
.then(res => handleResponse(endpoint, res, parameters.placeholder));
34 changes: 26 additions & 8 deletions test/commands/account/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('account:get command', () => {
.it('should throw an error when arg is not provided');
});

describe('account:get account', () => {
describe('account:get address', () => {
const address = '3520445367460290306L';
const queryResult = [
{
Expand All @@ -60,15 +60,21 @@ describe('account:get command', () => {
expect(api.default).to.be.calledWithExactly(apiConfig);
expect(query.default).to.be.calledWithExactly(apiClientStub, endpoint, [
{
limit: 1,
address,
query: {
limit: 1,
address,
},
placeholder: {
address,
message: 'No data was returned.',
},
},
]);
return expect(printMethodStub).to.be.calledWithExactly(queryResult);
});
});

describe('account:get accounts', () => {
describe('account:get addresses', () => {
const addresses = ['3520445367460290306L', '2802325248134221536L'];
const queryResult = [
{
Expand All @@ -89,12 +95,24 @@ describe('account:get command', () => {
expect(api.default).to.be.calledWithExactly(apiConfig);
expect(query.default).to.be.calledWithExactly(apiClientStub, endpoint, [
{
limit: 1,
address: addresses[0],
query: {
limit: 1,
address: addresses[0],
},
placeholder: {
address: addresses[0],
message: 'No data was returned.',
},
},
{
limit: 1,
address: addresses[1],
query: {
limit: 1,
address: addresses[1],
},
placeholder: {
address: addresses[1],
message: 'No data was returned.',
},
},
]);
return expect(printMethodStub).to.be.calledWithExactly(queryResult);
Expand Down
64 changes: 42 additions & 22 deletions test/utils/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ import query from '../../src/utils/query';
describe('query utils', () => {
const defaultEndpoint = 'accounts';
const defaultParameters = {
address: 'address1',
limit: 1,
query: {
address: 'address1',
limit: 1,
},
};
const defaultArrayParameters = [
{
address: 'address1',
limit: 1,
query: {
address: 'address1',
limit: 1,
},
},
{
address: 'address2',
limit: 1,
query: {
address: 'address2',
limit: 1,
},
},
];

Expand All @@ -50,7 +56,9 @@ describe('query utils', () => {
});

it('should call API client and should reject with an error', () => {
expect(apiClient.accounts.get).to.be.calledWithExactly(defaultParameters);
expect(apiClient.accounts.get).to.be.calledWithExactly(
defaultParameters.query,
);

return expect(queryResult).to.be.rejectedWith(
Error,
Expand All @@ -69,18 +77,36 @@ describe('query utils', () => {
get: sandbox.stub().resolves(response),
},
};
queryResult = query(apiClient, defaultEndpoint, defaultParameters);
return Promise.resolve();
});

it('should call API client and should reject with an error', () => {
expect(apiClient.accounts.get).to.be.calledWithExactly(defaultParameters);
queryResult = query(apiClient, defaultEndpoint, defaultParameters);
expect(apiClient.accounts.get).to.be.calledWithExactly(
defaultParameters.query,
);

return expect(queryResult).to.be.rejectedWith(
Error,
'No accounts found using specified parameters.',
);
});

it('should call API client and should return placeholder when it is set', () => {
const placeholder = {
id: 'default id',
};
const paramWithPlaceholder = {
...defaultParameters,
placeholder,
};
queryResult = query(apiClient, defaultEndpoint, paramWithPlaceholder);
expect(apiClient.accounts.get).to.be.calledWithExactly(
defaultParameters.query,
);

return expect(queryResult).to.be.eventually.equal(placeholder);
});
});

describe('when the response is an array', () => {
Expand All @@ -102,13 +128,10 @@ describe('query utils', () => {
return Promise.resolve();
});

it('should call API client', () => {
return expect(apiClient.accounts.get).to.be.calledWithExactly(
defaultParameters,
it('should call API client and resolve to an object', () => {
expect(apiClient.accounts.get).to.be.calledWithExactly(
defaultParameters.query,
);
});

it('should resolve to an object', () => {
return expect(queryResult).to.eventually.eql(response.data[0]);
});
});
Expand All @@ -130,13 +153,10 @@ describe('query utils', () => {
return Promise.resolve();
});

it('should call API client', () => {
return expect(apiClient.accounts.get).to.be.calledWithExactly(
defaultParameters,
it('should call API client and resolve to an object', () => {
expect(apiClient.accounts.get).to.be.calledWithExactly(
defaultParameters.query,
);
});

it('should resolve to an object', () => {
return expect(queryResult).to.eventually.eql(response.data);
});
});
Expand All @@ -161,7 +181,7 @@ describe('query utils', () => {

it('should call API client', () => {
defaultArrayParameters.forEach(param =>
expect(apiClient.accounts.get).to.be.calledWithExactly(param),
expect(apiClient.accounts.get).to.be.calledWithExactly(param.query),
);
return Promise.resolve();
});
Expand Down

0 comments on commit 9f572d0

Please sign in to comment.