Skip to content

Commit a40a256

Browse files
committed
add test for dismissbutton
1 parent d52e25c commit a40a256

File tree

5 files changed

+78
-93
lines changed

5 files changed

+78
-93
lines changed

src/Frontend/src/components/customchecks/CustomCheckView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const store = useCustomChecksStore();
3131
</div>
3232
</div>
3333
<div>
34-
<button type="button" class="btn btn-default" title="Dismiss this custom check so it doesn't show up as an alert" @click="store.dismissCustomCheck(customCheck.id)">Dismiss</button>
34+
<button type="button" class="btn btn-default" title="Dismiss this custom check so it doesn't show up as an alert" role="button" aria-label="custom-check-dismiss" @click="store.dismissCustomCheck(customCheck.id)">Dismiss</button>
3535
</div>
3636
</div>
3737
</div>

src/Frontend/test/preconditions/customChecks.ts

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,64 +32,13 @@ const customCheckTemplate = <CustomCheck>{
3232
export const hasCustomChecks =
3333
(failingCount: number, passingCount: number) =>
3434
({ driver }: SetupFactoryOptions) => {
35-
const serviceControlInstanceUrl = window.defaultConfig.service_control_url;
36-
37-
// Calculate total count
38-
const totalCount = failingCount + passingCount;
39-
40-
// Create checks (both failing and passing)
41-
const customChecks = Array.from({ length: totalCount }).map((_, index) => {
42-
// Generate the date based on the index
43-
const date = new Date();
44-
date.setDate(date.getDate() - index); // Subtract `index` days from the current date
45-
const reportedAt = date.toISOString(); // Convert to ISO string format
46-
47-
// Determine status and failure reason
48-
const status = index < failingCount ? "Fail" : "Pass";
49-
const failureReason = status === "Fail" ? `configured to fail on endpoint ${index}` : "";
50-
51-
// Generate a new GUID for the ID and host_id
52-
const newGuid = generateGuid();
53-
const originatingEndpointName = `endpoint ${index}`;
54-
const originatingHost = `ABC ${index}`;
55-
const customCategory = `Some Category ${index}`;
56-
const customeCheckId = `SampleCustomeCheck ${index}`;
57-
58-
return {
59-
...customCheckTemplate,
60-
id: `customchecks/${newGuid}`, // New GUID for ID
61-
category: customCategory,
62-
custom_check_id: customeCheckId,
63-
status, // Fail or Pass based on index
64-
failure_reason: failureReason, // Failure reason or empty for passing
65-
reported_at: reportedAt, // Autogenerated reported_at based on index
66-
originating_endpoint: {
67-
name: originatingEndpointName, // Endpoint name based on index
68-
host_id: newGuid, // New GUID for host_id
69-
host: originatingHost, // Host name based on index
70-
},
71-
};
72-
});
35+
const customChecksData = generateCustomChecksData(failingCount, passingCount)();
7336

74-
const failedCustomChecks = customChecks.filter((check) => check.status === "Fail");
75-
76-
driver.mockEndpointDynamic(`${serviceControlInstanceUrl}customchecks`, (url) => {
77-
const status = url.searchParams.get("status");
78-
if (status === "fail") {
79-
return {
80-
body: failedCustomChecks,
81-
headers: { "Total-Count": failedCustomChecks.length.toString() },
82-
};
83-
}
84-
85-
return {
86-
body: customChecks,
87-
headers: { "Total-Count": customChecks.length.toString() },
88-
};
89-
});
37+
// Call getCustomChecks to mock the endpoints with the generated data
38+
getCustomChecks(customChecksData)({ driver });
9039
};
9140

92-
export const setCustomChecksData = (failingCount: number, passingCount: number) => () => {
41+
export const generateCustomChecksData = (failingCount: number, passingCount: number) => () => {
9342
// Calculate total count
9443
const totalCount = failingCount + passingCount;
9544

@@ -136,24 +85,34 @@ export const getCustomChecks =
13685

13786
const failedCustomChecks = customChecks.filter((check) => check.status === "Fail");
13887

139-
driver.mockEndpointDynamic(`${serviceControlInstanceUrl}customchecks`, (url) => {
88+
driver.mockEndpointDynamic(`${serviceControlInstanceUrl}customchecks`, "get", (url) => {
14089
const status = url.searchParams.get("status");
14190
if (status === "fail") {
142-
return {
91+
return Promise.resolve({
14392
body: failedCustomChecks,
14493
headers: { "Total-Count": failedCustomChecks.length.toString() },
145-
};
94+
});
14695
}
14796

148-
return {
97+
return Promise.resolve({
14998
body: customChecks,
15099
headers: { "Total-Count": customChecks.length.toString() },
151-
};
100+
});
101+
});
102+
driver.mockEndpointDynamic(`${serviceControlInstanceUrl}customchecks/:id`, "delete", (url, params) => {
103+
const status = url.searchParams.get("status");
104+
console.log(status + ", " + params.id);
105+
106+
return Promise.resolve({
107+
body: { message: "Successfully deleted" },
108+
status: 200,
109+
headers: { "Content-Type": "application/json" },
110+
});
152111
});
153112
};
154113

155-
export const updateCustomCheckItem = (data: CustomCheck[], statusToUpdate: string) => {
156-
const itemToUpdate = data.find((item) => item.status === status);
114+
export const updateCustomCheckItemByStatus = (data: CustomCheck[], statusToUpdate: string) => {
115+
const itemToUpdate = data.find((item) => item.status === statusToUpdate);
157116

158117
if (itemToUpdate) {
159118
if (statusToUpdate === "Pass") {
@@ -165,3 +124,15 @@ export const updateCustomCheckItem = (data: CustomCheck[], statusToUpdate: strin
165124
}
166125
}
167126
};
127+
export const updateCustomCheckItemByItem = (data: CustomCheck[], itemToUpdate: CustomCheck, statusToUpdate: string) => {
128+
const itemFound = data.find((item) => item.id === itemToUpdate.id);
129+
if (itemFound != null) {
130+
if (statusToUpdate === "Fail") {
131+
itemToUpdate.status = Status.Fail;
132+
itemToUpdate.failure_reason = "Some reason I dont know";
133+
} else {
134+
itemToUpdate.status = Status.Pass;
135+
itemToUpdate.failure_reason = "";
136+
}
137+
}
138+
};

src/Frontend/test/preconditions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export { hasServiceControlMonitoringInstanceUrl } from "../preconditions/hasServ
55
export { hasUpToDateServiceControl } from "../preconditions/hasUpToDateServiceControl";
66
export { hasUpToDateServicePulse } from "../preconditions/hasUpToDateServicePulse";
77
export { errorsDefaultHandler } from "../preconditions/hasNoErrors";
8-
export { hasCustomChecksEmpty, hasCustomChecks, setCustomChecksData, getCustomChecks, updateCustomCheckItem } from "./customChecks";
8+
export { hasCustomChecksEmpty, hasCustomChecks, generateCustomChecksData, getCustomChecks, updateCustomCheckItemByStatus, updateCustomCheckItemByItem } from "./customChecks";
99
export { hasNoDisconnectedEndpoints } from "../preconditions/hasNoDisconnectedEndpoints";
1010
export { hasNoMonitoredEndpoints, hasMonitoredEndpointsList, monitoredEndpointsNamed } from "../preconditions/hasMonitoredEndpoints";
1111
export { hasEventLogItems } from "../preconditions/hasEventLogItems";
Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { test, describe } from "../../drivers/vitest/driver";
22
import { expect } from "vitest";
33
import * as precondition from "../../preconditions";
4-
import { customChecksListElement, customChecksDismissButtonList } from "./questions/failedCustomChecks";
4+
import { customChecksListElement, customChecksDismissButtonList, customChecksFailedRowsList } from "./questions/failedCustomChecks";
55
import { waitFor } from "@testing-library/vue";
66
import userEvent from "@testing-library/user-event";
77

88
describe("FEATURE: Dismiss custom checks", () => {
99
describe("RULE: Dismiss button should be visible", () => {
1010
test("EXAMPLE: Dismiss button is visible on each failing custom check", async ({ driver }) => {
1111
await driver.setUp(precondition.serviceControlWithMonitoring);
12-
await driver.setUp(precondition.hasCustomChecks(9, 3));
12+
await driver.setUp(precondition.hasCustomChecks(3, 2));
1313

1414
await driver.goTo("/custom-checks");
1515

@@ -18,7 +18,7 @@ describe("FEATURE: Dismiss custom checks", () => {
1818
});
1919

2020
await waitFor(async () => {
21-
expect(await customChecksDismissButtonList()).toHaveLength(9); //count of dismiss button
21+
expect(await customChecksDismissButtonList()).toHaveLength(3); //count of dismiss button
2222
});
2323
});
2424
});
@@ -31,34 +31,48 @@ describe("FEATURE: Dismiss custom checks", () => {
3131

3232
await waitFor(async () => {
3333
expect(await customChecksListElement()).toBeInTheDocument(); //failed list is visisble
34+
expect(await customChecksFailedRowsList()).toHaveLength(3); //count of failed checks matches failing count set
3435
});
3536

36-
//await waitFor(async () => {
37-
// const dismissButtonList = await customChecksDismissButtonList();
38-
// expect(dismissButtonList).toHaveLength(3); //count of dismiss button
39-
// const dismissButton = dismissButtonList[0];
40-
// await userEvent.click(dismissButton);
41-
// });
42-
//get one of the dismiss button
43-
// const dismissButton = await screen.getAllByRole("button", { name: /custom-check-dismiss/i })[0];
37+
let dismissButtonList = await customChecksDismissButtonList();
38+
expect(dismissButtonList).toHaveLength(3); //count of dismiss button matches the failed custom check count
4439

45-
// Simulate user click event
40+
//click the dismiss button
41+
await userEvent.click(dismissButtonList[0]);
4642

47-
//list count should decrease by one -
48-
//make sure that the id is notvisible on the page
49-
// await waitFor(async () => {
50-
// expect(await customChecksDismissButtonList()).toHaveLength(2); //count of dismiss button
51-
// });
43+
//get the new dismiss button list
44+
dismissButtonList = await customChecksDismissButtonList();
45+
//count of dismss button is decreased by 1
46+
expect(dismissButtonList).toHaveLength(2);
5247
});
5348
});
5449
describe("RULE: Failing after a dismiss should cause the failed check to reappear", () => {
55-
test.todo("EXAMPLE: Dismissed custom check should reappear in the list when it fails");
50+
test("EXAMPLE: Dismissed custom check should reappear in the list when it fails", async ({ driver }) => {
51+
await driver.setUp(precondition.serviceControlWithMonitoring);
52+
53+
const customCheckItems = precondition.generateCustomChecksData(3, 0)();
54+
await driver.setUp(precondition.getCustomChecks(customCheckItems));
55+
await driver.goTo("/custom-checks");
5656

57-
/* SCENARIO
58-
Given 2 failed custom checks
59-
And one of them is dismissed
60-
When the dismissed custom check fails
61-
Then the custom check should appear in the list
62-
*/
57+
await waitFor(async () => {
58+
expect(await customChecksListElement()).toBeInTheDocument(); //failed list is visisble
59+
expect(await customChecksFailedRowsList()).toHaveLength(3); //count of failed checks matches failing count set
60+
});
61+
62+
const dismissButtonList = await customChecksDismissButtonList();
63+
expect(dismissButtonList).toHaveLength(3); //count of dismiss button
64+
65+
//click dismiss button
66+
await userEvent.click(dismissButtonList[0]);
67+
expect(await customChecksDismissButtonList()).toHaveLength(2); //count of dismiss button
68+
expect(await customChecksFailedRowsList()).toHaveLength(2); //count of failed checks matches failing count set
69+
70+
//re-add the dismissed custom check Item
71+
72+
await driver.setUp(precondition.getCustomChecks(customCheckItems));
73+
await driver.goTo("/custom-checks");
74+
//custom list should be increased to 3
75+
expect(await customChecksFailedRowsList()).toHaveLength(3); //count of failed checks matches failing count set
76+
});
6377
});
6478
});

src/Frontend/test/specs/customchecks/viewing-failing-custom-checks.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expect } from "vitest";
33
import * as precondition from "../../preconditions";
44
import { customChecksFailedRowsList, customChecksListElement, customChecksMessageElement, customChecksFailedReasonList, customChecksListPaginationElement, customChecksReportedDateList } from "./questions/failedCustomChecks";
55
import { waitFor } from "@testing-library/vue";
6-
import { updateCustomCheckItem } from "../../preconditions";
6+
import { updateCustomCheckItemByStatus } from "../../preconditions/customChecks";
77

88
describe("FEATURE: Failing custom checks", () => {
99
describe("RULE: Failed custom checks should be displayed", () => {
@@ -87,7 +87,7 @@ describe("FEATURE: Failing custom checks", () => {
8787
describe("RULE: Custom checks should auto-refresh", () => {
8888
test("EXAMPLE:When a custom check fails, the custom checks tab is auto-refreshed with the new failed custom check", async ({ driver }) => {
8989
await driver.setUp(precondition.serviceControlWithMonitoring);
90-
const customCheckItems = precondition.setCustomChecksData(3, 2)();
90+
const customCheckItems = precondition.generateCustomChecksData(3, 2)();
9191
await driver.setUp(precondition.getCustomChecks(customCheckItems));
9292

9393
await driver.goTo("/custom-checks");
@@ -99,7 +99,7 @@ describe("FEATURE: Failing custom checks", () => {
9999
expect(await customChecksFailedRowsList()).toHaveLength(3); //count of failed checks matches failing count set
100100
});
101101

102-
updateCustomCheckItem(customCheckItems, "Pass"); // Fail an existing item that is passing
102+
updateCustomCheckItemByStatus(customCheckItems, "Pass"); // Fail an existing item that is passing
103103

104104
await driver.setUp(precondition.getCustomChecks(customCheckItems));
105105

@@ -111,7 +111,7 @@ describe("FEATURE: Failing custom checks", () => {
111111

112112
test("EXAMPLE: A failing custom check that begins passing is auto-refreshed and removed from the list on the custom checks tab", async ({ driver }) => {
113113
await driver.setUp(precondition.serviceControlWithMonitoring);
114-
const customCheckItems = precondition.setCustomChecksData(3, 2)();
114+
const customCheckItems = precondition.generateCustomChecksData(3, 2)();
115115
await driver.setUp(precondition.getCustomChecks(customCheckItems));
116116

117117
await driver.goTo("/custom-checks");
@@ -123,7 +123,7 @@ describe("FEATURE: Failing custom checks", () => {
123123
expect(await customChecksFailedRowsList()).toHaveLength(3); //count of failed checks matches failing count set
124124
});
125125

126-
updateCustomCheckItem(customCheckItems, "Fail"); // an existing item that is failing
126+
updateCustomCheckItemByStatus(customCheckItems, "Fail"); // an existing item that is failing
127127

128128
await driver.setUp(precondition.getCustomChecks(customCheckItems));
129129

0 commit comments

Comments
 (0)