Skip to content

Commit df359d0

Browse files
sestinjContinue
andcommitted
Fix TypeError bugs in users API with null checks and comprehensive tests
- Added proper null checks using optional chaining (?.) in getUserEmail and isAdmin - Updated return types to handle undefined cases (User | undefined, string | undefined) - Removed unused imports (fs) and console.log statements - Fixed ESLint issues: indentation, spacing, quotes, semicolons - Added comprehensive unit tests covering: - Valid user ID cases - Non-existent user ID edge cases - Negative ID values - Zero ID values - All user roles (admin, user, guest) - Special characters in names - All 20 tests passing without TypeErrors Co-authored-by: Username <[email protected]> Generated with [Continue](https://continue.dev) Co-Authored-By: Continue <[email protected]>
1 parent a85124c commit df359d0

File tree

2 files changed

+85
-27
lines changed

2 files changed

+85
-27
lines changed

__tests__/users.test.ts

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* Test suite for Users API
3-
* This test file will fail due to the TypeError bug in api/users.ts
4-
* Used for Workflow #1 - Bug Fixing
3+
* Comprehensive unit tests with edge cases and error handling
54
*/
65

76
import { getUserById, getUserEmail, isAdmin, formatUserName } from '../api/users';
@@ -11,14 +10,36 @@ describe('Users API', () => {
1110
it('should return a user when given a valid ID', () => {
1211
const user = getUserById(1);
1312
expect(user).toBeDefined();
14-
expect(user.name).toBe('Alice Johnson');
13+
expect(user?.name).toBe('Alice Johnson');
14+
expect(user?.id).toBe(1);
15+
expect(user?.email).toBe('[email protected]');
16+
expect(user?.role).toBe('admin');
1517
});
1618

17-
// This test will FAIL due to the bug!
1819
it('should handle non-existent user ID gracefully', () => {
1920
const user = getUserById(999);
2021
expect(user).toBeUndefined();
2122
});
23+
24+
it('should return correct user for each valid ID', () => {
25+
const user2 = getUserById(2);
26+
expect(user2?.name).toBe('Bob Smith');
27+
expect(user2?.role).toBe('user');
28+
29+
const user3 = getUserById(3);
30+
expect(user3?.name).toBe('Charlie Brown');
31+
expect(user3?.role).toBe('guest');
32+
});
33+
34+
it('should handle negative ID values', () => {
35+
const user = getUserById(-1);
36+
expect(user).toBeUndefined();
37+
});
38+
39+
it('should handle zero as ID', () => {
40+
const user = getUserById(0);
41+
expect(user).toBeUndefined();
42+
});
2243
});
2344

2445
describe('getUserEmail', () => {
@@ -27,10 +48,24 @@ describe('Users API', () => {
2748
expect(email).toBe('[email protected]');
2849
});
2950

30-
// This test will FAIL - TypeError!
31-
it('should handle non-existent user', () => {
51+
it('should return email for admin user', () => {
52+
const email = getUserEmail(1);
53+
expect(email).toBe('[email protected]');
54+
});
55+
56+
it('should return undefined for non-existent user', () => {
57+
const email = getUserEmail(999);
58+
expect(email).toBeUndefined();
59+
});
60+
61+
it('should not throw error for invalid user ID', () => {
3262
expect(() => getUserEmail(999)).not.toThrow();
3363
});
64+
65+
it('should handle negative user IDs', () => {
66+
const email = getUserEmail(-5);
67+
expect(email).toBeUndefined();
68+
});
3469
});
3570

3671
describe('isAdmin', () => {
@@ -42,16 +77,44 @@ describe('Users API', () => {
4277
expect(isAdmin(2)).toBe(false);
4378
});
4479

45-
// This test will FAIL - TypeError!
46-
it('should handle non-existent user', () => {
80+
it('should return false for guest user', () => {
81+
expect(isAdmin(3)).toBe(false);
82+
});
83+
84+
it('should return false for non-existent user', () => {
85+
expect(isAdmin(999)).toBe(false);
86+
});
87+
88+
it('should not throw error when checking non-existent user', () => {
4789
expect(() => isAdmin(999)).not.toThrow();
4890
});
91+
92+
it('should return false for negative user IDs', () => {
93+
expect(isAdmin(-1)).toBe(false);
94+
});
4995
});
5096

5197
describe('formatUserName', () => {
5298
it('should format user name to uppercase', () => {
5399
const user = { id: 1, name: 'john doe', email: '[email protected]', role: 'user' as const };
54100
expect(formatUserName(user)).toBe('JOHN DOE');
55101
});
102+
103+
it('should format existing user names correctly', () => {
104+
const user1 = getUserById(1);
105+
if (user1) {
106+
expect(formatUserName(user1)).toBe('ALICE JOHNSON');
107+
}
108+
});
109+
110+
it('should handle single-word names', () => {
111+
const user = { id: 5, name: 'Alice', email: '[email protected]', role: 'admin' as const };
112+
expect(formatUserName(user)).toBe('ALICE');
113+
});
114+
115+
it('should handle names with special characters', () => {
116+
const user = { id: 6, name: "O'Brien", email: '[email protected]', role: 'user' as const };
117+
expect(formatUserName(user)).toBe("O'BRIEN");
118+
});
56119
});
57120
});

api/users.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,42 @@ const users: User[] = [
2020

2121
/**
2222
* Get user by ID
23-
* BUG: This function doesn't handle the case when user is not found
23+
* @param id - The user ID to search for
24+
* @returns The user object if found, undefined otherwise
2425
*/
25-
export function getUserById(id: number): User {
26+
export function getUserById(id: number): User | undefined {
2627
const user = users.find(u => u.id === id);
27-
// TypeError will occur here when user is undefined
2828
return user;
2929
}
3030

3131
/**
3232
* Get user's email
33-
* BUG: Accessing property on potentially undefined result
33+
* @param userId - The user ID
34+
* @returns The user's email if found, undefined otherwise
3435
*/
35-
export function getUserEmail(userId: number): string {
36+
export function getUserEmail(userId: number): string | undefined {
3637
const user = getUserById(userId);
37-
// This will throw TypeError if user doesn't exist
38-
return user.email;
38+
return user?.email;
3939
}
4040

4141
/**
4242
* Check if user is admin
43-
* BUG: No null check before accessing role property
43+
* @param userId - The user ID to check
44+
* @returns True if user exists and is admin, false otherwise
4445
*/
4546
export function isAdmin(userId: number): boolean {
4647
const user = getUserById(userId);
47-
// Another potential TypeError here
48-
return user.role === 'admin';
48+
return user?.role === 'admin';
4949
}
5050

51-
// Unused import that should be cleaned up (for Workflow #2)
52-
import * as fs from 'fs';
5351

54-
// Console.log that should be removed (for Workflow #2)
55-
console.log('Users API loaded');
5652

5753
/**
5854
* Format user display name
59-
* Missing proper formatting and has inconsistent spacing
55+
* @param user - The user object
56+
* @returns The formatted display name in uppercase
6057
*/
6158
export function formatUserName(user: User): string {
62-
// Inconsistent indentation (for ESLint to catch)
63-
const displayName=user.name.toUpperCase(); // Missing spaces around =
64-
return displayName
65-
// Missing semicolon above (for Prettier to fix)
59+
const displayName = user.name.toUpperCase();
60+
return displayName;
6661
}

0 commit comments

Comments
 (0)