Skip to content

Commit

Permalink
Fail gracefully if no lat and long specified anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Mar 19, 2021
1 parent fc15abb commit 8f4909f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/sun-events-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ module.exports = function(RED) {

node.context().set("msg", msg)

let lat = msg.payload["lat"] || msg.payload["latitude"] || node.credentials.latitude
let lng = msg.payload["lng"] || msg.payload["longitude"] || node.credentials.longitude
let lat = msg.payload["lat"] || msg.payload["latitude"] || (node.credentials ? node.credentials.latitude : null)
let lng = msg.payload["lng"] || msg.payload["longitude"] || (node.credentials ? node.credentials.longitude : null)

if ( !(lat && lng) ) {
node.error("No latitude & longitude in payload, please refer to documentation.")
Expand Down
28 changes: 26 additions & 2 deletions spec/sun-events-node-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('sun-events Node', function () {
done(err)
}
});
for(let day = 0; day < 100; day++) {
for(let day = 0; day < 10; day++) {
n1.receive({ misc: "hello world", payload: {pi: 3.1459, latitude: test_lat, longitude: test_lng }});
jasmine.clock().tick(1000 * 60 * 60 * 24)
}
Expand Down Expand Up @@ -152,7 +152,31 @@ describe('sun-events Node', function () {
expect(n1.error).not.toHaveBeenCalled()
jasmine.clock().tick(1000 * 60 * 60 * 24)
});

})

it('should fail gracefully if no lat or long is supplied', function(done) {
const test_credentials = { "n1": {} }
const flow = [{ id: "n1", type: "sun events", name: "test name", wires: [["n2"]] }, { id: "n2", type: "helper" }];
helper.load(SunEventsNode, flow, test_credentials, function () {
let n1 = helper.getNode("n1");
let n2 = helper.getNode("n2");
expect(n2).toEqual(jasmine.objectContaining({ type: 'helper' })) // Don't forget to pass "done" into the it function !!!
n2.on("input", function (msg) {
try {
fail('no events should be output')
done();
} catch(err) {
done(err)
}
});
spyOn(n1, "error")
spyOn(n2, "on")
n1.receive({ payload: 'some random string' })
expect(n1.error).toHaveBeenCalled()
jasmine.clock().tick(1000 * 60 * 60 * 24)
expect(n2.on).not.toHaveBeenCalled()
done()
});
})

});

0 comments on commit 8f4909f

Please sign in to comment.