Skip to content

Commit

Permalink
cover new api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
vutran committed Oct 20, 2016
1 parent 285d841 commit 0d1d004
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
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,
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) {
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

0 comments on commit 0d1d004

Please sign in to comment.