Small changes to the lookup and search commands and improve its tests.

This commit is contained in:
André Cruz
2013-02-18 01:39:05 +00:00
parent e7299b1125
commit e32f3de9b5
5 changed files with 136 additions and 88 deletions

View File

@@ -22,25 +22,30 @@ module.exports = function (name) {
source.lookup(name, function (err, url) {
if (err) {
emitter.emit('package', []);
source.search(name, function (err, packages) {
if (packages.length) {
template('suggestions', {packages: packages, name: name})
.on('data', emitter.emit.bind(emitter, 'data'));
template('suggestions', { packages: packages, name: name })
.on('data', function (data) {
emitter.emit('data', data);
emitter.emit('end');
});
} else {
template('warning-missing', {name: name})
.on('data', emitter.emit.bind(emitter, 'data'));
.on('data', function (data) {
emitter.emit('data', data);
emitter.emit('end');
});
}
});
} else {
var result = {name: name, url: url};
var result = { name: name, url: url };
emitter.emit('package', result);
template('lookup', result)
.on('data', emitter.emit.bind(emitter, 'data'));
.on('data', function (data) {
emitter.emit('data', data);
emitter.emit('end');
});
}
});

View File

@@ -27,10 +27,16 @@ module.exports = function (name) {
if (results.length) {
template('search', {results: results})
.on('data', emitter.emit.bind(emitter, 'data'));
.on('data', function (data) {
emitter.emit('data', data);
emitter.emit('end');
});
} else {
template('search-empty', {results: results})
.on('data', emitter.emit.bind(emitter, 'data'));
.on('data', function (data) {
emitter.emit('data', data);
emitter.emit('end');
});
}
};

View File

@@ -1,77 +0,0 @@
var assert = require('assert');
var commands = require('../lib').commands;
var nock = require('nock');
describe('command', function() {
describe('search', function() {
it('Should emit a packages event for search when nothing is found', function(next) {
nock('https://bower.herokuapp.com')
.get('/packages/search/asdf')
.reply(200, {});
commands.search('asdf', {}).on('packages', function(packages) {
assert.deepEqual([], packages);
next();
});
});
it('Should emit a packages event for search when something is found', function(next) {
var expected = [
{ name: 'fawagahds-mobile',
url: 'git://github.com/strongbad/fawagahds-mobile.js',
endpoint: undefined
}
];
nock('https://bower.herokuapp.com')
.get('/packages/search/fawagahds')
.reply(200, expected);
commands.search('fawagahds', {}).on('packages', function(packages) {
assert.deepEqual(packages, expected);
next();
});
});
afterEach(function() {
nock.cleanAll();
});
});
describe('lookup', function() {
it('Should emit a package event for lookup when nothing is found', function(next) {
nock('https://bower.herokuapp.com')
.get('/packages/asdf')
.reply(404);
commands.lookup('asdf', {}).on('package', function(package) {
assert.deepEqual([], package);
next();
});
});
it('Should emit a package event for lookup when something is found', function(next) {
var expected = {
name: 'fawagahds-mobile',
url: 'git://github.com/strongbad/fawagahds-mobile.js'
};
nock('https://bower.herokuapp.com')
.get('/packages/fawagahds-mobile')
.reply(200, expected);
commands.lookup('fawagahds-mobile', {}).on('package', function(package) {
assert.deepEqual(package, expected);
next();
});
afterEach(function() {
nock.cleanAll();
});
});
});
});

59
test/lookup.js Normal file
View File

@@ -0,0 +1,59 @@
var assert = require('assert');
var lookup = require('../lib/commands/lookup');
var nock = require('nock');
describe('lookup', function () {
afterEach(function () {
nock.cleanAll();
});
it('Should have line method', function () {
assert(!!lookup.line);
});
it('Should emit end event', function (next) {
nock('https://bower.herokuapp.com')
.get('/packages/asdf')
.reply(404);
lookup('asdf')
.on('end', function () {
next();
});
});
it('Should not emit a package event for lookup when nothing is found', function (next) {
var ok = true;
nock('https://bower.herokuapp.com')
.get('/packages/asdf')
.reply(404);
lookup('asdf')
.on('package', function () {
ok = false;
})
.on('end', function () {
assert(ok === true);
next();
});
});
it('Should emit a package event for lookup when something is found', function (next) {
var expected = {
name: 'fawagahds-mobile',
url: 'git://github.com/strongbad/fawagahds-mobile.js'
};
nock('https://bower.herokuapp.com')
.get('/packages/fawagahds-mobile')
.reply(200, expected);
lookup('fawagahds-mobile', {}).on('package', function (pkg) {
assert.deepEqual(pkg, expected);
next();
});
});
});

55
test/search.js Normal file
View File

@@ -0,0 +1,55 @@
var assert = require('assert');
var search = require('../lib/commands/search');
var nock = require('nock');
describe('search', function () {
afterEach(function () {
nock.cleanAll();
});
it('Should have line method', function () {
assert(!!search.line);
});
it('Should emit end event', function (next) {
nock('https://bower.herokuapp.com')
.get('/packages/search/asdf')
.reply(200, {});
search('asdf')
.on('end', function () {
next();
});
});
it('Should emit a packages event for search when nothing is found', function (next) {
nock('https://bower.herokuapp.com')
.get('/packages/search/asdf')
.reply(200, {});
search('asdf').on('packages', function (packages) {
assert.deepEqual([], packages);
next();
});
});
it('Should emit a packages event for search when something is found', function (next) {
var expected = [
{ name: 'fawagahds-mobile',
url: 'git://github.com/strongbad/fawagahds-mobile.js',
endpoint: undefined
}
];
nock('https://bower.herokuapp.com')
.get('/packages/search/fawagahds')
.reply(200, expected);
search('fawagahds').on('packages', function (packages) {
assert.deepEqual(packages, expected);
next();
});
});
});