Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to use helper with async/await style programming #28

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ it('should warn if the `somethingGood` prop is falsy', function (done) {
/* ..etc.. */
};
helper.load(FooNode, flow, function () {
var n1 = helper.getNode("n1");
n1.warn.should.be.calledWithExactly('badness');
done();
});
Expand Down Expand Up @@ -289,14 +290,14 @@ For additional test examples taken from the Node-RED core, see the `.js` files s

> *Work in progress.*

### load(testNode, testFlows, testCredentials, cb)
### load(testNode, testFlows[, testCredentials][, cb])

Loads a flow then starts the flow. This function has the following arguments:
Return a promise to load a flow then start the flow. This function has the following arguments:

* testNode: (object|array of objects) Module object of a node to be tested returned by require function. This node will be registered, and can be used in testFlows.
* testFlow: (array of objects) Flow data to test a node. If you want to use flow data exported from Node-RED editor, export the flow to the clipboard and paste the content into your test scripts.
* testCredentials: (object) Optional node credentials.
* cb: (function) Function to call back when testFlows has been started.
* cb: (function) Optional function to call back when testFlows has been started.

### unload()

Expand All @@ -306,9 +307,34 @@ Return promise to stop all flows, clean up test runtime.

Returns a node instance by id in the testFlow. Any node that is defined in testFlows can be retrieved, including any helper node added to the flow.

An extra method is added to the node:

* next(event)
Alternative to on(event, callback) for async/await style programming. Returns promise to find the next event in queue. If there is no events in queue, the promise itself will be queued.

If the event is input, a clone of the message will be returned.

Example:

````javascript
it('should make payload lower case', async function() {
var flow = [
{id:"n1",type:"lower-case",name:"test name",wires:[["n2"]]},
{id:"n2",type:"helper"}
];
await helper.load(lowerNode, flow);
var n2 = helper.getNode("n2");
var n1 = helper.getNode("n1");

n1.receive({payload:"UpperCase"});
let msg = await n2.next("input");
msg.should.have.property('payload', 'uppercase');
});
````

### clearFlows()

Stop all flows.
Return promise to stop all flows.

### request()

Expand All @@ -320,9 +346,9 @@ Example:
helper.request().post('/inject/invalid').expect(404).end(done);
```

### startServer(done)
### startServer([done])

Starts a Node-RED server for testing nodes that depend on http or web sockets endpoints like the debug node.
Return promise to start a Node-RED server for testing nodes that depend on http or web sockets endpoints like the debug node.
To start a Node-RED server before all test cases:

```javascript
Expand All @@ -331,9 +357,9 @@ before(function(done) {
});
```

### stopServer(done)
### stopServer([done])

Stop server. Generally called after unload() complete. For example, to unload a flow then stop a server after each test:
Return promise to stop server. Generally called after unload() complete. For example, to unload a flow then stop a server after each test:

```javascript
afterEach(function(done) {
Expand Down
36 changes: 36 additions & 0 deletions examples/comment_async_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

var should = require("should");
var helper = require("../index.js");
helper.init(require.resolve('node-red'));

var commentNode = require("./nodes/90-comment.js");

describe('comment Node', function() {

afterEach(async function() {
await helper.unload();
});

it('should be loaded', async function() {
var flow = [{id:"n1", type:"comment", name: "comment" }];
await helper.load(commentNode, flow);
var n1 = helper.getNode("n1");
n1.should.have.property('name', 'comment');
});

});
Loading