Tests for sendOutboundPing()

pull/1451/head
Rick Carlino 2019-09-18 09:51:22 -05:00
parent 13bf921d07
commit c1b5de325e
2 changed files with 44 additions and 17 deletions

View File

@ -24,27 +24,31 @@ export function readPing(bot: Farmbot, direction: Direction): number | undefined
}
export function sendOutboundPing(bot: Farmbot) {
const id = uuid();
return new Promise((resolve, reject) => {
const id = uuid();
const x = { done: false };
const x = { done: false };
const ok = () => {
if (!x.done) {
x.done = true;
pingOK(id, now());
}
};
const ok = () => {
if (!x.done) {
x.done = true;
pingOK(id, now());
resolve();
}
};
const no = () => {
if (!x.done) {
x.done = true;
pingNO(id, now());
}
};
const no = () => {
if (!x.done) {
x.done = true;
pingNO(id, now());
reject();
}
};
dispatchQosStart(id);
setTimeout(no, PING_INTERVAL + 150);
bot.ping().then(ok, no);
dispatchQosStart(id);
setTimeout(no, PING_INTERVAL + 150);
bot.ping().then(ok, no);
});
}
export function startPinging(bot: Farmbot) {

View File

@ -0,0 +1,23 @@
jest.mock("../../../connectivity", () => {
return {
pingNO: jest.fn(),
dispatchQosStart: jest.fn()
};
});
import { sendOutboundPing } from "../../../connectivity/ping_mqtt";
import { DeepPartial } from "redux";
import { Farmbot } from "farmbot";
import { pingNO } from "../../../connectivity";
describe("sendOutboundPing()", () => {
it("handles failure", (done) => {
const fakeBot: DeepPartial<Farmbot> = {
ping: jest.fn(() => Promise.reject())
};
expect(pingNO).not.toHaveBeenCalled();
sendOutboundPing(fakeBot as Farmbot).then(fail, () => {
expect(pingNO).toHaveBeenCalled();
done();
});
});
});