Skip to content

Commit

Permalink
Tests for most basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Nov 11, 2015
1 parent f0e7b84 commit faf4a4c
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 29 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ Prompt.prototype.search = function(searchTerm) {
//if another search is triggered before the current search finishes, don't set results
if (thisPromise !== self.lastPromise) return;

choices = new Choices(choices);
self.currentChoices = choices;
choices = new Choices(choices.filter(function(choice) {
return choice.type !== 'separator';
}));

self.currentChoices = choices
self.searching = false;
self.render();
});
Expand All @@ -163,7 +166,7 @@ Prompt.prototype.search = function(searchTerm) {


Prompt.prototype.ensureSelectedInRange = function() {
var selectedIndex = Math.min(this.selected, this.currentChoices.length - 1); //not above currentChoices length - 1
var selectedIndex = Math.min(this.selected, this.currentChoices.length); //not above currentChoices length - 1
this.selected = Math.max(selectedIndex, 0); //not below 0
}

Expand All @@ -188,7 +191,6 @@ Prompt.prototype.onKeypress = function(e) {
this.render();
} else {
this.render(); //render input automatically

//Only search if input have actually changed, not because of other keypresses
if (this.lastSearchTerm !== this.rl.line) {
this.search(this.rl.line); //trigger new search
Expand Down
11 changes: 0 additions & 11 deletions test/helpers/fixtures.js

This file was deleted.

6 changes: 4 additions & 2 deletions test/helpers/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var sinon = require('sinon');
var util = require('util');
var _ = require('lodash');

var stub = {
var stub = {};

_.assign(stub, {
write: sinon.stub().returns(stub),
moveCursor: sinon.stub().returns(stub),
setPrompt: sinon.stub().returns(stub),
Expand All @@ -23,7 +25,7 @@ var stub = {
this.__raw__ += str;
}
}
};
});

var ReadlineStub = function() {
this.line = '';
Expand Down
181 changes: 169 additions & 12 deletions test/spec/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var expect = require('chai').expect;
var sinon = require('sinon');
var _ = require('lodash');
var Promise = require('bluebird');
var fixtures = require('../helpers/fixtures');
var inquirer = require('inquirer');
var ReadlineStub = require('../helpers/readline');
var Prompt = require('../../index');

Expand All @@ -12,21 +12,23 @@ describe('inquirer-autocomplete-prompt', function() {
var resolve;
var reject;
var promise;
var rl;
var defaultChoices;

beforeEach(function() {
defaultChoices = ['foo', new inquirer.Separator(), 'bar', 'bum'];
promise = new Promise(function(res, rej) {
resolve = res;
reject = rej;
});
source = sinon.stub().returns(promise)
source = sinon.stub().returns(promise);

this.fixture = _.clone(fixtures.autocomplete);
this.rl = new ReadlineStub();
rl = new ReadlineStub();
prompt = new Prompt({
message: 'test',
name: 'name',
source: source
}, this.rl);
}, rl);
});

it('requires a name', function() {
Expand Down Expand Up @@ -65,21 +67,176 @@ describe('inquirer-autocomplete-prompt', function() {
sinon.assert.calledWithExactly(source, undefined, null);
});

describe('when it has searched', function() {
describe('when it has some results', function() {
var promiseForAnswer;
beforeEach(function() {
promiseForAnswer = getPromiseForAnswer();
resolve(defaultChoices);
return promise;
});

it.skip('should move selected cursor on keypress', function(done) {
prompt.run(function(answer) {
it('should move selected cursor on keypress', function() {
moveDown();
enter();

return promiseForAnswer.then(function(answer) {
expect(answer).to.equal('bar');
})
});

it('moves up and down', function() {
moveDown();
moveDown();
moveUp();
enter();

return promiseForAnswer.then(function(answer) {
expect(answer).to.equal('bar');
done();
})
});

it('loops choices going down', function() {
moveDown();
moveDown();
moveDown();
enter();

return promiseForAnswer.then(function(answer) {
expect(answer).to.equal('foo');
})
});

it('loops choices going up', function() {
moveUp();
enter();

return promiseForAnswer.then(function(answer) {
expect(answer).to.equal('bum');
})
})
});


describe('searching', function() {
beforeEach(function() {
prompt.run();
source.reset();
});

it('searches after each char when user types', function() {
type('a');
sinon.assert.calledWithExactly(source, undefined, 'a');
type('bba');
sinon.assert.calledWithExactly(source, undefined, 'ab');
sinon.assert.calledWithExactly(source, undefined, 'abb');
sinon.assert.calledWithExactly(source, undefined, 'abba');
sinon.assert.callCount(source, 4);
});

it('does not search again if same searchterm (not input added)', function() {
type('ice');
sinon.assert.calledThrice(source);
source.reset();
typeNonChar();
sinon.assert.notCalled(source);
});
});

describe('submit', function() {

describe('without choices', function() {

beforeEach(function() {
prompt.run();
source.reset();
});

it('searches again, since not possible to select something that does not exist', function() {
sinon.assert.notCalled(source);
enter();
sinon.assert.calledOnce(source);
});
});

describe('with choices', function() {
var promiseForAnswer;

this.rl.input.emit('keypress', '', {
name: 'down'
beforeEach(function() {
promiseForAnswer = getPromiseForAnswer();
resolve(defaultChoices);
return promise;
});

this.rl.emit('line');
it('stores the value as the answer and status to answered', function() {
enter();
return promiseForAnswer.then(function(answer) {
expect(answer).to.equal('foo');
expect(prompt.answer).to.equal('foo');
expect(prompt.status).to.equal('answered');
})
});

describe('after selecting', function() {
beforeEach(function() {
enter();
source.reset();
return promiseForAnswer;
});

it('stops searching on typing', function() {
type('test');
sinon.assert.notCalled(source);
});

it('does not change answer on enter', function() {
enter();
sinon.assert.notCalled(source);
return promiseForAnswer.then(function(answer) {
expect(answer).to.equal('foo');
expect(prompt.answer).to.equal('foo');
expect(prompt.status).to.equal('answered');
})
});
});
});

});

function getPromiseForAnswer() {
return new Promise(function(resolve) {
prompt.run(function(answer) {
resolve(answer);
});
});
}

function typeNonChar() {
rl.emit('keypress', '', {
name: 'shift'
});
}

function type(word) {
word.split('').forEach(function(char) {
rl.line = rl.line + char;
rl.emit('keypress', char)
});
}

function moveDown() {
rl.emit('keypress', '', {
name: 'down'
});
}

function moveUp() {
rl.emit('keypress', '', {
name: 'up'
});
}

function enter() {
rl.emit('line');
}

})

0 comments on commit faf4a4c

Please sign in to comment.