Tests for connect_device.ts
parent
9517905f5c
commit
36f24974fe
|
@ -5,6 +5,7 @@ def same_thing
|
|||
sh "yarn install"
|
||||
sh "rails db:migrate"
|
||||
end
|
||||
|
||||
namespace :api do
|
||||
desc "Run Webpack and Rails"
|
||||
task start: :environment do
|
||||
|
|
|
@ -6,7 +6,8 @@ jest.mock("farmbot-toastr", () => ({
|
|||
}));
|
||||
|
||||
jest.mock("../../index", () => ({
|
||||
dispatchNetworkUp: jest.fn()
|
||||
dispatchNetworkUp: jest.fn(),
|
||||
dispatchNetworkDown: jest.fn()
|
||||
}));
|
||||
|
||||
const mockDevice = {
|
||||
|
@ -25,13 +26,17 @@ import {
|
|||
TITLE,
|
||||
bothUp,
|
||||
initLog,
|
||||
readStatus
|
||||
readStatus,
|
||||
onOffline,
|
||||
changeLastClientConnected,
|
||||
onSent,
|
||||
onOnline
|
||||
} from "../../connect_device";
|
||||
import { Actions } from "../../../constants";
|
||||
import { Actions, Content } from "../../../constants";
|
||||
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 { dispatchNetworkUp } from "../../index";
|
||||
import { dispatchNetworkUp, dispatchNetworkDown } from "../../index";
|
||||
import { getDevice } from "../../../device";
|
||||
|
||||
describe("readStatus()", () => {
|
||||
|
@ -120,3 +125,45 @@ describe("bothUp()", () => {
|
|||
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); }, () => { });
|
||||
}
|
||||
|
||||
const onOffline = () => {
|
||||
export const onOffline = () => {
|
||||
dispatchNetworkDown("user.mqtt");
|
||||
error(t(Content.MQTT_DISCONNECTED));
|
||||
};
|
||||
|
||||
const changeLastClientConnected = (bot: Farmbot) => () => {
|
||||
export const changeLastClientConnected = (bot: Farmbot) => () => {
|
||||
bot.setUserEnv({
|
||||
"LAST_CLIENT_CONNECTED": JSON.stringify(new Date())
|
||||
});
|
||||
|
@ -106,12 +106,10 @@ const onStatus = (dispatch: Function, getState: GetState) =>
|
|||
}
|
||||
}, 500));
|
||||
|
||||
const onSent = (/** The MQTT Client Object (bot.client) */ client: {}) =>
|
||||
(any: {}) => {
|
||||
const theValue = get(client, "connected", false);
|
||||
theValue ?
|
||||
dispatchNetworkUp("user.mqtt") : dispatchNetworkDown("user.mqtt");
|
||||
};
|
||||
type Client = { connected?: boolean };
|
||||
|
||||
export const onSent = (client: Client) => () => !!client.connected ?
|
||||
dispatchNetworkUp("user.mqtt") : dispatchNetworkDown("user.mqtt");
|
||||
|
||||
const onLogs = (dispatch: Function) => (msg: Log) => {
|
||||
bothUp();
|
||||
|
@ -137,7 +135,8 @@ function onMalformed() {
|
|||
HACKY_FLAGS.alreadyToldUserAboutMalformedMsg = true;
|
||||
}
|
||||
}
|
||||
const onOnline = () => dispatchNetworkUp("user.mqtt");
|
||||
|
||||
export const onOnline = () => dispatchNetworkUp("user.mqtt");
|
||||
|
||||
const attachEventListeners =
|
||||
(bot: Farmbot, dispatch: Function, getState: GetState) => {
|
||||
|
|
Loading…
Reference in New Issue