Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update search tests and lintings #40

Merged
merged 3 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions __tests__/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ERR_MODULE_NOT_FOUND,
ERR_MODULE_DISABLED,
ERR_MODULE_REMOVE_FAILED,
ERR_MODULE_SEARCH_FAILED,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one still need to be added to 'errors/index.js'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, looks like I left that out as well! Will get to this piece tonight otherwise feel free to send a quick PR. 👍

ERR_THEME_ALREADY_ACTIVE,
} from '../../src/errors';

Expand Down Expand Up @@ -152,3 +153,42 @@ describe('themes', () => {
}
});
});

describe('search', () => {
it('should return some search results', async () => {
const mockHttpResponse = {
results: [
{
name: ['foobar-default-theme'],
description: ['Foobar default theme'],
},
{
name: ['foobar-default-plugin'],
description: ['Foobar default plugin'],
},
],
};
require('http').__setMockResponse(JSON.stringify(mockHttpResponse));
expect(await api.search('foobar')).toContainEqual({
name: 'foobar-default-theme',
desc: 'Foobar default theme',
});

expect(await api.search('foobar')).not.toContainEqual({
name: 'invalid-default-theme',
desc: 'Invalid default theme',
});
});

it('should fail to receive search results', async () => {
const mockHttpResponse = {
results: null,
};
require('http').__setMockResponse(JSON.stringify(mockHttpResponse));
try {
await api.search('foobar');
} catch (err) {
expect(err).toBe(ERR_MODULE_SEARCH_FAILED);
}
});
});
11 changes: 5 additions & 6 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ const config = new Conf();
* Lists plugins & themes with the keyword 'dext-plugin' or 'dext-theme' on npm
*
* @param {String} plugin - The name of the plugin/theme
* @return {Promise} - Resolves the search results as an array
* @return {Promise} - Resolves the search results
*/
const search = searchTerm => new Promise((resolve, reject) => {
searchPackages(searchTerm).then((packages) => {
if (Array.isArray(packages)) {
// Loop over all found packages to return a list
const results = packages.map(pkg => pkg.name[0]);
resolve(results);
} else {
if (!Array.isArray(packages) || !packages.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no packages are found for the search it will result in a reject (and an error in the cli) (because of !packages.length) and maybe you just want to reject if the search actually fails.

reject(ERR_MODULE_SEARCH_FAILED);
return;
}

resolve(packages);
});
});

Expand Down
4 changes: 4 additions & 0 deletions src/utils/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const searchPackages = q => new Promise((resolve) => {
});
res.on('end', () => {
const results = JSON.parse(body);
if (!results.results) {
resolve([]);
return;
}
const resultsFlat = results.results.map(c => ({
name: c.name[0],
desc: c.description ? c.description[0] : '',
Expand Down