forked from rianadon/node-red-contrib-sprinkler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
61 lines (54 loc) · 2.29 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const helper = require('node-red-node-test-helper');
const ZoneTimer = require('./src/zone-timer.js');
const Program = require('./src/program.js');
const zone1 = { id: 'n1', type: 'zone-timer', duration: '1s', durationtype: 'str', program: 'progm', wires: [['n2']] };
const zone2 = { ...zone1, id: 'n2', duration: '4s', wires: [['n3']] };
const program = { id: 'progm', type: 'program', name: 'Program A' };
describe('linear execution', function () {
beforeEach(function (done) {
helper.startServer(done);
});
afterEach(function (done) {
helper.unload();
helper.stopServer(done);
});
it('should be loaded', function (done) {
var flow = [{ id: 'n1', type: 'zone-timer', name: 'timer' }];
helper.load(ZoneTimer, flow, function () {
var n1 = helper.getNode('n1');
n1.should.have.property('name', 'timer');
done();
});
});
it('waits for 1 second', function (done) {
const flow = [zone1, { id: 'n2', type: 'helper' }, program];
helper.load([ZoneTimer, Program], flow, function () {
const zone = helper.getNode('n1');
const help = helper.getNode('n2');
const start = Date.now();
help.on('input', function (msg) {
if (Date.now() - start > 900 && Date.now() - start < 1100) {
done();
} else {
done(new Error('Timer did not wait right time: ' + (Date.now() - start)));
}
});
zone.receive({ payload: 'test' });
});
});
it('waits with multiple nodes', function (done) {
const flow = [zone1, zone2, { id: 'n3', type: 'helper' }, program];
helper.load([ZoneTimer, Program], flow, function () {
helper.getNode('progm').resolution = 100; // Scale down timing by factor of 10
const start = Date.now();
helper.getNode('n3').on('input', function (msg) {
if (Date.now() - start > 500 && Date.now() - start < 700) {
done();
} else {
done(new Error('Timer did not wait right time: ' + (Date.now() - start)));
}
});
helper.getNode('n1').receive({ payload: 'test' });
});
});
});