Skip to content

Commit

Permalink
tests for hiding underscored transitions from the API and the logs st…
Browse files Browse the repository at this point in the history
…ream.

related to this pull request zettajs/zetta-device#19 on zetta-device.
  • Loading branch information
landlessness committed Mar 24, 2016
1 parent 850bf9a commit 6d6315b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
8 changes: 7 additions & 1 deletion test/fixture/example_driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ TestDriver.prototype.init = function(config) {
.state('ready')
.type('testdriver')
.name('Matt\'s Test Device')
.when('ready', { allow: ['change', 'test', 'error', 'test-number', 'test-text', 'test-none', 'test-date'] })
.when('ready', { allow: ['_ignore-me', 'change', 'test', 'error', 'test-number', 'test-text', 'test-none', 'test-date'] })
.when('changed', { allow: ['prepare', 'test', 'error'] })
.map('_ignore-me', this._ignoreMe)
.map('change', this.change)
.map('prepare', this.prepare)
.map('test', this.test, [{ name: 'value', type: 'number'}])
Expand All @@ -43,6 +44,11 @@ TestDriver.prototype.test = function(value, cb) {
cb();
};

TestDriver.prototype._ignoreMe = function(cb) {
this.state = 'changed';
cb();
};

TestDriver.prototype.change = function(cb) {
this.state = 'changed';
cb();
Expand Down
19 changes: 19 additions & 0 deletions test/test_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,25 @@ describe('Zetta Api', function() {
.end(done);
});


it('should not show _underscore transition in API but it should be available in JS', function(done) {
// assert that change and _ignore-me are available in current state
assert(device.available('_ignore-me'));
assert(device.available('change'));
request(getHttpServer(app))
.get(url)
.expect(getBody(function(res, body) {
assert(body.actions, 7);
// but while change is in the API
assert.equal(body.actions[0].name, 'change');
for (i = 0; i < body.actions.length; i++) {
// _ignore-me is not in the API
assert.notEqual(body.actions[i].name, '_ignore-me');
}
}))
.end(done);
});

it('device should have action change', function(done) {
request(getHttpServer(app))
.get(url)
Expand Down
66 changes: 66 additions & 0 deletions test/test_driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ describe('Driver', function() {
});
});

it('should change the state from ready to changed when calling hidden underscored _ignore-me.', function(done) {
machine.call('_ignore-me', function() {
assert.equal(machine.state, 'changed');
done();
});
});

it('should be able to call transiton afterchange after change was called', function(done) {
machine.call('change', function() {
assert.equal(machine.state, 'changed');
Expand All @@ -129,6 +136,16 @@ describe('Driver', function() {
});
});

it('should be able to call transiton afterchange after hidden underscored _ignore-me was called', function(done) {
machine.call('_ignore-me', function() {
assert.equal(machine.state, 'changed');
machine.call('prepare', function(err) {
assert.equal(machine.state, 'ready');
done();
});
});
});

it('should not throw an error when a disallowed transition tries to happen.', function(done) {
assert.doesNotThrow(function(){
machine.call('change', function() {
Expand Down Expand Up @@ -191,6 +208,44 @@ describe('Driver', function() {
});
});

it('should not publish hidden underscored transitions to pubsub', function(done) {
var topic = machine.type + '/' + machine.id + '/logs';

var recv = 0;
pubsub.subscribe(topic, function(topic, msg) {
assert.ok(msg.timestamp);
assert.ok(msg.topic);
assert.ok(!msg.data);
assert.ok(msg.properties);
assert.ok(msg.input);
assert.ok(msg.transition);
recv++;
});
machine.call('_ignore-me');
setImmediate(function() {
assert.equal(recv, 0);
done();
});
});

it('should not publish hidden underscored transitions to logs', function(done) {
var recv = 0;
pubsub.subscribe('logs', function(topic, msg) {
assert.ok(msg.timestamp);
assert.ok(msg.topic);
assert.ok(!msg.data);
assert.ok(msg.properties);
assert.ok(msg.input);
assert.ok(msg.transition);
recv++;
});
machine.call('_ignore-me');
setImmediate(function() {
assert.equal(recv, 0);
done();
});
});

it('transitionsAvailable should return proper transitions', function() {
//.when('ready', { allow: ['change', 'test'] })
//.when('changed', { allow: ['prepare', 'test'] })
Expand All @@ -206,6 +261,17 @@ describe('Driver', function() {
assert(Object.keys(transitions).indexOf('test') > -1);

});

it('transitionsAvailable should hide transitions that begin with an underscore', function() {
//.when('ready', { allow: ['_ignore-me', 'change', 'test'] })
//.when('changed', { allow: ['prepare', 'test'] })

machine.state = 'ready';
var transitions = machine.transitionsAvailable();
assert.equal(Object.keys(transitions).indexOf('_ignore-me'), -1);

});

});

describe('Monitors', function(){
Expand Down

0 comments on commit 6d6315b

Please sign in to comment.