From 9332c5e4b2f6735a70bf84142c73aa22c878f1cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sat, 1 Sep 2018 22:58:15 +0100 Subject: [PATCH] feat(tests): add Alert tests --- .../Alert/__snapshots__/index.test.js.snap | 78 +++++++++++++++++ .../AlertGrid/AlertGroup/Alert/index.test.js | 87 +++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/__snapshots__/index.test.js.snap create mode 100644 ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/__snapshots__/index.test.js.snap b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/__snapshots__/index.test.js.snap new file mode 100644 index 000000000..321615444 --- /dev/null +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/__snapshots__/index.test.js.snap @@ -0,0 +1,78 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` matches snapshot with showAlertmanagers=false showReceiver=false 1`] = ` +" +
  • +
    +
    + + + + + + help: + + + some long text + +
    +
    + + + + + hidden +
    +
    + + + + + job: node_exporter + + + cluster: dev + + + + + + + link + +
  • +" +`; diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js new file mode 100644 index 000000000..67f424f33 --- /dev/null +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js @@ -0,0 +1,87 @@ +import React from "react"; + +import { Provider } from "mobx-react"; + +import { mount } from "enzyme"; + +import { advanceTo, clear } from "jest-date-mock"; + +import toDiffableHtml from "diffable-html"; + +import { MockAlert, MockAnnotation } from "__mocks__/Alerts.js"; +import { AlertStore } from "Stores/AlertStore"; +import { Alert } from "."; + +let alertStore; + +beforeEach(() => { + advanceTo(new Date(2018, 7, 15, 20, 40, 0)); + alertStore = new AlertStore([]); +}); + +afterEach(() => { + // reset Date() to current time + clear(); +}); + +const MockAfterUpdate = jest.fn(); + +const MockedAlert = () => { + return MockAlert( + [ + MockAnnotation("help", "some long text", true, false), + MockAnnotation("hidden", "some hidden text", false, false), + MockAnnotation("link", "http://localhost", true, true) + ], + { job: "node_exporter", cluster: "dev" }, + "active" + ); +}; + +const MountedAlert = (alert, showAlertmanagers, showReceiver) => { + return mount( + + + + ); +}; + +describe("", () => { + it("matches snapshot with showAlertmanagers=false showReceiver=false", () => { + const alert = MockedAlert(); + const tree = MountedAlert(alert, false, false); + expect(toDiffableHtml(tree.html())).toMatchSnapshot(); + }); + + it("renders @alertmanager label with showAlertmanagers=true", () => { + const alert = MockedAlert(); + const tree = MountedAlert(alert, true, false); + const label = tree + .find("FilteringLabel") + .filterWhere(elem => elem.props().name === "@alertmanager"); + expect(label.text()).toBe("@alertmanager: default"); + }); + + it("renders @receiver label with showReceiver=true", () => { + const alert = MockedAlert(); + const tree = MountedAlert(alert, false, true); + const label = tree + .find("FilteringLabel") + .filterWhere(elem => elem.props().name === "@receiver"); + expect(label.text()).toBe("@receiver: by-name"); + }); + + it("renders a silence if alert is silenced", () => { + const alert = MockedAlert(); + alert.alertmanager[0].silencedBy = ["silence123456789"]; + const tree = MountedAlert(alert, false, false); + const silence = tree.find("Silence"); + expect(silence).toHaveLength(1); + expect(silence.html()).toMatch(/silence123456789/); + }); +});