From 56a2a147ca1ea2f0665817daa1943fee18f3f0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Wed, 22 Aug 2018 12:27:07 +0100 Subject: [PATCH] feat(test): add BaseLabel tests --- .../Components/Labels/BaseLabel/index.test.js | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 ui/src/Components/Labels/BaseLabel/index.test.js diff --git a/ui/src/Components/Labels/BaseLabel/index.test.js b/ui/src/Components/Labels/BaseLabel/index.test.js new file mode 100644 index 000000000..dc86bff0c --- /dev/null +++ b/ui/src/Components/Labels/BaseLabel/index.test.js @@ -0,0 +1,80 @@ +import React from "react"; +import renderer from "react-test-renderer"; + +import { AlertStore } from "Stores/AlertStore"; + +import { BaseLabel } from "."; + +let alertStore; + +beforeEach(() => { + alertStore = new AlertStore([]); +}); + +const FakeBaseLabel = () => { + // BaseLabel doesn't implement render since it's an abstract component + // Add a dummy implementation for testing + class RenderableBaseLabel extends BaseLabel { + render() { + return null; + } + } + return renderer.create( + + ); +}; + +describe("", () => { + it("isStaticColorLabel() returns true for labels present in staticColorLabels", () => { + alertStore.settings.values.staticColorLabels = ["foo", "job", "bar"]; + const instance = FakeBaseLabel().getInstance(); + expect(instance.isStaticColorLabel("job")).toBeTruthy(); + }); + + it("isStaticColorLabel() returns false for labels not present in staticColorLabels", () => { + alertStore.settings.values.staticColorLabels = ["foo"]; + const instance = FakeBaseLabel().getInstance(); + expect(instance.isStaticColorLabel("job")).toBeFalsy(); + }); + + it("getColorClass() on a label included in staticColorLabels should return 'info'", () => { + alertStore.settings.values.staticColorLabels = ["job"]; + const instance = FakeBaseLabel().getInstance(); + expect(instance.getColorClass("job", "foo")).toBe("info"); + }); + + it("getColorClass() on a label without any special color should return 'warning'", () => { + const instance = FakeBaseLabel().getInstance(); + expect(instance.getColorClass("foo", "bar")).toBe("warning"); + }); + + it("getColorClass() on 'alertname' label should return 'dark'", () => { + const instance = FakeBaseLabel().getInstance(); + expect(instance.getColorClass("alertname", "foo")).toBe("dark"); + }); + + it("getColorStyle() on a label included in staticColorLabels should be empty", () => { + alertStore.settings.values.staticColorLabels = ["job"]; + const instance = FakeBaseLabel().getInstance(); + expect(instance.getColorStyle("job", "bar")).toMatchObject({}); + }); + + it("getColorStyle() on a label without any color information should be empty", () => { + const instance = FakeBaseLabel().getInstance(); + expect(instance.getColorStyle("foo", "bar")).toMatchObject({}); + }); + + it("getColorStyle() on a label with color information should be correctly formatted", () => { + alertStore.data.colors["foo"] = { + bar: { + font: { red: 1, green: 2, blue: 3, alpha: 100 }, + background: { red: 4, green: 5, blue: 6, alpha: 200 } + } + }; + const instance = FakeBaseLabel().getInstance(); + expect(instance.getColorStyle("foo", "bar")).toMatchObject({ + color: "rgba(1, 2, 3, 100)", + backgroundColor: "rgba(4, 5, 6, 200)" + }); + }); +});