@@ -287,4 +287,75 @@ describe('UserService', () => {
287
287
expect ( service . usernameExists ) . toHaveBeenCalledWith ( baseUsername ) ;
288
288
} ) ;
289
289
} ) ;
290
+
291
+ describe ( 'normalizeUsername' , ( ) => {
292
+ it ( 'should normalize a username' , ( ) => {
293
+ const inputUsername = 'tést user' ;
294
+ const normalizedUsername = 'test_user' ;
295
+
296
+ const result = ( service as any ) . normalizeUsername ( inputUsername ) ;
297
+
298
+ expect ( result ) . toBe ( normalizedUsername ) ;
299
+ } ) ;
300
+
301
+ it ( 'should remove special characters from a username' , ( ) => {
302
+ const inputUsername = '静_かな' ;
303
+ const normalizedUsername = '_' ;
304
+
305
+ const result = ( service as any ) . normalizeUsername ( inputUsername ) ;
306
+
307
+ expect ( result ) . toBe ( normalizedUsername ) ;
308
+ } ) ;
309
+
310
+ it ( 'should replace spaces with underscores in a username' , ( ) => {
311
+ const inputUsername = 'Имя пользователя' ;
312
+ const normalizedUsername = '_' ;
313
+
314
+ const result = ( service as any ) . normalizeUsername ( inputUsername ) ;
315
+
316
+ expect ( result ) . toBe ( normalizedUsername ) ;
317
+ } ) ;
318
+
319
+ it ( 'should replace spaces with underscores in a username' , ( ) => {
320
+ const inputUsername = 'Eglė Čepulytė' ;
321
+ const normalizedUsername = 'Egle_Cepulyte' ;
322
+
323
+ const result = ( service as any ) . normalizeUsername ( inputUsername ) ;
324
+
325
+ expect ( result ) . toBe ( normalizedUsername ) ;
326
+ } ) ;
327
+ } ) ;
328
+
329
+ describe ( 'updateUsername' , ( ) => {
330
+ it ( 'should update a user username' , async ( ) => {
331
+ const user = {
332
+ username : 'testuser' ,
333
+ save : jest . fn ( ) . mockReturnThis ( ) ,
334
+ } as unknown as UserDocument ;
335
+ const body = { username : 'newuser' } ;
336
+
337
+ jest . spyOn ( service , 'usernameExists' ) . mockResolvedValue ( false ) ;
338
+
339
+ const result = await service . updateUsername ( user , body ) ;
340
+
341
+ expect ( result ) . toEqual ( user ) ;
342
+ expect ( user . username ) . toBe ( body . username ) ;
343
+ expect ( service . usernameExists ) . toHaveBeenCalledWith ( body . username ) ;
344
+ } ) ;
345
+
346
+ it ( 'should throw an error if username already exists' , async ( ) => {
347
+ const user = {
348
+ username : 'testuser' ,
349
+ save : jest . fn ( ) . mockReturnThis ( ) ,
350
+ } as unknown as UserDocument ;
351
+
352
+ const body = { username : 'newuser' } ;
353
+
354
+ jest . spyOn ( service , 'usernameExists' ) . mockResolvedValue ( true ) ;
355
+
356
+ await expect ( service . updateUsername ( user , body ) ) . rejects . toThrow (
357
+ new HttpException ( 'Username already exists' , HttpStatus . BAD_REQUEST ) ,
358
+ ) ;
359
+ } ) ;
360
+ } ) ;
290
361
} ) ;
0 commit comments