Farmbot-Web-App/frontend/logs/__tests__/index_test.tsx

128 lines
4.2 KiB
TypeScript
Raw Normal View History

2017-11-07 18:16:38 -07:00
jest.mock("react-redux", () => ({
connect: jest.fn()
}));
2017-12-11 04:50:41 -07:00
const mockStorj: Dictionary<number | boolean> = {};
2017-11-07 18:16:38 -07:00
import * as React from "react";
import { mount } from "enzyme";
2017-12-01 22:02:18 -07:00
import { Logs } from "../index";
2017-11-08 15:51:46 -07:00
import { ToolTips } from "../../constants";
2018-08-01 18:20:50 -06:00
import { TaggedLog, Dictionary } from "farmbot";
2017-12-11 04:50:41 -07:00
import { NumericSetting } from "../../session_keys";
2018-05-17 14:12:33 -06:00
import { fakeLog } from "../../__test_support__/fake_state/resources";
2018-05-17 15:22:33 -06:00
import { LogsProps } from "../interfaces";
2017-11-07 18:16:38 -07:00
describe("<Logs />", () => {
2017-11-14 15:44:31 -07:00
function fakeLogs(): TaggedLog[] {
2018-05-17 14:12:33 -06:00
const log1 = fakeLog();
log1.body.message = "Fake log message 1";
const log2 = fakeLog();
log2.body.message = "Fake log message 2";
log2.body.type = "success";
return [log1, log2];
2017-11-07 18:16:38 -07:00
}
2018-05-17 15:22:33 -06:00
const fakeProps = (): LogsProps => {
2018-01-27 02:29:13 -07:00
return {
logs: fakeLogs(),
timeOffset: 0,
dispatch: jest.fn(),
2018-08-27 14:09:27 -06:00
sourceFbosConfig: jest.fn(),
getConfigValue: x => mockStorj[x],
2018-01-27 02:29:13 -07:00
};
};
2017-11-07 18:16:38 -07:00
it("renders", () => {
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Logs {...fakeProps()} />);
2017-11-08 15:51:46 -07:00
["Logs", ToolTips.LOGS, "Type", "Message", "Time", "Info",
2017-11-07 18:16:38 -07:00
"Fake log message 1", "Success", "Fake log message 2"]
.map(string =>
expect(wrapper.text().toLowerCase()).toContain(string.toLowerCase()));
2017-11-08 15:51:46 -07:00
const filterBtn = wrapper.find("button").first();
2017-12-11 04:50:41 -07:00
expect(filterBtn.text().toLowerCase()).toEqual("filters active");
expect(filterBtn.hasClass("green")).toBeTruthy();
2017-11-07 18:16:38 -07:00
});
2018-05-17 15:22:33 -06:00
it("shows message when logs are loading", () => {
const p = fakeProps();
p.logs[0].body.message = "";
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Logs {...p} />);
2018-05-17 15:22:33 -06:00
expect(wrapper.text().toLowerCase()).toContain("loading");
});
2017-11-07 18:16:38 -07:00
it("filters logs", () => {
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Logs {...fakeProps()} />);
2017-12-11 04:50:41 -07:00
wrapper.setState({ info: 0 });
2017-11-07 18:16:38 -07:00
expect(wrapper.text()).not.toContain("Fake log message 1");
2017-11-08 15:51:46 -07:00
const filterBtn = wrapper.find("button").first();
expect(filterBtn.text().toLowerCase()).toEqual("filters active");
expect(filterBtn.hasClass("green")).toBeTruthy();
2017-11-07 18:16:38 -07:00
});
2018-05-17 15:22:33 -06:00
it("doesn't show logs of any verbosity when type is disabled", () => {
const p = fakeProps();
p.logs[0].body.verbosity = 0;
const notShownMessage = "This log should not be shown.";
p.logs[0].body.message = notShownMessage;
p.logs[0].body.type = "info";
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Logs {...p} />);
2018-05-17 15:22:33 -06:00
wrapper.setState({ info: 0 });
expect(wrapper.text()).not.toContain(notShownMessage);
});
it("shows position", () => {
2018-01-27 02:29:13 -07:00
const p = fakeProps();
2018-03-31 12:10:36 -06:00
p.logs[0].body.x = 100;
2018-05-17 14:12:33 -06:00
p.logs[0].body.y = undefined;
p.logs[0].body.z = undefined;
2018-03-31 12:10:36 -06:00
p.logs[1].body.x = 0;
p.logs[1].body.y = 1;
p.logs[1].body.z = 2;
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Logs {...p} />);
expect(wrapper.text()).toContain("Unknown");
expect(wrapper.text()).toContain("0, 1, 2");
});
2017-12-02 01:40:29 -07:00
it("shows verbosity", () => {
2018-01-27 02:29:13 -07:00
const p = fakeProps();
2018-03-31 12:10:36 -06:00
p.logs[0].body.verbosity = -999;
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Logs {...p} />);
2017-12-11 04:50:41 -07:00
expect(wrapper.text()).toContain(-999);
});
it("loads filter setting", () => {
mockStorj[NumericSetting.warn_log] = 3;
const wrapper = mount<Logs>(<Logs {...fakeProps()} />);
expect(wrapper.instance().state.warn).toEqual(3);
2017-12-11 04:50:41 -07:00
});
it("shows overall filter status", () => {
2018-07-03 12:21:05 -06:00
const wrapper = mount(<Logs {...fakeProps()} />);
2017-12-11 04:50:41 -07:00
wrapper.setState({
success: 3, busy: 3, warn: 3, error: 3, info: 3, fun: 3, debug: 3
});
const filterBtn = wrapper.find("button").first();
expect(filterBtn.text().toLowerCase()).toEqual("filter");
expect(filterBtn.hasClass("gray")).toBeTruthy();
});
it("toggles filter", () => {
mockStorj[NumericSetting.warn_log] = 3;
const wrapper = mount<Logs>(<Logs {...fakeProps()} />);
expect(wrapper.instance().state.warn).toEqual(3);
2018-11-09 13:43:51 -07:00
wrapper.instance().toggle("warn")();
expect(wrapper.instance().state.warn).toEqual(0);
2018-11-09 13:43:51 -07:00
wrapper.instance().toggle("warn")();
expect(wrapper.instance().state.warn).toEqual(1);
2017-12-11 04:50:41 -07:00
});
it("sets filter", () => {
mockStorj[NumericSetting.warn_log] = 3;
const wrapper = mount<Logs>(<Logs {...fakeProps()} />);
expect(wrapper.instance().state.warn).toEqual(3);
2018-11-09 13:43:51 -07:00
wrapper.instance().setFilterLevel("warn")(2);
expect(wrapper.instance().state.warn).toEqual(2);
2017-12-02 01:40:29 -07:00
});
2017-11-07 18:16:38 -07:00
});