2 * Copyright 2014 IBM Corp.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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");
28 available: function() { return false; }
31 function loadFlows(testFlows, cb) {
33 getFlows: function() {
34 return when.resolve(testFlows);
36 getCredentials: function() {
37 return when.resolve({});
40 RED.init(settings, storage);
41 flows.load().then(function() {
42 should.deepEqual(testFlows, flows.getFlows());
47 describe('flows', function() {
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() {
59 describe('#each',function() {
60 it('should "visit" all nodes',function(done) {
62 new RedNode({id:'n0'}),
63 new RedNode({id:'n1'})
66 flows.each(function(node) {
67 should.deepEqual(nodes[count], node);
76 describe('#load',function() {
77 it('should load nothing when storage is empty',function(done) {
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(); });
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) {
91 loadFlows([{"id":"n1","type":"debug"}], function() { });
92 events.once('nodes-started', function() {
93 typeRegistryGet.restore();
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(){});
103 loadFlows([{"id":"n2","type":"inject"}], function() {
104 events.emit('type-registered','inject');
106 events.once('nodes-started', function() {
107 typeRegistryGet.restore();
113 describe('#setFlows',function() {
114 it('should save and start an empty tab flow',function(done) {
116 var testFlows = [{"type":"tab","id":"tab1","label":"Sheet 1"}];
118 saveFlows: function(conf) {
119 var defer = when.defer();
121 should.deepEqual(testFlows, conf);
122 return defer.promise;
124 saveCredentials: function (creds) {
128 RED.init(settings, storage);
129 flows.setFlows(testFlows);
130 events.once('nodes-started', function() { done(); });