Skip to content

Commit c993cf1

Browse files
committed
minor fixes
1 parent e93ec2c commit c993cf1

4 files changed

Lines changed: 76 additions & 64 deletions

File tree

web-report/src/components/FaultsComponent.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import {Card} from "@/components/ui/card.tsx";
22
import {ShieldAlert} from "lucide-react";
33
import type React from "react";
44
import {Faults} from "@/types/GeneratedTypes.tsx";
5+
import {getFaultCounts} from "@/utils.tsx";
56

67
export const FaultsComponent: React.FC<Faults> = ({total_number, found_faults}) => {
7-
const faultCounts = new Map();
8-
//TODO make this calculation in utils and test it.
9-
found_faults.forEach(fault => {
10-
fault.fault_categories.forEach(category => {
11-
faultCounts.set(category.code, (faultCounts.get(category.code) || 0) + 1);
12-
});
13-
});
8+
const faultCounts = getFaultCounts(found_faults);
149

1510
return(
1611
<Card className="border-2 border-black p-6 rounded-none">

web-report/src/components/RestReports.tsx

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,11 @@ import {Card} from "@/components/ui/card.tsx";
22
import type React from "react";
33
import {CoveragePieChart} from "@/components/CoveragePieChart.tsx";
44
import {RESTReport} from "@/types/GeneratedTypes.tsx";
5+
import {calculateAllStatusCounts} from "@/utils.tsx";
56

67
export const RestReports: React.FC<RESTReport> = ({total_http_calls, covered_http_status, endpoint_ids}) => {
78
const total = endpoint_ids.length;
8-
const allStatusCounts ={
9-
"2XX": 0,
10-
"3XX": 0,
11-
"4XX": 0,
12-
"5XX": 0
13-
}
14-
15-
//TODO make this calculation in utils and test it.
16-
endpoint_ids.map(
17-
(endpoint) => {
18-
const allStatusCodes = covered_http_status.filter(status => status.endpoint_id === endpoint)
19-
.map(
20-
(status) => status.http_status
21-
).flat()
22-
const uniqueStatusCodes = [...new Set(allStatusCodes)];
23-
24-
const isContainStatusCode = {
25-
"2XX": false,
26-
"3XX": false,
27-
"4XX": false,
28-
"5XX": false
29-
}
30-
31-
uniqueStatusCodes.map(
32-
(status) => {
33-
if (status >= 200 && status < 300) {
34-
isContainStatusCode["2XX"] = true;
35-
} else if (status >= 300 && status < 400) {
36-
isContainStatusCode["3XX"] = true;
37-
} else if (status >= 400 && status < 500) {
38-
isContainStatusCode["4XX"] = true;
39-
} else if (status >= 500 && status < 600) {
40-
isContainStatusCode["5XX"] = true;
41-
}
42-
}
43-
)
44-
45-
if (isContainStatusCode["2XX"]) {
46-
allStatusCounts["2XX"]++;
47-
}
48-
if (isContainStatusCode["3XX"]) {
49-
allStatusCounts["3XX"]++;
50-
}
51-
if (isContainStatusCode["4XX"]) {
52-
allStatusCounts["4XX"]++;
53-
}
54-
if (isContainStatusCode["5XX"]) {
55-
allStatusCounts["5XX"]++;
56-
}
57-
}
58-
)
9+
const allStatusCounts = calculateAllStatusCounts(covered_http_status, endpoint_ids);
5910

6011
return (
6112
<Card className="border-2 border-black p-6 rounded-none">

web-report/src/tests/App.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import '@testing-library/jest-dom';
33
import {resolve} from "path";
44
import {readFileSync} from "fs";
55
import {vi} from "vitest";
6-
import {fetchFileContent} from "@/utils.tsx";
6+
import {fetchFileContent, getFaultCounts} from "@/utils.tsx";
77
import App from "@/App.tsx";
88

99
// Read the report.json file
@@ -80,11 +80,12 @@ describe('App test', () => {
8080
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
8181
const total = reportData.problem_details.rest.endpoint_ids.length;
8282
const total_http_calls = reportData.problem_details.rest.total_http_calls;
83+
8384
await waitFor(() => {
84-
//TODO make number of endpoints testable
8585
expect(screen.getByTestId('rest-report-endpoint')).toContainHTML(`${total}`);
8686
expect(screen.getByTestId('rest-report-http-calls')).toContainHTML(`${total_http_calls}`);
8787
});
88+
8889
});
8990

9091
it('check generated tests', async () => {
@@ -135,12 +136,11 @@ describe('App test', () => {
135136
render(<App />);
136137
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
137138
const total_faults = reportData.faults.total_number;
139+
const faultCounts = getFaultCounts(reportData.faults.found_faults);
138140

139141
await waitFor(() => {
140142
expect(screen.getByTestId('faults-component-total-faults')).toContainHTML(`${total_faults}`);
141-
142-
//TODO make faultCounts testable
143-
// expect(screen.getByTestId('faults-component-fault-counts')).toContainHTML(`${total_test_files}`);
143+
expect(screen.getByTestId('faults-component-fault-counts')).toContainHTML(faultCounts.size.toString());
144144
});
145145
});
146146
});

web-report/src/utils.tsx

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {CoveredEndpoint, FoundFault} from "@/types/GeneratedTypes.tsx";
2+
13
export const getColor = (code: string | number, isBackground: boolean, isFault: boolean) => {
24
if (isFault) {
35
return isBackground ? "bg-red-500" : "text-red-500";
@@ -57,4 +59,68 @@ export const extractCodeLines = (
5759
}
5860

5961
return lines.slice(startIndex, endIndex + 2).join('\n');
60-
};
62+
};
63+
64+
export const calculateAllStatusCounts = (covered_http_status: CoveredEndpoint[], endpoint_ids:string[]) => {
65+
const allStatusCounts ={
66+
"2XX": 0,
67+
"3XX": 0,
68+
"4XX": 0,
69+
"5XX": 0
70+
}
71+
72+
endpoint_ids.map(
73+
(endpoint) => {
74+
const allStatusCodes = covered_http_status.filter(status => status.endpoint_id === endpoint)
75+
.map(
76+
(status) => status.http_status
77+
).flat()
78+
const uniqueStatusCodes = [...new Set(allStatusCodes)];
79+
80+
const isContainStatusCode = {
81+
"2XX": false,
82+
"3XX": false,
83+
"4XX": false,
84+
"5XX": false
85+
}
86+
87+
uniqueStatusCodes.map(
88+
(status) => {
89+
if (status >= 200 && status < 300) {
90+
isContainStatusCode["2XX"] = true;
91+
} else if (status >= 300 && status < 400) {
92+
isContainStatusCode["3XX"] = true;
93+
} else if (status >= 400 && status < 500) {
94+
isContainStatusCode["4XX"] = true;
95+
} else if (status >= 500 && status < 600) {
96+
isContainStatusCode["5XX"] = true;
97+
}
98+
}
99+
)
100+
101+
if (isContainStatusCode["2XX"]) {
102+
allStatusCounts["2XX"]++;
103+
}
104+
if (isContainStatusCode["3XX"]) {
105+
allStatusCounts["3XX"]++;
106+
}
107+
if (isContainStatusCode["4XX"]) {
108+
allStatusCounts["4XX"]++;
109+
}
110+
if (isContainStatusCode["5XX"]) {
111+
allStatusCounts["5XX"]++;
112+
}
113+
}
114+
)
115+
return allStatusCounts;
116+
}
117+
118+
export const getFaultCounts = (found_faults: FoundFault[]) => {
119+
const faultCounts = new Map();
120+
found_faults.forEach(fault => {
121+
fault.fault_categories.forEach(category => {
122+
faultCounts.set(category.code, (faultCounts.get(category.code) || 0) + 1);
123+
});
124+
});
125+
return faultCounts;
126+
}

0 commit comments

Comments
 (0)