mirror of
https://github.com/prymitive/karma
synced 2026-05-13 03:56:59 +00:00
feat(tests): add silenceFormStore tests
This commit is contained in:
123
ui/src/Stores/SilenceFormStore.test.js
Normal file
123
ui/src/Stores/SilenceFormStore.test.js
Normal file
@@ -0,0 +1,123 @@
|
||||
import moment from "moment";
|
||||
|
||||
import { MockAlert, MockAlertGroup } from "__mocks__/Alerts.js";
|
||||
import { SilenceFormStore } from "./SilenceFormStore";
|
||||
|
||||
let store;
|
||||
beforeEach(() => {
|
||||
store = new SilenceFormStore();
|
||||
});
|
||||
|
||||
describe("SilenceFormStore.toggle", () => {
|
||||
it("toggle() toggles 'visible' correctly", () => {
|
||||
expect(store.toggle.visible).toBe(false);
|
||||
store.toggle.toggle();
|
||||
expect(store.toggle.visible).toBe(true);
|
||||
store.toggle.toggle();
|
||||
expect(store.toggle.visible).toBe(false);
|
||||
});
|
||||
|
||||
it("show() set 'visible' to true", () => {
|
||||
expect(store.toggle.visible).toBe(false);
|
||||
store.toggle.show();
|
||||
expect(store.toggle.visible).toBe(true);
|
||||
});
|
||||
|
||||
it("hide() set 'visible' to false", () => {
|
||||
expect(store.toggle.visible).toBe(false);
|
||||
store.toggle.visible = true;
|
||||
expect(store.toggle.visible).toBe(true);
|
||||
store.toggle.hide();
|
||||
expect(store.toggle.visible).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
const MockGroup = () => {
|
||||
const alerts = [
|
||||
MockAlert([], { instance: "prod1", cluster: "prod" }),
|
||||
MockAlert([], { instance: "prod2", cluster: "prod" }),
|
||||
MockAlert([], { instance: "dev1", cluster: "dev" })
|
||||
];
|
||||
const group = MockAlertGroup({ alertname: "FakeAlert" }, alerts, [], {
|
||||
job: "mock"
|
||||
});
|
||||
return group;
|
||||
};
|
||||
|
||||
describe("SilenceFormStore.data", () => {
|
||||
it("resetProgress() sets 'inProgress' to false", () => {
|
||||
store.data.inProgress = true;
|
||||
expect(store.data.inProgress).toBe(true);
|
||||
store.data.resetProgress();
|
||||
expect(store.data.inProgress).toBe(false);
|
||||
});
|
||||
|
||||
it("addEmptyMatcher() appends a matcher", () => {
|
||||
expect(store.data.matchers).toHaveLength(0);
|
||||
store.data.addEmptyMatcher();
|
||||
expect(store.data.matchers).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("deleteMatcher() removes a matcher with passed id", () => {
|
||||
store.data.addEmptyMatcher();
|
||||
store.data.addEmptyMatcher();
|
||||
store.data.addEmptyMatcher();
|
||||
const matcherID = store.data.matchers[1].id;
|
||||
store.data.deleteMatcher(matcherID);
|
||||
expect(store.data.matchers).toHaveLength(2);
|
||||
expect(store.data.matchers).not.toContainEqual(
|
||||
expect.objectContaining({ id: matcherID })
|
||||
);
|
||||
});
|
||||
|
||||
it("fillMatchersFromGroup() creates correct matcher object for a group", () => {
|
||||
const group = MockGroup();
|
||||
store.data.fillMatchersFromGroup(group);
|
||||
expect(store.data.matchers).toHaveLength(4);
|
||||
expect(store.data.matchers).toContainEqual(
|
||||
expect.objectContaining({
|
||||
name: "alertname",
|
||||
values: [{ label: "FakeAlert", value: "FakeAlert" }],
|
||||
isRegex: false
|
||||
})
|
||||
);
|
||||
expect(store.data.matchers).toContainEqual(
|
||||
expect.objectContaining({
|
||||
name: "job",
|
||||
values: [{ label: "mock", value: "mock" }],
|
||||
isRegex: false
|
||||
})
|
||||
);
|
||||
expect(store.data.matchers).toContainEqual(
|
||||
expect.objectContaining({
|
||||
name: "instance",
|
||||
values: [
|
||||
{ label: "dev1", value: "dev1" },
|
||||
{ label: "prod1", value: "prod1" },
|
||||
{ label: "prod2", value: "prod2" }
|
||||
],
|
||||
isRegex: true
|
||||
})
|
||||
);
|
||||
expect(store.data.matchers).toContainEqual(
|
||||
expect.objectContaining({
|
||||
name: "cluster",
|
||||
values: [
|
||||
{ label: "dev", value: "dev" },
|
||||
{ label: "prod", value: "prod" }
|
||||
],
|
||||
isRegex: true
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("toAlertmanagerPayload creates payload that matches snapshot", () => {
|
||||
const group = MockGroup();
|
||||
store.data.fillMatchersFromGroup(group);
|
||||
store.data.startsAt = moment([2000, 1, 1, 0, 0, 0]);
|
||||
store.data.endsAt = moment([2000, 1, 1, 1, 0, 0]);
|
||||
store.data.createdBy = "me@example.com";
|
||||
store.data.comment = "toAlertmanagerPayload test";
|
||||
expect(store.data.toAlertmanagerPayload).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
32
ui/src/Stores/__snapshots__/SilenceFormStore.test.js.snap
Normal file
32
ui/src/Stores/__snapshots__/SilenceFormStore.test.js.snap
Normal file
@@ -0,0 +1,32 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`SilenceFormStore.data toAlertmanagerPayload creates payload that matches snapshot 1`] = `
|
||||
Object {
|
||||
"comment": "toAlertmanagerPayload test",
|
||||
"createdBy": "",
|
||||
"endsAt": "2000-02-01T01:00:00.000Z",
|
||||
"matchers": Array [
|
||||
Object {
|
||||
"isRegex": false,
|
||||
"name": "alertname",
|
||||
"value": "FakeAlert",
|
||||
},
|
||||
Object {
|
||||
"isRegex": false,
|
||||
"name": "job",
|
||||
"value": "mock",
|
||||
},
|
||||
Object {
|
||||
"isRegex": true,
|
||||
"name": "instance",
|
||||
"value": "(dev1|prod1|prod2)",
|
||||
},
|
||||
Object {
|
||||
"isRegex": true,
|
||||
"name": "cluster",
|
||||
"value": "(dev|prod)",
|
||||
},
|
||||
],
|
||||
"startsAt": "2000-02-01T00:00:00.000Z",
|
||||
}
|
||||
`;
|
||||
53
ui/src/__mocks__/Alerts.js
Normal file
53
ui/src/__mocks__/Alerts.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const MockAnnotation = (name, value, visible, isLink) => ({
|
||||
name: name,
|
||||
value: value,
|
||||
visible: visible,
|
||||
isLink: isLink
|
||||
});
|
||||
|
||||
const MockAlert = (annotations, labels, state) => ({
|
||||
annotations: annotations,
|
||||
labels: labels,
|
||||
startsAt: "2018-08-14T17:36:40.017867056Z",
|
||||
endsAt: "0001-01-01T00:00:00Z",
|
||||
state: state,
|
||||
alertmanager: [
|
||||
{
|
||||
name: "default",
|
||||
uri: "file:///mock",
|
||||
state: "active",
|
||||
startsAt: "2018-08-14T17:36:40.017867056Z",
|
||||
endsAt: "0001-01-01T00:00:00Z",
|
||||
source: "localhost/prometheus",
|
||||
silencedBy: []
|
||||
}
|
||||
],
|
||||
receiver: "by-name"
|
||||
});
|
||||
|
||||
const MockAlertGroup = (
|
||||
rootLabels,
|
||||
alerts,
|
||||
sharedAnnotations,
|
||||
sharedLabels
|
||||
) => ({
|
||||
receiver: "by-name",
|
||||
labels: rootLabels,
|
||||
alerts: alerts,
|
||||
id: "099c5ca6d1c92f615b13056b935d0c8dee70f18c",
|
||||
hash: "53a4bb3d7e916450b3bda550976f9578db5b2ad3",
|
||||
alertmanagerCount: {
|
||||
default: 1
|
||||
},
|
||||
stateCount: {
|
||||
active: 1,
|
||||
suppressed: 0,
|
||||
unprocessed: 0
|
||||
},
|
||||
shared: {
|
||||
annotations: sharedAnnotations,
|
||||
labels: sharedLabels
|
||||
}
|
||||
});
|
||||
|
||||
export { MockAnnotation, MockAlert, MockAlertGroup };
|
||||
Reference in New Issue
Block a user