From 6d6315b3f965f3292035dbac96d8ae6167f37765 Mon Sep 17 00:00:00 2001 From: landlessness Date: Wed, 23 Mar 2016 22:22:29 -0400 Subject: [PATCH] tests for hiding underscored transitions from the API and the logs stream. related to this pull request https://github.com/zettajs/zetta-device/pull/19 on zetta-device. --- test/fixture/example_driver.js | 8 ++++- test/test_api.js | 19 ++++++++++ test/test_driver.js | 66 ++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/test/fixture/example_driver.js b/test/fixture/example_driver.js index be33215..6eeac66 100644 --- a/test/fixture/example_driver.js +++ b/test/fixture/example_driver.js @@ -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'}]) @@ -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(); diff --git a/test/test_api.js b/test/test_api.js index 88a2ac1..3de46ce 100644 --- a/test/test_api.js +++ b/test/test_api.js @@ -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) diff --git a/test/test_driver.js b/test/test_driver.js index 6e93599..ecd615b 100644 --- a/test/test_driver.js +++ b/test/test_driver.js @@ -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'); @@ -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() { @@ -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'] }) @@ -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(){