-
Notifications
You must be signed in to change notification settings - Fork 324
/
ping_mqtt_test.ts
57 lines (51 loc) · 1.5 KB
/
ping_mqtt_test.ts
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
jest.mock("../index", () => ({
dispatchNetworkDown: jest.fn(),
dispatchNetworkUp: jest.fn(),
dispatchQosStart: jest.fn(),
pingOK: jest.fn(),
pingNO: jest.fn(),
}));
import {
startPinging,
PING_INTERVAL,
sendOutboundPing,
} from "../ping_mqtt";
import { Farmbot } from "farmbot";
import { FarmBotInternalConfig } from "farmbot/dist/config";
import { pingNO } from "../index";
import { DeepPartial } from "../../redux/interfaces";
const state: Partial<FarmBotInternalConfig> = {
LAST_PING_IN: 123,
LAST_PING_OUT: 456
};
function fakeBot(): Farmbot {
const fb: Partial<Farmbot> = {
setConfig: jest.fn(),
publish: jest.fn(),
on: jest.fn(),
ping: jest.fn((_timeout: number, _now: number) => Promise.resolve(1)),
getConfig: jest.fn(key => state[key] as never),
};
return fb as Farmbot;
}
describe("ping util", () => {
it("binds event handlers with startPinging()", async () => {
jest.useFakeTimers();
const bot = fakeBot();
await startPinging(bot);
expect(bot.ping).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(PING_INTERVAL + 10);
expect(bot.ping).toHaveBeenCalledTimes(2);
});
});
describe("sendOutboundPing()", () => {
it("handles failure", async () => {
const fakeBot: DeepPartial<Farmbot> = {
ping: jest.fn(() => Promise.reject())
};
expect(pingNO).not.toHaveBeenCalled();
await expect(sendOutboundPing(fakeBot as Farmbot)).rejects
.toThrow(/sendOutboundPing failed/);
expect(pingNO).toHaveBeenCalled();
});
});