-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: extract username from user should not return undefined (#5061)
This fixes a return type error by changing the logic of `extractUsernameFromUser` to never return undefined. In the previous code, `user` could be truthy, but that doesn't mean `email` or `username` were defined. This assumes we always fallback to "unknown" in those scenarios.
- Loading branch information
Showing
2 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IUser } from '../server-impl'; | ||
import { extractUsernameFromUser } from './extract-user'; | ||
|
||
describe('extractUsernameFromUser', () => { | ||
test('Should return the email if it exists', () => { | ||
const user = { | ||
email: '[email protected]', | ||
username: 'ratatoskr', | ||
} as IUser; | ||
|
||
expect(extractUsernameFromUser(user)).toBe(user.email); | ||
}); | ||
|
||
test('Should return the username if it exists and email does not', () => { | ||
const user = { | ||
username: 'ratatoskr', | ||
} as IUser; | ||
|
||
expect(extractUsernameFromUser(user)).toBe(user.username); | ||
}); | ||
|
||
test('Should return "unknown" if neither email nor username exists', () => { | ||
const user = {} as IUser; | ||
|
||
expect(extractUsernameFromUser(user)).toBe('unknown'); | ||
}); | ||
|
||
test('Should return "unknown" if user is null', () => { | ||
const user = null as unknown as IUser; | ||
expect(extractUsernameFromUser(user)).toBe('unknown'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters