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 ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . toBeInTheDocument ( ) ;
40+ } ) ;
41+
42+ it ( 'handles successful data loading' , async ( ) => {
43+ render ( < App /> ) ;
44+
45+ // Initially shows loading state
46+ expect ( screen . getByText ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . toBeInTheDocument ( ) ;
47+
48+ // Wait for loading to complete and verify header data
49+ await waitFor ( ( ) => {
50+ expect ( screen . queryByText ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . 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 ( / C o u l d n o t l o a d t h e r e p o r t f i l e / ) ) . toBeInTheDocument ( ) ;
64+ } ) ;
65+ } ) ;
66+
67+ it ( 'check header data' , async ( ) => {
68+ render ( < App /> ) ;
69+ expect ( screen . getByText ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . 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 ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . 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 ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . 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 ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . 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 ( / P l e a s e w a i t , f i l e s a r e l o a d i n g .../ ) ) . 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