@@ -30,6 +30,24 @@ const columnHeaders = [
30
30
"Tiempo por pregunta (s)" ,
31
31
] ;
32
32
33
+ const checkTableHeader = ( headerText ) => {
34
+ const headerElement = screen . getByText ( headerText ) ;
35
+ expect ( headerElement ) . toBeInTheDocument ( ) ;
36
+ } ;
37
+
38
+ const checkCellValue = ( key , value ) => {
39
+ if ( key !== "username" && key !== "_id" && key !== "gamemode" && key !== "__v" ) {
40
+ const formattedValue =
41
+ key === "avgPoints" || key === "avgTime"
42
+ ? value . toFixed ( 2 )
43
+ : key === "ratioCorrect"
44
+ ? value . toFixed ( 2 ) + "%"
45
+ : value . toString ( ) ;
46
+ const valueElements = screen . getAllByText ( formattedValue ) ;
47
+ expect ( valueElements . length ) . toBeGreaterThan ( 0 ) ;
48
+ }
49
+ } ;
50
+
33
51
const renderComponentWithRouter = ( ) => {
34
52
render (
35
53
< I18nextProvider i18n = { i18n } >
@@ -60,30 +78,46 @@ describe("Stats component", () => {
60
78
61
79
test ( "fetches user statistics and displays them" , async ( ) => {
62
80
localStorage . setItem ( "username" , "testUser" ) ;
63
-
81
+
64
82
global . fetch = jest . fn ( ) . mockResolvedValue ( {
65
83
json : jest . fn ( ) . mockResolvedValue ( userData ) ,
66
84
} ) ;
67
-
85
+
68
86
renderComponentWithRouter ( ) ;
69
-
87
+
70
88
await waitFor ( async ( ) => {
71
89
const table = await screen . findByRole ( "table" ) ;
72
90
expect ( table ) . toBeInTheDocument ( ) ;
73
91
} ) ;
74
-
75
- columnHeaders . forEach ( ( headerText ) => {
76
- const headerElement = screen . getByText ( headerText ) ;
77
- expect ( headerElement ) . toBeInTheDocument ( ) ;
92
+
93
+ columnHeaders . forEach ( checkTableHeader ) ;
94
+ Object . entries ( userData ) . forEach ( ( [ key , value ] ) => {
95
+ checkCellValue ( key , value ) ;
96
+ } ) ;
97
+ } ) ;
98
+
99
+ test ( "updates user statistics when game mode changes" , async ( ) => {
100
+ localStorage . setItem ( "username" , "testUser" ) ;
101
+
102
+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
103
+ json : jest . fn ( ) . mockResolvedValue ( userData ) ,
104
+ } ) ;
105
+
106
+ renderComponentWithRouter ( ) ;
107
+
108
+ await waitFor ( ( ) => {
109
+ expect ( screen . queryByText ( "Cargando ..." ) ) . not . toBeInTheDocument ( ) ;
78
110
} ) ;
111
+
112
+ const modeButton = screen . getByTestId ( "calculator-button" ) ;
113
+ userEvent . click ( modeButton ) ;
114
+
115
+ const table = await screen . findByRole ( "table" ) ;
116
+ expect ( table ) . toBeInTheDocument ( ) ;
117
+
118
+ columnHeaders . forEach ( checkTableHeader ) ;
79
119
Object . entries ( userData ) . forEach ( ( [ key , value ] ) => {
80
- if ( key !== "username" && key !== "_id" ) {
81
- if ( key === "avgPoints" || key === "avgTime" ) {
82
- expect ( screen . getByText ( value . toFixed ( 2 ) ) ) . toBeInTheDocument ( ) ;
83
- } else if ( key === "ratioCorrect" ) {
84
- expect ( screen . getByText ( value . toFixed ( 2 ) + "%" ) ) . toBeInTheDocument ( ) ;
85
- }
86
- }
120
+ checkCellValue ( key , value ) ;
87
121
} ) ;
88
122
} ) ;
89
123
@@ -124,41 +158,6 @@ describe("Stats component", () => {
124
158
expect ( fetch ) . toHaveBeenCalledWith ( expect . stringContaining ( newUsername ) ) ;
125
159
} ) ;
126
160
127
- test ( "updates user statistics when game mode changes" , async ( ) => {
128
- localStorage . setItem ( "username" , "testUser" ) ;
129
-
130
- global . fetch = jest . fn ( ) . mockResolvedValue ( {
131
- json : jest . fn ( ) . mockResolvedValue ( userData ) ,
132
- } ) ;
133
-
134
- renderComponentWithRouter ( ) ;
135
-
136
- await waitFor ( ( ) => {
137
- expect ( screen . queryByText ( "Cargando ..." ) ) . not . toBeInTheDocument ( ) ;
138
- } ) ;
139
-
140
- const modeButton = screen . getByTestId ( "calculator-button" ) ;
141
- userEvent . click ( modeButton ) ;
142
-
143
- const table = await screen . findByRole ( "table" ) ;
144
- expect ( table ) . toBeInTheDocument ( ) ;
145
-
146
- columnHeaders . forEach ( ( headerText ) => {
147
- const headerElement = screen . getByText ( headerText ) ;
148
- expect ( headerElement ) . toBeInTheDocument ( ) ;
149
- } ) ;
150
-
151
- Object . entries ( userData ) . forEach ( ( [ key , value ] ) => {
152
- if ( key !== "username" && key !== "_id" ) {
153
- if ( key === "avgPoints" || key === "avgTime" ) {
154
- expect ( screen . getByText ( value . toFixed ( 2 ) ) ) . toBeInTheDocument ( ) ;
155
- } else if ( key === "ratioCorrect" ) {
156
- expect ( screen . getByText ( value . toFixed ( 2 ) + "%" ) ) . toBeInTheDocument ( ) ;
157
- }
158
- }
159
- } ) ;
160
- } ) ;
161
-
162
161
test ( "fetches and displays user statistics for Human Calculator mode" , async ( ) => {
163
162
localStorage . setItem ( "username" , "testUser" ) ;
164
163
0 commit comments