@@ -25,7 +25,7 @@ describe("Test Question API", () => {
25
25
const newQuestion = {
26
26
title : "Sample Question" ,
27
27
description : "This is a sample question" ,
28
- category : "General" ,
28
+ category : [ "General" ] ,
29
29
complexity : "Easy" ,
30
30
} ;
31
31
@@ -37,7 +37,7 @@ describe("Test Question API", () => {
37
37
"description" ,
38
38
"This is a sample question"
39
39
) ;
40
- expect ( res . body . question ) . toHaveProperty ( "category" , "General" ) ;
40
+ expect ( res . body . question ) . toHaveProperty ( "category" , [ "General" ] ) ;
41
41
expect ( res . body . question ) . toHaveProperty ( "complexity" , "Easy" ) ;
42
42
} ) ;
43
43
@@ -46,7 +46,7 @@ describe("Test Question API", () => {
46
46
const newQuestion = {
47
47
title : "" ,
48
48
description : "This is a sample question" ,
49
- category : "General" ,
49
+ category : [ "General" ] ,
50
50
complexity : "Easy" ,
51
51
} ;
52
52
@@ -61,7 +61,7 @@ describe("Test Question API", () => {
61
61
const newQuestion = {
62
62
title : "Sample Question" ,
63
63
description : "" ,
64
- category : "General" ,
64
+ category : [ "General" ] ,
65
65
complexity : "Easy" ,
66
66
} ;
67
67
@@ -76,13 +76,28 @@ describe("Test Question API", () => {
76
76
const newQuestion = {
77
77
title : "Sample Question" ,
78
78
description : "This is a sample question" ,
79
- category : "" ,
79
+ category : [ ] ,
80
80
complexity : "Easy" ,
81
81
} ;
82
82
83
83
const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
84
84
expect ( res . statusCode ) . toBe ( 400 ) ;
85
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Invalid value" ) ;
85
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must be a non-empty array" ) ;
86
+ expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
87
+ } ) ;
88
+
89
+ // Empty category with whitespace only
90
+ test ( "POST /api/create - empty category with whitespace string" , async ( ) => {
91
+ const newQuestion = {
92
+ title : "Sample Question" ,
93
+ description : "This is a sample question" ,
94
+ category : [ " " ] ,
95
+ complexity : "Easy" ,
96
+ } ;
97
+
98
+ const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
99
+ expect ( res . statusCode ) . toBe ( 400 ) ;
100
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
86
101
expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
87
102
} ) ;
88
103
@@ -91,7 +106,7 @@ describe("Test Question API", () => {
91
106
const newQuestion = {
92
107
title : "Sample Question" ,
93
108
description : "This is a sample question" ,
94
- category : "General" ,
109
+ category : [ "General" ] ,
95
110
complexity : "" ,
96
111
} ;
97
112
@@ -121,7 +136,7 @@ describe("Test Question API", () => {
121
136
const newQuestion = {
122
137
title : "Sample Question" ,
123
138
description : [ "test" ] ,
124
- category : "General" ,
139
+ category : [ "General" ] ,
125
140
complexity : "Easy" ,
126
141
} ;
127
142
@@ -136,13 +151,58 @@ describe("Test Question API", () => {
136
151
const newQuestion = {
137
152
title : "Sample Question" ,
138
153
description : "This is a sample question" ,
139
- category : [ "test" ] ,
154
+ category : "test" ,
140
155
complexity : "Easy" ,
141
156
} ;
142
157
143
158
const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
144
159
expect ( res . statusCode ) . toBe ( 400 ) ;
145
- expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Invalid value" ) ;
160
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must be a non-empty array" ) ;
161
+ expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
162
+ } ) ;
163
+
164
+ // Invalid category
165
+ test ( "POST /api/create - invalid category" , async ( ) => {
166
+ const newQuestion = {
167
+ title : "Sample Question" ,
168
+ description : "This is a sample question" ,
169
+ category : [ 3 , "test" ] ,
170
+ complexity : "Easy" ,
171
+ } ;
172
+
173
+ const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
174
+ expect ( res . statusCode ) . toBe ( 400 ) ;
175
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
176
+ expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
177
+ } ) ;
178
+
179
+ // Invalid category
180
+ test ( "POST /api/create - invalid category" , async ( ) => {
181
+ const newQuestion = {
182
+ title : "Sample Question" ,
183
+ description : "This is a sample question" ,
184
+ category : [ "test" , "" ] ,
185
+ complexity : "Easy" ,
186
+ } ;
187
+
188
+ const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
189
+ expect ( res . statusCode ) . toBe ( 400 ) ;
190
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
191
+ expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
192
+ } ) ;
193
+
194
+ // Invalid category
195
+ test ( "POST /api/create - invalid category with whitespace" , async ( ) => {
196
+ const newQuestion = {
197
+ title : "Sample Question" ,
198
+ description : "This is a sample question" ,
199
+ category : [ " " ] ,
200
+ complexity : "Easy" ,
201
+ } ;
202
+
203
+ const res = await request . post ( "/api/create" ) . send ( newQuestion ) ;
204
+ expect ( res . statusCode ) . toBe ( 400 ) ;
205
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
146
206
expect ( res . body . errors [ 0 ] . path ) . toBe ( "category" ) ;
147
207
} ) ;
148
208
@@ -151,7 +211,7 @@ describe("Test Question API", () => {
151
211
const newQuestion = {
152
212
title : "Sample Question" ,
153
213
description : "This is a sample question" ,
154
- category : "General" ,
214
+ category : [ "General" ] ,
155
215
complexity : [ "test" ] ,
156
216
} ;
157
217
@@ -176,7 +236,7 @@ describe("Test Get All", () => {
176
236
"description" ,
177
237
"This is a sample question"
178
238
) ;
179
- expect ( sampleQuestion ) . toHaveProperty ( "category" , "General" ) ;
239
+ expect ( sampleQuestion ) . toHaveProperty ( "category" , [ "General" ] ) ;
180
240
expect ( sampleQuestion ) . toHaveProperty ( "complexity" , "Easy" ) ;
181
241
} ) ;
182
242
} ) ;
@@ -191,7 +251,7 @@ describe("Test Get by Id", () => {
191
251
expect ( res . body ) . toHaveProperty ( "questionid" , questionId ) ;
192
252
expect ( res . body ) . toHaveProperty ( "title" , "Sample Question" ) ;
193
253
expect ( res . body ) . toHaveProperty ( "description" , "This is a sample question" ) ;
194
- expect ( res . body ) . toHaveProperty ( "category" , "General" ) ;
254
+ expect ( res . body ) . toHaveProperty ( "category" , [ "General" ] ) ;
195
255
expect ( res . body ) . toHaveProperty ( "complexity" , "Easy" ) ;
196
256
} ) ;
197
257
@@ -219,7 +279,7 @@ describe("Test Update", () => {
219
279
const updateQuestion = {
220
280
title : "Update Title" ,
221
281
description : "Update Description" ,
222
- category : "Update Category" ,
282
+ category : [ "Update Category" ] ,
223
283
complexity : "Update Complexity" ,
224
284
} ;
225
285
const questionId = 1090 ;
@@ -235,6 +295,19 @@ describe("Test Update", () => {
235
295
} ) ;
236
296
237
297
// Empty update
298
+ test ( "POST - empty title update" , async ( ) => {
299
+ const updateQuestion = {
300
+ title : 3 ,
301
+ } ;
302
+ const questionId = 1090 ;
303
+ const res = await request
304
+ . post ( `/api/${ questionId } /update` )
305
+ . send ( updateQuestion ) ;
306
+ expect ( res . statusCode ) . toBe ( 400 ) ;
307
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Field must be a string" ) ;
308
+ } ) ;
309
+
310
+ // Empty field update
238
311
test ( "POST - empty update" , async ( ) => {
239
312
const updateQuestion = {
240
313
title : "" ,
@@ -250,12 +323,54 @@ describe("Test Update", () => {
250
323
expect ( res . body . errors [ 0 ] . msg ) . toBe ( "At least one field must be provided" ) ;
251
324
} ) ;
252
325
326
+ // Update with invalid category
327
+ test ( "POST - empty category" , async ( ) => {
328
+ const updateQuestion = {
329
+ category : [ ]
330
+ } ;
331
+ const questionId = 1090 ;
332
+ const res = await request
333
+ . post ( `/api/${ questionId } /update` )
334
+ . send ( updateQuestion ) ;
335
+ expect ( res . statusCode ) . toBe ( 400 ) ;
336
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must be a non-empty array" ) ;
337
+ } ) ;
338
+
339
+ // Update with invalid category
340
+ test ( "POST - category containing numbers" , async ( ) => {
341
+ const updateQuestion = {
342
+ category : [ 3 ] ,
343
+ complexity : [ "test" ] ,
344
+ } ;
345
+ const questionId = 1090 ;
346
+ const res = await request
347
+ . post ( `/api/${ questionId } /update` )
348
+ . send ( updateQuestion ) ;
349
+ expect ( res . statusCode ) . toBe ( 400 ) ;
350
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
351
+ } ) ;
352
+
353
+ // Update with invalid category
354
+ test ( "POST - category containing empty string" , async ( ) => {
355
+ const updateQuestion = {
356
+ description : "This is a sample question" ,
357
+ category : [ "test" , "" ] ,
358
+ complexity : [ "test" ] ,
359
+ } ;
360
+ const questionId = 1090 ;
361
+ const res = await request
362
+ . post ( `/api/${ questionId } /update` )
363
+ . send ( updateQuestion ) ;
364
+ expect ( res . statusCode ) . toBe ( 400 ) ;
365
+ expect ( res . body . errors [ 0 ] . msg ) . toBe ( "Category must contain only non-empty strings" ) ;
366
+ } ) ;
367
+
253
368
// Negative id
254
369
test ( "POST - negative id update" , async ( ) => {
255
370
const updateQuestion = {
256
371
title : "Update Title" ,
257
372
description : "Update Description" ,
258
- category : "Update Category" ,
373
+ category : [ "Update Category" ] ,
259
374
complexity : "Update Complexity" ,
260
375
} ;
261
376
const questionId = - 1 ;
@@ -271,7 +386,7 @@ describe("Test Update", () => {
271
386
const updateQuestion = {
272
387
title : "Update Title" ,
273
388
description : "Update Description" ,
274
- category : "Update Category" ,
389
+ category : [ "Update Category" ] ,
275
390
complexity : "Update Complexity" ,
276
391
} ;
277
392
const questionId = 999999 ;
0 commit comments