Farmbot-Web-App/frontend/devices/components/fbos_settings/__tests__/last_seen_row_test.tsx

77 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-06-07 18:26:12 -06:00
jest.mock("../../../../api/crud", () => ({ refresh: jest.fn() }));
import * as React from "react";
2020-03-13 15:06:40 -06:00
import { LastSeen, LastSeenProps, getLastSeenNumber } from "../last_seen_row";
import { mount } from "enzyme";
2020-03-13 15:06:40 -06:00
import { SpecialStatus } from "farmbot";
2019-04-09 23:17:03 -06:00
import { fakeTimeSettings } from "../../../../__test_support__/fake_time_settings";
2019-06-07 18:26:12 -06:00
import { refresh } from "../../../../api/crud";
2020-03-13 15:06:40 -06:00
import { bot } from "../../../../__test_support__/fake_state/bot";
import { fakeDevice } from "../../../../__test_support__/resource_index_builder";
2018-01-24 17:32:50 -07:00
2019-06-07 18:26:12 -06:00
describe("<LastSeen />", () => {
2020-03-13 15:06:40 -06:00
const fakeProps = (): LastSeenProps => ({
device: fakeDevice(),
botToMqttLastSeen: 0,
2019-06-07 18:26:12 -06:00
dispatch: jest.fn(),
2019-04-09 23:17:03 -06:00
timeSettings: fakeTimeSettings(),
});
2018-01-24 17:32:50 -07:00
it("blinks when loading", () => {
2020-03-13 15:06:40 -06:00
const p = fakeProps();
2018-01-24 17:32:50 -07:00
p.device.specialStatus = SpecialStatus.SAVING;
const wrapper = mount(<LastSeen {...p} />);
expect(wrapper.text()).toContain("Loading");
});
it("tells you the device has never been seen", () => {
2020-03-13 15:06:40 -06:00
const wrapper = mount(<LastSeen {...fakeProps()} />);
2018-01-24 17:32:50 -07:00
expect(wrapper.text()).toContain("network connectivity issue");
});
2019-06-07 18:26:12 -06:00
it("tells you when the device was last seen, no MQTT", () => {
2020-03-13 15:06:40 -06:00
const p = fakeProps();
2018-01-24 17:32:50 -07:00
p.device.body.last_saw_api = "2017-08-07T19:40:01.487Z";
p.botToMqttLastSeen = 0;
2018-11-09 13:43:51 -07:00
const wrapper = mount<LastSeen>(<LastSeen {...p} />);
expect(wrapper.instance().lastSeen).toEqual("2017-08-07T19:40:01.487Z");
});
2019-06-07 18:26:12 -06:00
it("tells you when the device was last seen, latest: API", () => {
2020-03-13 15:06:40 -06:00
const p = fakeProps();
2019-06-07 18:26:12 -06:00
p.device.body.last_saw_api = "2017-08-07T19:40:01.487Z";
p.botToMqttLastSeen = new Date("2016-08-07T19:40:01.487Z").getTime();
2019-06-07 18:26:12 -06:00
const wrapper = mount<LastSeen>(<LastSeen {...p} />);
expect(wrapper.instance().lastSeen).toEqual("2017-08-07T19:40:01.487Z");
});
2018-01-24 17:32:50 -07:00
it("tells you when the device was last seen, latest: message broker", () => {
2020-03-13 15:06:40 -06:00
const p = fakeProps();
2018-01-24 17:32:50 -07:00
p.device.body.last_saw_api = "2017-08-07T19:40:01.487Z";
p.botToMqttLastSeen = new Date("2017-08-07T20:40:01.487Z").getTime();
2018-11-09 13:43:51 -07:00
const wrapper = mount<LastSeen>(<LastSeen {...p} />);
2019-09-05 12:18:18 -06:00
const t = new Date("2017-08-07T20:40:01.487Z").getTime();
expect(wrapper.instance().lastSeen).toEqual(t);
2018-01-24 17:32:50 -07:00
});
it("handles a click", () => {
2020-03-13 15:06:40 -06:00
const p = fakeProps();
2018-01-24 17:32:50 -07:00
const wrapper = mount(<LastSeen {...p} />);
wrapper.find("i").simulate("click");
2019-06-07 18:26:12 -06:00
expect(refresh).toHaveBeenCalled();
2018-01-24 17:32:50 -07:00
});
});
2020-03-13 15:06:40 -06:00
describe("getLastSeenNumber()", () => {
it("returns number: unknown", () => {
const result = getLastSeenNumber(bot);
expect(result).toEqual(NaN);
});
it("returns number: known", () => {
bot.connectivity.uptime["bot.mqtt"] = { state: "up", at: 0 };
const result = getLastSeenNumber(bot);
expect(result).toEqual(0);
});
});