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

182 lines
6.2 KiB
TypeScript
Raw Normal View History

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";
2020-02-26 11:10:59 -07:00
import { mount, shallow } from "enzyme";
2019-09-19 13:09:00 -06:00
import { RawLogs as 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";
2019-02-26 20:12:02 -07:00
import { MessageType } from "../../sequences/interfaces";
2019-04-09 23:17:03 -06:00
import { fakeTimeSettings } from "../../__test_support__/fake_time_settings";
2020-04-13 19:15:11 -06:00
import { SearchField } from "../../ui/search_field";
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";
2019-02-26 20:12:02 -07:00
log2.body.type = MessageType.success;
2018-05-17 14:12:33 -06:00
return [log1, log2];
2017-11-07 18:16:38 -07:00
}
2019-04-16 11:03:44 -06:00
const fakeProps = (): LogsProps => ({
logs: fakeLogs(),
timeSettings: fakeTimeSettings(),
dispatch: jest.fn(),
sourceFbosConfig: jest.fn(),
getConfigValue: x => mockStorj[x],
2019-08-23 15:19:22 -06:00
shouldDisplay: () => false,
2019-04-16 11:03:44 -06:00
});
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} />);
2019-09-17 10:18:32 -06:00
wrapper.setState({ markdown: false });
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;
2019-02-26 20:12:02 -07:00
p.logs[0].body.type = MessageType.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
});
2019-08-26 16:08:23 -06:00
const fakeLogsState = () => ({
assertion: 3,
busy: 3,
debug: 3,
error: 3,
fun: 3,
info: 3,
success: 3,
warn: 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()} />);
2019-08-26 16:08:23 -06:00
wrapper.setState(fakeLogsState());
const filterBtn = wrapper.find("button").first();
expect(filterBtn.text().toLowerCase()).toEqual("filter");
expect(filterBtn.hasClass("gray")).toBeTruthy();
});
it("shows filtered overall filter status", () => {
const p = fakeProps();
const wrapper = mount(<Logs {...p} />);
const state = fakeLogsState();
state.assertion = 2;
wrapper.setState(state);
const filterBtn = wrapper.find("button").first();
expect(filterBtn.text().toLowerCase()).toEqual("filters active");
expect(filterBtn.hasClass("green")).toBeTruthy();
});
it("shows unfiltered overall filter status", () => {
const p = fakeProps();
const wrapper = mount(<Logs {...p} />);
const state = fakeLogsState();
2020-04-23 13:11:25 -06:00
state.assertion = 3;
2019-08-26 16:08:23 -06:00
wrapper.setState(state);
2017-12-11 04:50:41 -07:00
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);
2019-02-26 20:12:02 -07:00
wrapper.instance().toggle(MessageType.warn)();
expect(wrapper.instance().state.warn).toEqual(0);
2019-02-26 20:12:02 -07:00
wrapper.instance().toggle(MessageType.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);
2019-02-26 20:12:02 -07:00
wrapper.instance().setFilterLevel(MessageType.warn)(2);
expect(wrapper.instance().state.warn).toEqual(2);
2017-12-02 01:40:29 -07:00
});
2019-09-16 15:22:20 -06:00
it("toggles raw text display", () => {
const wrapper = mount<Logs>(<Logs {...fakeProps()} />);
expect(wrapper.state().markdown).toBeTruthy();
2019-09-17 09:53:18 -06:00
wrapper.find(".fa-stack").simulate("click");
expect(wrapper.state().markdown).toBeFalsy();
2019-09-16 15:22:20 -06:00
});
it("renders formatted messages", () => {
const p = fakeProps();
p.logs[0].body.message = "`message`";
const wrapper = mount<Logs>(<Logs {...p} />);
2019-09-17 09:53:18 -06:00
expect(wrapper.state().markdown).toBeTruthy();
2019-09-16 15:22:20 -06:00
expect(wrapper.html()).toContain("<code>message</code>");
2019-09-17 09:53:18 -06:00
wrapper.setState({ markdown: false });
expect(wrapper.html()).not.toContain("<code>message</code>");
2019-09-16 15:22:20 -06:00
});
2020-02-26 11:10:59 -07:00
it("changes search term", () => {
const p = fakeProps();
const wrapper = shallow<Logs>(<Logs {...p} />);
2020-04-13 19:15:11 -06:00
wrapper.find(SearchField).first().simulate("change", "one");
2020-02-26 11:10:59 -07:00
expect(wrapper.state().searchTerm).toEqual("one");
});
2017-11-07 18:16:38 -07:00
});