Skip to content

Commit

Permalink
Returns Tessels from list to API
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed Feb 12, 2016
1 parent 11a884b commit 1c6b38e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 6 deletions.
7 changes: 3 additions & 4 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ Tessel.list = function(opts) {
} else if (foundTessels.length === 1) {
// Close all opened connections and resolve
controller.closeTesselConnections(foundTessels)
.then(resolve);
.then(() => resolve(foundTessels));
}
// If we have only one Tessel or two Tessels with the same name (just USB and LAN)
else if (foundTessels.length === 1 ||
(foundTessels.length === 2 && foundTessels[0].name === foundTessels[1].name)) {
// Close all opened connections and resolve
controller.closeTesselConnections(foundTessels)
.then(resolve);
.then(() => resolve(foundTessels));
}
// Otherwise
else {
Expand All @@ -125,8 +125,7 @@ Tessel.list = function(opts) {
// Helpful instructions on how to switch
logs.info('Set default Tessel with environment variable (e.g. "export TESSEL=bulbasaur") or use the --name flag.');
// Close all opened connections and resolve
controller.closeTesselConnections(foundTessels)
.then(resolve);
controller.closeTesselConnections(foundTessels).then(() => resolve(foundTessels));
});
}
});
Expand Down
110 changes: 108 additions & 2 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,116 @@ exports['API Surface'] = {
ensureExistence: function(test) {
test.ok(api === controller);
test.ok(api === controller);
test.ok(api.commands === commands)
test.ok(api.commands === commands);
test.ok(api.Tessel === Tessel);
test.ok(api.USBConnection === USB.Connection);
test.ok(api.LANConnection === LAN.Connection);
test.done();
}
}
};

exports['CLI.list'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.logsWarn = this.sandbox.stub(logs, 'warn', function() {});
this.logsInfo = this.sandbox.stub(logs, 'info', function() {});
this.logsBasic = this.sandbox.stub(logs, 'basic', function() {});
this.closeConnections = this.sandbox.stub(controller, 'closeTesselConnections').returns(Promise.resolve());
var test = this;
this.seeker = this.sandbox.stub(discover, 'TesselSeeker', function Seeker() {
this.start = (options) => {
test.activeSeeker = this;
setTimeout(() => this.stop(), options.timeout);
return this;
};
this.stop = function() {
this.emit('end');
return this;
};
});

util.inherits(this.seeker, Emitter);
done();
},
tearDown: function(done) {
this.sandbox.restore();
done();
},
rejectWithNoTessels: function(test) {
test.expect(1);
api.list({
timeout: 0.0001,
usb: true
})
.then(() => {
test.fail('Should not have returned any Tessels');
test.done();
})
.catch((err) => {
test.ok(err);
test.done();
});
},
resolveWithOneTessel: function(test) {
// Create a new Tessel
var tessel = new Tessel({
connectionType: 'USB'
});
tessel.name = 'TestTessel';

api.list({
timeout: 0.1,
usb: true
})
.then((tessels) => {
test.ok(tessels.length === 1);
test.ok(tessels[0].name === tessel.name);
test.done();
})
.catch(() => {
test.fail('Should not have rejected with one Tessel available');
test.done();
});

// Emit the Tessel
setImmediate(() => {
this.activeSeeker.emit('tessel', tessel);
});
},

resolveWithMultipleTessels: function(test) {
// Create a new Tessel
var tessel1 = new Tessel({
connectionType: 'USB'
});

tessel1.name = 'TestTessel1';

// Create a new Tessel
var tessel2 = new Tessel({
connectionType: 'LAN'
});

tessel2.name = 'TestTessel2';

api.list({
timeout: 0.1
})
.then((tessels) => {
test.ok(tessels.length === 2);
test.ok(tessels[0].name === tessel1.name);
test.ok(tessels[1].name === tessel2.name);
test.done();
})
.catch(() => {
test.fail('Should not have rejected with one Tessel available');
test.done();
});

// Emit the Tessel
setImmediate(() => {
this.activeSeeker.emit('tessel', tessel1);
this.activeSeeker.emit('tessel', tessel2);
});
}
};

0 comments on commit 1c6b38e

Please sign in to comment.