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 2 commits
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
30 changes: 25 additions & 5 deletions __tests__/utils/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@ jest.mock('http');

describe('search', () => {
it('should seach for packages', async () => {
const mockHttpResponse = {results: [{"name":["foobar-default-theme"]},{"name":["foobar-default-plugin"]}]};
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));

const pkg = 'foobar';
const mockResponse = [{"name":["foobar-default-theme"]},{"name":["foobar-default-plugin"]}];
expect(await search.searchPackages(pkg)).toEqual(mockResponse);
const q = 'foobar';
const mockResponse = [
{
name: 'foobar-default-theme',
desc: 'Foobar default theme',
},
{
name: 'foobar-default-plugin',
desc: 'Foobar default plugin',
},
];
expect(await search.searchPackages(q)).toEqual(mockResponse);
});

it('should retrieve the package url', () => {
const pkg = 'foobar';
expect(search.getSearchUrl(pkg)).toEqual('http://npmsearch.com/query\?q\=foobar%20AND%20\(keywords:dext-theme%20OR%20keywords:dext-plugin\)\&fields\=name');
expect(search.getSearchUrl(pkg)).toEqual('https://npmsearch.com/query?q=foobar%20AND%20(keywords:dext-theme%20OR%20keywords:dext-plugin)&fields=name,description');
});
});
6 changes: 3 additions & 3 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const config = new Conf();
* @param {String} plugin - The name of the plugin/theme
* @return {Promise} - Resolves the search results as an array
*/
const search = (searchTerm) => new Promise((resolve, reject) => {
searchPackages(searchTerm).then(packages => {
if(Array.isArray(packages)) {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = {
conf,
download,
paths,
search
search,
};
29 changes: 21 additions & 8 deletions src/utils/search.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
const http = require('http');

const getSearchUrl = pkg => `http://npmsearch.com/query\?q\=${pkg}%20AND%20\(keywords:dext-theme%20OR%20keywords:dext-plugin\)\&fields\=name`;
/**
* Retrieves the API url
*
* @param {String} q - The keyword to search
* @return {String}
*/
const getSearchUrl = q => `https://npmsearch.com/query?q=${q}%20AND%20(keywords:dext-theme%20OR%20keywords:dext-plugin)&fields=name,description`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't work on my machine for some reason...

Error: Protocol "https:" not supported. Expected "http:"
    at new ClientRequest (_http_client.js:55:11)
    at Object.exports.request (http.js:31:10)
    at Object.exports.get (http.js:35:21)
    at Promise (/Users/wolfwood/dev/dext-cli/node_modules/dext-core-utils/src/utils/search.js:23:15)
    at searchPackages (/Users/wolfwood/dev/dext-cli/node_modules/dext-core-utils/src/utils/search.js:19:29)
    at Promise (/Users/wolfwood/dev/dext-cli/node_modules/dext-core-utils/src/api/index.js:29:3)
    at Object.search (/Users/wolfwood/dev/dext-cli/node_modules/dext-core-utils/src/api/index.js:28:30)
    at Args.args.command (/Users/wolfwood/dev/dext-cli/cli.js:12:14)
    at Args.runCommand (/Users/wolfwood/dev/dext-cli/node_modules/args/dist/index.js:327:37)
    at Args.parse (/Users/wolfwood/dev/dext-cli/node_modules/args/dist/index.js:429:12)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah! This is my fault. I've updated the endpoint to the secure protocol. We'll just need to require the https module instead of http. Feel free to send a new PR for this. 👍


/**
* Searches for package marked with the keywords 'dext-plugin' or 'dext-theme'
* Searches for packages marked with the keywords 'dext-plugin' or 'dext-theme'
*
* { name, desc }
*
* @param {String} pkg - The name of the npm package
* @return {Promise} - The search results
* @param {String} q - A keyword to search for (queried by package name)
* @return {Promise} - Resolves an array of the package names and descriptions
*/
const searchPackages = (pkg) => new Promise((resolve) => {
const searchPackages = q => new Promise((resolve) => {
let body = '';
const endpoint = getSearchUrl(q);
// Retrieve the search results
return http.get(getSearchUrl(pkg), (res) => {
return http.get(endpoint, (res) => {
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
const results = JSON.parse(body);
const resultsFlat = results.results.map(c => ({
name: c.name[0],
desc: c.description ? c.description[0] : '',
}));
// Return the results part of the HTTP response
resolve(results.results);
resolve(resultsFlat);
});
});
});

module.exports = {
getSearchUrl,
searchPackages
searchPackages,
};