Tests for connect_device.ts
parent
9517905f5c
commit
36f24974fe
|
@ -5,6 +5,7 @@ def same_thing
|
||||||
sh "yarn install"
|
sh "yarn install"
|
||||||
sh "rails db:migrate"
|
sh "rails db:migrate"
|
||||||
end
|
end
|
||||||
|
|
||||||
namespace :api do
|
namespace :api do
|
||||||
desc "Run Webpack and Rails"
|
desc "Run Webpack and Rails"
|
||||||
task start: :environment do
|
task start: :environment do
|
||||||
|
|
|
@ -6,7 +6,8 @@ jest.mock("farmbot-toastr", () => ({
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock("../../index", () => ({
|
jest.mock("../../index", () => ({
|
||||||
dispatchNetworkUp: jest.fn()
|
dispatchNetworkUp: jest.fn(),
|
||||||
|
dispatchNetworkDown: jest.fn()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const mockDevice = {
|
const mockDevice = {
|
||||||
|
@ -25,13 +26,17 @@ import {
|
||||||
TITLE,
|
TITLE,
|
||||||
bothUp,
|
bothUp,
|
||||||
initLog,
|
initLog,
|
||||||
readStatus
|
readStatus,
|
||||||
|
onOffline,
|
||||||
|
changeLastClientConnected,
|
||||||
|
onSent,
|
||||||
|
onOnline
|
||||||
} from "../../connect_device";
|
} from "../../connect_device";
|
||||||
import { Actions } from "../../../constants";
|
import { Actions, Content } from "../../../constants";
|
||||||
import { Log } from "../../../interfaces";
|
import { Log } from "../../../interfaces";
|
||||||
import { ALLOWED_CHANNEL_NAMES, ALLOWED_MESSAGE_TYPES } from "farmbot";
|
import { ALLOWED_CHANNEL_NAMES, ALLOWED_MESSAGE_TYPES, Farmbot } from "farmbot";
|
||||||
import { success, error, info } from "farmbot-toastr";
|
import { success, error, info } from "farmbot-toastr";
|
||||||
import { dispatchNetworkUp } from "../../index";
|
import { dispatchNetworkUp, dispatchNetworkDown } from "../../index";
|
||||||
import { getDevice } from "../../../device";
|
import { getDevice } from "../../../device";
|
||||||
|
|
||||||
describe("readStatus()", () => {
|
describe("readStatus()", () => {
|
||||||
|
@ -120,3 +125,45 @@ describe("bothUp()", () => {
|
||||||
expect(dispatchNetworkUp).toHaveBeenCalledWith("bot.mqtt");
|
expect(dispatchNetworkUp).toHaveBeenCalledWith("bot.mqtt");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("onOffline", () => {
|
||||||
|
it("tells the app MQTT is down", () => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
onOffline();
|
||||||
|
expect(dispatchNetworkDown).toHaveBeenCalledWith("user.mqtt");
|
||||||
|
expect(error).toHaveBeenCalledWith(Content.MQTT_DISCONNECTED);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("onOnline", () => {
|
||||||
|
it("tells the app MQTT is up", () => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
onOnline();
|
||||||
|
expect(dispatchNetworkUp).toHaveBeenCalledWith("user.mqtt");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("changeLastClientConnected", () => {
|
||||||
|
it("tells farmbot when the last browser session was opened", () => {
|
||||||
|
const setUserEnv = jest.fn();
|
||||||
|
const fakeFarmbot = { setUserEnv: setUserEnv as any } as Farmbot;
|
||||||
|
changeLastClientConnected(fakeFarmbot)();
|
||||||
|
expect(fakeFarmbot.setUserEnv)
|
||||||
|
.toHaveBeenCalled();
|
||||||
|
expect(Object.keys(setUserEnv.mock.calls[0][0]))
|
||||||
|
.toContain("LAST_CLIENT_CONNECTED");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("onSent", () => {
|
||||||
|
it("marks MQTT as up", () => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
onSent({ connected: true })();
|
||||||
|
expect(dispatchNetworkUp).toHaveBeenCalledWith("user.mqtt");
|
||||||
|
});
|
||||||
|
it("marks MQTT as down", () => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
onSent({ connected: false })();
|
||||||
|
expect(dispatchNetworkDown).toHaveBeenCalledWith("user.mqtt");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -80,12 +80,12 @@ export function readStatus() {
|
||||||
.then(() => { commandOK(noun); }, () => { });
|
.then(() => { commandOK(noun); }, () => { });
|
||||||
}
|
}
|
||||||
|
|
||||||
const onOffline = () => {
|
export const onOffline = () => {
|
||||||
dispatchNetworkDown("user.mqtt");
|
dispatchNetworkDown("user.mqtt");
|
||||||
error(t(Content.MQTT_DISCONNECTED));
|
error(t(Content.MQTT_DISCONNECTED));
|
||||||
};
|
};
|
||||||
|
|
||||||
const changeLastClientConnected = (bot: Farmbot) => () => {
|
export const changeLastClientConnected = (bot: Farmbot) => () => {
|
||||||
bot.setUserEnv({
|
bot.setUserEnv({
|
||||||
"LAST_CLIENT_CONNECTED": JSON.stringify(new Date())
|
"LAST_CLIENT_CONNECTED": JSON.stringify(new Date())
|
||||||
});
|
});
|
||||||
|
@ -106,12 +106,10 @@ const onStatus = (dispatch: Function, getState: GetState) =>
|
||||||
}
|
}
|
||||||
}, 500));
|
}, 500));
|
||||||
|
|
||||||
const onSent = (/** The MQTT Client Object (bot.client) */ client: {}) =>
|
type Client = { connected?: boolean };
|
||||||
(any: {}) => {
|
|
||||||
const theValue = get(client, "connected", false);
|
export const onSent = (client: Client) => () => !!client.connected ?
|
||||||
theValue ?
|
dispatchNetworkUp("user.mqtt") : dispatchNetworkDown("user.mqtt");
|
||||||
dispatchNetworkUp("user.mqtt") : dispatchNetworkDown("user.mqtt");
|
|
||||||
};
|
|
||||||
|
|
||||||
const onLogs = (dispatch: Function) => (msg: Log) => {
|
const onLogs = (dispatch: Function) => (msg: Log) => {
|
||||||
bothUp();
|
bothUp();
|
||||||
|
@ -137,7 +135,8 @@ function onMalformed() {
|
||||||
HACKY_FLAGS.alreadyToldUserAboutMalformedMsg = true;
|
HACKY_FLAGS.alreadyToldUserAboutMalformedMsg = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const onOnline = () => dispatchNetworkUp("user.mqtt");
|
|
||||||
|
export const onOnline = () => dispatchNetworkUp("user.mqtt");
|
||||||
|
|
||||||
const attachEventListeners =
|
const attachEventListeners =
|
||||||
(bot: Farmbot, dispatch: Function, getState: GetState) => {
|
(bot: Farmbot, dispatch: Function, getState: GetState) => {
|
||||||
|
|
Loading…
Reference in New Issue