Chinthakayala, Sheshashailavas (sc2914) | d156997 | 2017-08-28 05:25:46 -0900 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright 2014 IBM Corp. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | **/ |
| 16 | |
| 17 | var should = require("should"); |
| 18 | var sinon = require("sinon"); |
| 19 | var when = require("when"); |
| 20 | var flows = require("../../../red/nodes/flows"); |
| 21 | var RedNode = require("../../../red/nodes/Node"); |
| 22 | var RED = require("../../../red/nodes"); |
| 23 | var events = require("../../../red/events"); |
| 24 | var typeRegistry = require("../../../red/nodes/registry"); |
| 25 | |
| 26 | |
| 27 | var settings = { |
| 28 | available: function() { return false; } |
| 29 | } |
| 30 | |
| 31 | function loadFlows(testFlows, cb) { |
| 32 | var storage = { |
| 33 | getFlows: function() { |
| 34 | return when.resolve(testFlows); |
| 35 | }, |
| 36 | getCredentials: function() { |
| 37 | return when.resolve({}); |
| 38 | } |
| 39 | }; |
| 40 | RED.init(settings, storage); |
| 41 | flows.load().then(function() { |
| 42 | should.deepEqual(testFlows, flows.getFlows()); |
| 43 | cb(); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | describe('flows', function() { |
| 48 | |
| 49 | describe('#add',function() { |
| 50 | it('should be called by node constructor',function(done) { |
| 51 | var n = new RedNode({id:'123',type:'abc'}); |
| 52 | should.deepEqual(n, flows.get("123")); |
| 53 | flows.clear().then(function() { |
| 54 | done(); |
| 55 | }); |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | describe('#each',function() { |
| 60 | it('should "visit" all nodes',function(done) { |
| 61 | var nodes = [ |
| 62 | new RedNode({id:'n0'}), |
| 63 | new RedNode({id:'n1'}) |
| 64 | ]; |
| 65 | var count = 0; |
| 66 | flows.each(function(node) { |
| 67 | should.deepEqual(nodes[count], node); |
| 68 | count += 1; |
| 69 | if (count == 2) { |
| 70 | done(); |
| 71 | } |
| 72 | }); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | describe('#load',function() { |
| 77 | it('should load nothing when storage is empty',function(done) { |
| 78 | loadFlows([], done); |
| 79 | }); |
| 80 | |
| 81 | it('should load and start an empty tab flow',function(done) { |
| 82 | loadFlows([{"type":"tab","id":"tab1","label":"Sheet 1"}], function() {}); |
| 83 | events.once('nodes-started', function() { done(); }); |
| 84 | }); |
| 85 | |
| 86 | it('should load and start a registered node type', function(done) { |
| 87 | RED.registerType('debug', function() {}); |
| 88 | var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) { |
| 89 | return function() {}; |
| 90 | }); |
| 91 | loadFlows([{"id":"n1","type":"debug"}], function() { }); |
| 92 | events.once('nodes-started', function() { |
| 93 | typeRegistryGet.restore(); |
| 94 | done(); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | it('should load and start when node type is registered', function(done) { |
| 99 | var typeRegistryGet = sinon.stub(typeRegistry,"get"); |
| 100 | typeRegistryGet.onCall(0).returns(null); |
| 101 | typeRegistryGet.returns(function(){}); |
| 102 | |
| 103 | loadFlows([{"id":"n2","type":"inject"}], function() { |
| 104 | events.emit('type-registered','inject'); |
| 105 | }); |
| 106 | events.once('nodes-started', function() { |
| 107 | typeRegistryGet.restore(); |
| 108 | done(); |
| 109 | }); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe('#setFlows',function() { |
| 114 | it('should save and start an empty tab flow',function(done) { |
| 115 | var saved = 0; |
| 116 | var testFlows = [{"type":"tab","id":"tab1","label":"Sheet 1"}]; |
| 117 | var storage = { |
| 118 | saveFlows: function(conf) { |
| 119 | var defer = when.defer(); |
| 120 | defer.resolve(); |
| 121 | should.deepEqual(testFlows, conf); |
| 122 | return defer.promise; |
| 123 | }, |
| 124 | saveCredentials: function (creds) { |
| 125 | return when(true); |
| 126 | } |
| 127 | }; |
| 128 | RED.init(settings, storage); |
| 129 | flows.setFlows(testFlows); |
| 130 | events.once('nodes-started', function() { done(); }); |
| 131 | }); |
| 132 | }); |
| 133 | |
| 134 | }); |