diff --git a/ui/src/Components/Labels/FilterInputLabel/index.test.js b/ui/src/Components/Labels/FilterInputLabel/index.test.js index 0f5e19c92..ac0daa404 100644 --- a/ui/src/Components/Labels/FilterInputLabel/index.test.js +++ b/ui/src/Components/Labels/FilterInputLabel/index.test.js @@ -1,5 +1,6 @@ import React from "react"; -import renderer from "react-test-renderer"; + +import { shallow, mount } from "enzyme"; import { AlertStore, NewUnappliedFilter } from "Stores/AlertStore"; @@ -22,7 +23,7 @@ const MockColors = () => { }; }; -const FakeLabel = (matcher, applied) => { +const ShallowLabel = (matcher, applied, valid) => { const name = "foo"; const value = "bar"; const filter = NewUnappliedFilter(`${name}${matcher}${value}`); @@ -30,27 +31,23 @@ const FakeLabel = (matcher, applied) => { filter.name = name; filter.matcher = matcher; filter.value = value; - return renderer.create( - - ); + filter.isValid = valid; + return shallow(); }; const ValidateClass = (matcher, applied, expectedClass) => { - const tree = FakeLabel(matcher, applied).toJSON(); - expect(tree.props.className.split(" ")).toContain(expectedClass); + const tree = ShallowLabel(matcher, applied, true); + expect(tree.props().className.split(" ")).toContain(expectedClass); }; const ValidateOnChange = newRaw => { - const component = renderer.create( + const tree = shallow( ); - const tree = component.toTree(); - - // call onChange with new raw value - tree.instance.onChange({ raw: newRaw }); + tree.instance().onChange({ raw: newRaw }); return tree; }; @@ -92,40 +89,40 @@ describe(" className", () => { describe(" style", () => { it("unapplied filter with color information and '=' matcher should have empty style", () => { MockColors(); - const tree = FakeLabel("=", false).toJSON(); - expect(tree.props.style).toMatchObject({}); + const tree = ShallowLabel("=", false, true); + expect(tree.props().style).toMatchObject({}); }); it("unapplied filter with no color information and '=' matcher should have empty style", () => { - const tree = FakeLabel("=", false).toJSON(); - expect(tree.props.style).toMatchObject({}); + const tree = ShallowLabel("=", false, true); + expect(tree.props().style).toMatchObject({}); }); it("unapplied filter with no color information and any matcher other than '=' should have empty style", () => { for (const matcher of NonEqualMatchers) { - const tree = FakeLabel(matcher, false).toJSON(); - expect(tree.props.style).toMatchObject({}); + const tree = ShallowLabel(matcher, false, true); + expect(tree.props().style).toMatchObject({}); } }); it("applied filter with color information and '=' matcher should have non empty style", () => { MockColors(); - const tree = FakeLabel("=", true).toJSON(); - expect(tree.props.style).toMatchObject({ + const tree = ShallowLabel("=", true, true); + expect(tree.props().style).toMatchObject({ color: "rgba(1, 2, 3, 100)", backgroundColor: "rgba(4, 5, 6, 200)" }); }); it("applied filter with no color information and '=' matcher should have empty style", () => { - const tree = FakeLabel("=", true).toJSON(); - expect(tree.props.style).toMatchObject({}); + const tree = ShallowLabel("=", true, true); + expect(tree.props().style).toMatchObject({}); }); it("applied filter with no color information and any matcher other than '=' should have empty style", () => { for (const matcher of NonEqualMatchers) { - const tree = FakeLabel(matcher, true).toJSON(); - expect(tree.props.style).toMatchObject({}); + const tree = ShallowLabel(matcher, true, true); + expect(tree.props().style).toMatchObject({}); } }); }); @@ -168,17 +165,27 @@ describe(" onChange", () => { NewUnappliedFilter("foo=bar"), NewUnappliedFilter("bar=baz") ]; - const component = renderer.create( + const tree = mount( ); - const button = component.root.findByType("button"); - button.props.onClick(); + const button = tree.find("button"); + button.simulate("click"); expect(alertStore.filters.values).toHaveLength(1); expect(alertStore.filters.values).toContainEqual( NewUnappliedFilter("bar=baz") ); }); }); + +describe(" render", () => { + it("invalid filter matches snapshot", () => { + const tree = ShallowLabel("=", true, false); + const errorSpan = tree.find(".text-danger"); + expect(errorSpan).toHaveLength(1); + const errorIcon = errorSpan.find("FontAwesomeIcon"); + expect(errorIcon).toHaveLength(1); + }); +}); diff --git a/ui/src/Components/MainModal/index.test.js b/ui/src/Components/MainModal/index.test.js index 79b6e5ee6..fc0a699d6 100644 --- a/ui/src/Components/MainModal/index.test.js +++ b/ui/src/Components/MainModal/index.test.js @@ -1,6 +1,6 @@ import React from "react"; -import { shallow } from "enzyme"; +import { shallow, mount } from "enzyme"; import { AlertStore } from "Stores/AlertStore"; import { Settings } from "Stores/Settings"; @@ -14,27 +14,33 @@ beforeEach(() => { settingsStore = new Settings(); }); -const RenderMainModal = () => { +const ShallowMainModal = () => { return shallow( ); }; +const MountedMainModal = () => { + return mount( + + ); +}; + describe("", () => { it("only renders FontAwesomeIcon when modal is not shown", () => { - const tree = RenderMainModal(); + const tree = ShallowMainModal(); expect(tree.text()).toBe(""); }); it("renders the modal when it is shown", () => { - const tree = RenderMainModal(); + const tree = ShallowMainModal(); const toggle = tree.find(".nav-link"); toggle.simulate("click"); expect(tree.text()).toBe(""); }); it("hides the modal when toggle() is called twice", () => { - const tree = RenderMainModal(); + const tree = ShallowMainModal(); const toggle = tree.find(".nav-link"); toggle.simulate("click"); toggle.simulate("click"); @@ -42,7 +48,7 @@ describe("", () => { }); it("hides the modal when hide() is called", () => { - const tree = RenderMainModal(); + const tree = ShallowMainModal(); const toggle = tree.find(".nav-link"); toggle.simulate("click"); expect(tree.text()).toBe(""); @@ -50,4 +56,27 @@ describe("", () => { instance.toggle.hide(); expect(tree.text()).toBe(""); }); + + it("'modal-open' class is appended to body node when modal is visible", () => { + const tree = MountedMainModal(); + const toggle = tree.find(".nav-link"); + toggle.simulate("click"); + expect(document.body.className.split(" ")).toContain("modal-open"); + }); + + it("'modal-open' class is removed from body node after modal is hidden", () => { + const tree = MountedMainModal(); + const toggle = tree.find(".nav-link"); + toggle.simulate("click"); + toggle.simulate("click"); + expect(document.body.className.split(" ")).not.toContain("modal-open"); + }); + + it("'modal-open' class is removed from body node after modal is unmounted", () => { + const tree = MountedMainModal(); + const toggle = tree.find(".nav-link"); + toggle.simulate("click"); + tree.unmount(); + expect(document.body.className.split(" ")).not.toContain("modal-open"); + }); }); diff --git a/ui/src/Components/NavBar/index.js b/ui/src/Components/NavBar/index.js index 7d9c551c5..92e617587 100644 --- a/ui/src/Components/NavBar/index.js +++ b/ui/src/Components/NavBar/index.js @@ -15,8 +15,8 @@ import { SilenceModal } from "Components/SilenceModal"; import "./index.css"; -const navbarResize = function(width, height) { - document.body.style["padding-top"] = height + 4 + "px"; +const NavbarOnResize = function(width, height) { + document.body.style["padding-top"] = `${height + 4}px`; }; const NavBar = observer( @@ -41,7 +41,7 @@ const NavBar = observer( return (