Skip to content

Commit b1e86d4

Browse files
committed
tests
1 parent 9513493 commit b1e86d4

16 files changed

Lines changed: 4387 additions & 295 deletions

web-report/package-lock.json

Lines changed: 3432 additions & 118 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web-report/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
"dev": "vite",
88
"generate": "json2ts ../src/main/resources/wfc/schemas/report.yaml src/types/GeneratedTypes.tsx",
99
"build": "tsc -b && vite build",
10-
"installAndBuild": "yarn install && yarn generate && tsc -b && vite build",
10+
"installAndBuild": "yarn install && yarn generate && vitest --no-watch && tsc -b && vite build",
1111
"lint": "eslint .",
1212
"preview": "vite preview",
13-
"debug": "cpx \"tests/static/*\" ../target/generated-sources/webreport && vite preview"
13+
"debug": "vite build && cpx \"src/tests/static/*\" ../target/generated-sources/webreport && vite preview",
14+
"test": "vitest"
1415
},
1516
"dependencies": {
1617
"@radix-ui/react-accordion": "^1.2.3",
@@ -34,18 +35,25 @@
3435
},
3536
"devDependencies": {
3637
"@eslint/js": "^9.21.0",
38+
"@testing-library/dom": "^10.4.0",
39+
"@testing-library/jest-dom": "^6.6.3",
40+
"@testing-library/react": "^16.3.0",
41+
"@types/jest": "^29.5.14",
3742
"@types/node": "^22.13.10",
3843
"@types/react": "^19.0.10",
3944
"@types/react-dom": "^19.0.4",
4045
"@types/react-syntax-highlighter": "^15.5.13",
4146
"@vitejs/plugin-react": "^4.3.4",
47+
"@vitest/ui": "3.1.3",
4248
"cpx": "^1.5.0",
4349
"eslint": "^9.21.0",
4450
"eslint-plugin-react-hooks": "^5.1.0",
4551
"eslint-plugin-react-refresh": "^0.4.19",
4652
"globals": "^15.15.0",
53+
"happy-dom": "^17.4.7",
4754
"typescript": "^5.8.3",
4855
"typescript-eslint": "^8.24.1",
49-
"vite": "^6.2.0"
56+
"vite": "^6.2.0",
57+
"vitest": "^3.1.3"
5058
}
5159
}

web-report/src/components/Dashboard.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ export const Dashboard: React.FC<IDashboard> = ({data, test_files}) => {
5656
<TabsTrigger
5757
value="overview"
5858
className="min-w-[150px] py-3 border border-gray-300 data-[state=active]:bg-white data-[state=active]:border-2 data-[state=active]:border-black data-[state=active]:shadow-[2px_2px_0px_0px_rgba(0,0,0,1)]"
59+
data-testid="tab-overview"
5960
>
6061
Overview
6162
</TabsTrigger>
6263
<TabsTrigger
6364
value="endpoints"
6465
className="min-w-[150px] py-3 border border-gray-300 data-[state=active]:bg-white data-[state=active]:border-2 data-[state=active]:border-black data-[state=active]:shadow-[2px_2px_0px_0px_rgba(0,0,0,1)]"
66+
data-testid="tab-endpoints"
6567
>
6668
Endpoints
6769
</TabsTrigger>

web-report/src/components/EndpointAccordion.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const EndpointAccordion: React.FC<IEndpointAccordionProps> = ({
3434
const faultColors = ["bg-red-300", "bg-red-500", "bg-red-700"];
3535

3636
return (
37-
<AccordionItem value={value} className="border-2 border-black mb-4 overflow-hidden">
37+
<AccordionItem value={value} className="border-2 border-black mb-4 overflow-hidden" data-testid={endpoint}>
3838
<AccordionTrigger className="bg-blue-100 px-4 py-3 text-lg font-bold hover:no-underline hover:bg-blue-200">
3939
{endpoint}
4040
</AccordionTrigger>

web-report/src/components/FaultsComponent.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,25 @@ import {Faults} from "@/types/GeneratedTypes.tsx";
55

66
export const FaultsComponent: React.FC<Faults> = ({total_number, found_faults}) => {
77
const faultCounts = new Map();
8-
8+
//TODO make this calculation in utils and test it.
99
found_faults.forEach(fault => {
1010
fault.fault_categories.forEach(category => {
1111
faultCounts.set(category.code, (faultCounts.get(category.code) || 0) + 1);
1212
});
1313
});
1414

15-
16-
17-
1815
return(
1916
<Card className="border-2 border-black p-6 rounded-none">
2017
<div className="flex items-start gap-4 mb-4">
2118
<ShieldAlert className="w-6 h-6 text-gray-500" />
2219
<div className="flex-1">
2320
<div className="flex justify-between">
2421
<span className="text-lg font-bold">Total Faults</span>
25-
<span className="text-lg font-bold">{total_number}</span>
22+
<span className="text-lg font-bold" data-testid="faults-component-total-faults">{total_number}</span>
2623
</div>
2724
<div className="flex justify-between">
2825
<span className="text-lg font-bold">Distinct Fault Types:</span>
29-
<span className="text-lg font-bold">{faultCounts.size}</span>
26+
<span className="text-lg font-bold" data-testid="faults-component-fault-counts">{faultCounts.size}</span>
3027
</div>
3128
</div>
3229
</div>

web-report/src/components/GeneratedTests.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export const GeneratedTests: React.FC<IGeneratedTests> = ({total_tests, total_te
1414
<div className="flex-1">
1515
<div className="flex justify-between">
1616
<span className="text-lg font-bold">Generated Tests Cases:</span>
17-
<span className="text-lg font-bold">{total_tests}</span>
17+
<span className="text-lg font-bold" data-testid="generated-tests-total-tests">{total_tests}</span>
1818
</div>
1919
<div className="flex justify-between">
2020
<span className="text-lg font-bold">Generated Test Files:</span>
21-
<span className="text-lg font-bold">{total_test_files}</span>
21+
<span className="text-lg font-bold" data-testid="generated-tests-total-test-files">{total_test_files}</span>
2222
</div>
2323
</div>
2424
</div>

web-report/src/components/Header.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ interface IHeaderProps {
88

99
export const Header: React.FC<IHeaderProps> = ({date, tool_name_version, schema_version}) => (
1010
<div className="flex justify-between border-b border-black pb-2 mb-4">
11-
<div className="font-bold">Creation Date: {new Date(date).toUTCString()}</div>
12-
<div className="font-bold text-center">Tool: {tool_name_version}</div>
13-
<div className="font-bold text-right">Schema Version: {schema_version}</div>
11+
<div className="font-bold" data-testid="header-creation-date">Creation Date: {new Date(date).toUTCString()}</div>
12+
<div className="font-bold text-center" data-testid="header-tool-name-version">Tool: {tool_name_version}</div>
13+
<div className="font-bold text-right" data-testid="header-schema-version">Schema Version: {schema_version}</div>
1414
</div>
1515
)

web-report/src/components/RestReports.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const RestReports: React.FC<RESTReport> = ({total_http_calls, covered_htt
1212
"5XX": 0
1313
}
1414

15+
//TODO make this calculation in utils and test it.
1516
endpoint_ids.map(
1617
(endpoint) => {
1718
const allStatusCodes = covered_http_status.filter(status => status.endpoint_id === endpoint)
@@ -73,12 +74,12 @@ export const RestReports: React.FC<RESTReport> = ({total_http_calls, covered_htt
7374
<div className="mt-6">
7475
<div className="flex justify-between font-bold">
7576
<span># Endpoints:</span>
76-
<span>{endpoint_ids.length}</span>
77+
<span data-testid="rest-report-endpoint">{endpoint_ids.length}</span>
7778
</div>
7879
<div className="border-t border-black my-2"></div>
7980
<div className="flex justify-between font-bold">
8081
<span># HTTP Calls:</span>
81-
<span>{total_http_calls}</span>
82+
<span data-testid="rest-report-http-calls">{total_http_calls}</span>
8283
</div>
8384
<div className="border-t border-black my-2"></div>
8485
</div>

web-report/src/pages/Endpoints.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const Endpoints: React.FC<IProps> = ({addTestTab, data}) => {
7575
<Accordion type="single" collapsible className="w-full">
7676
{
7777
transformed.map((item, index) => (
78-
<EndpointAccordion key={index} value={`_${index}`} endpoint={item.endpoint}
78+
<EndpointAccordion data-testid="endpoint" key={index} value={`_${index}`} endpoint={item.endpoint}
7979
status_codes={item.http_status_codes} faults={item.faults}
8080
addTestTab={addTestTab}/>
8181
))

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

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import {render, waitFor, screen, act, fireEvent} from '@testing-library/react';
2+
import '@testing-library/jest-dom';
3+
import {resolve} from "path";
4+
import {readFileSync} from "fs";
5+
import {vi} from "vitest";
6+
import {fetchFileContent} from "@/utils.tsx";
7+
import App from "@/App.tsx";
8+
9+
// Read the report.json file
10+
const reportJsonPath = resolve(__dirname, './static/report.json');
11+
const reportData = JSON.parse(readFileSync(reportJsonPath, 'utf-8'));
12+
13+
// Mock the fetchFileContent function to return the content of the report.json file
14+
vi.mock('@/utils.tsx', async (importOriginal) => {
15+
const originalModule = await importOriginal();
16+
return{
17+
...originalModule as typeof importOriginal,
18+
fetchFileContent: vi.fn(async (filePath: string) => {
19+
const folderPath = resolve(__dirname, './static/');
20+
const fullPath = resolve(folderPath, filePath);
21+
if (fullPath.endsWith('.json')) {
22+
return reportData;
23+
} else if (filePath.endsWith('.java')) {
24+
return readFileSync(fullPath, 'utf-8');
25+
}
26+
throw new Error('File not found');
27+
})
28+
}
29+
});
30+
31+
describe('App test', () => {
32+
beforeEach(() => {
33+
// Clear all mocks before each test
34+
vi.clearAllMocks();
35+
});
36+
37+
it('shows loading state initially', () => {
38+
render(<App />);
39+
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
40+
});
41+
42+
it('handles successful data loading', async () => {
43+
render(<App />);
44+
45+
// Initially shows loading state
46+
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
47+
48+
// Wait for loading to complete and verify header data
49+
await waitFor(() => {
50+
expect(screen.queryByText(/Please wait, files are loading.../)).toBeNull();
51+
});
52+
53+
});
54+
55+
it('handles error state', async () => {
56+
// Mock fetchFileContent to throw an error
57+
vi.mocked(fetchFileContent).mockRejectedValueOnce(new Error('Failed to load'));
58+
59+
render(<App />);
60+
61+
// Wait for error state
62+
await waitFor(() => {
63+
expect(screen.getByText(/Could not load the report file/)).toBeInTheDocument();
64+
});
65+
});
66+
67+
it('check header data', async () => {
68+
render(<App />);
69+
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
70+
await waitFor(() => {
71+
expect(screen.getByTestId('header-creation-date')).toContainHTML(new Date(reportData.creation_time).toUTCString());
72+
expect(screen.getByTestId('header-tool-name-version')).toContainHTML(`${reportData.tool_name}`);
73+
expect(screen.getByTestId('header-tool-name-version')).toContainHTML(`${reportData.tool_version}`);
74+
expect(screen.getByTestId('header-schema-version')).toContainHTML(reportData.schema_version);
75+
});
76+
});
77+
78+
it('check rest report', async () => {
79+
render(<App />);
80+
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
81+
const total = reportData.problem_details.rest.endpoint_ids.length;
82+
const total_http_calls = reportData.problem_details.rest.total_http_calls;
83+
await waitFor(() => {
84+
//TODO make number of endpoints testable
85+
expect(screen.getByTestId('rest-report-endpoint')).toContainHTML(`${total}`);
86+
expect(screen.getByTestId('rest-report-http-calls')).toContainHTML(`${total_http_calls}`);
87+
});
88+
});
89+
90+
it('check generated tests', async () => {
91+
render(<App />);
92+
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
93+
const total_tests = reportData.total_tests;
94+
const total_test_files = reportData.test_file_paths.length;
95+
await waitFor(() => {
96+
expect(screen.getByTestId('generated-tests-total-tests')).toContainHTML(`${total_tests}`);
97+
expect(screen.getByTestId('generated-tests-total-test-files')).toContainHTML(`${total_test_files}`);
98+
});
99+
});
100+
101+
it('check endpoints tab', async () => {
102+
render(<App />);
103+
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
104+
105+
await waitFor(() => {
106+
const overviewTab = screen.getByTestId('tab-overview');
107+
108+
// check if the endpoints tab is active
109+
expect(overviewTab).toHaveClass('data-[state=active]:bg-white');
110+
111+
});
112+
const endpointsTab = screen.getByTestId('tab-endpoints');
113+
114+
act(() => {
115+
// click on the endpoints tab
116+
fireEvent.focus(endpointsTab);
117+
118+
});
119+
120+
await waitFor(() => {
121+
// check if the endpoints tab is active
122+
expect(endpointsTab).toHaveClass('data-[state=active]:bg-white');
123+
});
124+
125+
await waitFor(() => {
126+
// check if the endpoints are displayed
127+
reportData.problem_details.rest.endpoint_ids.forEach((endpoint: string) => {
128+
// const testId = convertEndpointToTestId(endpoint);
129+
expect(screen.getByTestId(endpoint)).toBeInTheDocument();
130+
});
131+
});
132+
});
133+
134+
it('check faults component', async () => {
135+
render(<App />);
136+
expect(screen.getByText(/Please wait, files are loading.../)).toBeInTheDocument();
137+
const total_faults = reportData.faults.total_number;
138+
139+
await waitFor(() => {
140+
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}`);
144+
});
145+
});
146+
});

0 commit comments

Comments
 (0)