Finish tests for qos.ts

pull/1418/head
Rick Carlino 2019-09-07 12:23:13 -05:00
parent f7b645d580
commit cb77c09555
4 changed files with 38 additions and 12 deletions

View File

@ -1,5 +1,4 @@
jest.mock("../util/util", () => ({
shortRevision: jest.fn(() => "ABCD"),
trim: jest.fn((s: unknown) => s),
defensiveClone: jest.fn((s: unknown) => s)
}));
@ -13,7 +12,6 @@ jest.mock("i18next", () => ({ init: jest.fn() }));
jest.mock("../routes", () => ({ attachAppToDom: { mock: "Yeah" } }));
import { stopIE } from "../util/stop_ie";
import { shortRevision } from "../util/util";
import { detectLanguage } from "../i18n";
import I from "i18next";
@ -24,7 +22,6 @@ describe("entry file", () => {
await import("../entry");
expect(stopIE).toHaveBeenCalled();
expect(shortRevision).toHaveBeenCalled();
expect(detectLanguage).toHaveBeenCalled();
expect(I.init).toHaveBeenCalled();
expect(console.log).toHaveBeenCalledWith("ABCD");

View File

@ -41,7 +41,6 @@ export function sendOutboundPing(bot: Farmbot) {
const no = () => markStale(id);
dispatchQosStart(id);
bot.ping().then(ok, no);
// setTimeout(no, PING_INTERVAL);
}
export function startPinging(bot: Farmbot) {

View File

@ -3,7 +3,6 @@ jest.mock("../../util/stop_ie", () => ({
}));
jest.mock("../../util", () => ({
shortRevision: jest.fn(),
attachToRoot: jest.fn()
}));
@ -16,7 +15,7 @@ jest.mock("i18next", () => ({
}));
import { stopIE } from "../../util/stop_ie";
import { shortRevision, attachToRoot } from "../../util";
import { attachToRoot } from "../../util";
import { detectLanguage } from "../../i18n";
import { DemoIframe } from "../demo_iframe";
import I from "i18next";
@ -25,7 +24,6 @@ describe("DemoIframe loader", () => {
it("calls expected callbacks", (done) => {
import("../index").then(() => {
expect(stopIE).toHaveBeenCalled();
expect(shortRevision).toHaveBeenCalled();
expect(detectLanguage).toHaveBeenCalled();
expect(I.init).toHaveBeenCalled();
expect(attachToRoot).toHaveBeenCalledWith(DemoIframe);

View File

@ -1,8 +1,17 @@
import { calculateLatency, calculatePingLoss, completePing, startPing, failPing } from "../qos";
import { fakePings } from "../../../__test_support__/fake_state/pings";
import {
calculateLatency,
calculatePingLoss,
completePing,
startPing,
failPing,
PingDictionary
} from "../qos";
import {
fakePings
} from "../../../__test_support__/fake_state/pings";
describe("QoS helpers", () => {
it("calculateLatency", () => {
it("calculates latency", () => {
const report = calculateLatency({
"a": { kind: "timeout", start: 111, end: 423 },
"b": { kind: "pending", start: 213 },
@ -17,7 +26,15 @@ describe("QoS helpers", () => {
expect(report.total).toEqual(4);
});
it("calculatePingLoss", () => {
it("returns 0 when latency can't be calculated", () => {
const report = calculateLatency({});
expect(report.best).toEqual(0);
expect(report.worst).toEqual(0);
expect(report.average).toEqual(0);
expect(report.total).toEqual(1);
});
it("calculates ping loss", () => {
const report = calculatePingLoss(fakePings());
expect(report.total).toEqual(3);
expect(report.complete).toEqual(1);
@ -25,7 +42,7 @@ describe("QoS helpers", () => {
expect(report.complete).toEqual(1);
});
it("completePing", () => {
it("marks a ping as complete", () => {
const KEY = "b";
const state = fakePings();
const before = state[KEY];
@ -36,6 +53,14 @@ describe("QoS helpers", () => {
expect(after && after.kind).toEqual("complete");
});
it("does not mark pings as complete twice", () => {
const state: PingDictionary = {
"x": { kind: "complete", start: 319, end: 631 },
};
const nextState = completePing(state, "x");
expect(nextState).toBe(state); // No, not "toEqual"
});
it("starts a ping", () => {
const state = fakePings();
const nextState = startPing(state, "x");
@ -52,4 +77,11 @@ describe("QoS helpers", () => {
expect(after && after.kind).toEqual("timeout");
});
it("skips pings that don't need to be failed", () => {
const state: PingDictionary = {
"x": { kind: "complete", start: 319, end: 631 },
};
const nextState = failPing(state, "x");
expect(nextState).toBe(state); // No, not "toEqual"
});
});