test(vue): add unit tests for getDisplayName utility#530
Conversation
📝 WalkthroughWalkthroughThis pull request adds a comprehensive test suite for the ChangesDisplay Name Utility Tests
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/vue/src/__tests__/utils/getDisplayName.test.ts (1)
26-59: ⚡ Quick winThe tests validate return order but not lookup keys.
Using only
mockReturnValueOncecan still pass ifgetDisplayNamecallsgetMappedUserProfileValuewith wrong attribute names but the same call count/order. Add argument assertions (e.g.,toHaveBeenNthCalledWith) or switch to a key-based mock implementation map.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/vue/src/__tests__/utils/getDisplayName.test.ts` around lines 26 - 59, The tests for getDisplayName currently only assert call order via mockReturnValueOnce, which can hide wrong lookup keys; update the tests in getDisplayName.test.ts to assert the actual lookup keys passed to getMappedUserProfileValue (or mockGet) — either replace the sequenced mockReturnValueOnce approach with a key-based mock implementation (e.g., mockGet.mockImplementation(key => ({ firstName: 'Jane', lastName: 'Doe', username: 'janedoe', email: 'jane@example.com' }[key]))) or add explicit argument assertions using toHaveBeenNthCalledWith against mockGet for each case; target the tests that call getDisplayName(mergedMappings, user, ...) and verify mockGet/getMappedUserProfileValue was called with the expected attribute names.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/vue/src/__tests__/utils/getDisplayName.test.ts`:
- Around line 33-47: Add a new unit test to cover the `name` fallback in
getDisplayName: in the test file using the same helpers (`mockGet`,
`mergedMappings`, `user`), set mockGet to return undefined for firstName,
lastName, username, and email (four consecutive mockReturnValueOnce(undefined)
calls) and then return a non-empty string for `name` (e.g., 'Jane Doe'); call
getDisplayName(mergedMappings, user) and assert the result equals that `name`
value so the explicit `name` branch in getDisplayName is exercised.
---
Nitpick comments:
In `@packages/vue/src/__tests__/utils/getDisplayName.test.ts`:
- Around line 26-59: The tests for getDisplayName currently only assert call
order via mockReturnValueOnce, which can hide wrong lookup keys; update the
tests in getDisplayName.test.ts to assert the actual lookup keys passed to
getMappedUserProfileValue (or mockGet) — either replace the sequenced
mockReturnValueOnce approach with a key-based mock implementation (e.g.,
mockGet.mockImplementation(key => ({ firstName: 'Jane', lastName: 'Doe',
username: 'janedoe', email: 'jane@example.com' }[key]))) or add explicit
argument assertions using toHaveBeenNthCalledWith against mockGet for each case;
target the tests that call getDisplayName(mergedMappings, user, ...) and verify
mockGet/getMappedUserProfileValue was called with the expected attribute names.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: bf893e0e-aae8-48fc-a88f-6e6eb13031b1
📒 Files selected for processing (1)
packages/vue/src/__tests__/utils/getDisplayName.test.ts
| it('returns username when no firstName or lastName', () => { | ||
| mockGet.mockReturnValueOnce(undefined); | ||
| mockGet.mockReturnValueOnce(undefined); | ||
| mockGet.mockReturnValueOnce('janedoe'); | ||
| const result = getDisplayName(mergedMappings, user); | ||
| expect(result).toBe('janedoe'); | ||
| }); | ||
|
|
||
| it('returns email when no firstName lastName or username', () => { | ||
| mockGet.mockReturnValueOnce(undefined); | ||
| mockGet.mockReturnValueOnce(undefined); | ||
| mockGet.mockReturnValueOnce(undefined); | ||
| mockGet.mockReturnValueOnce('jane@example.com'); | ||
| const result = getDisplayName(mergedMappings, user); | ||
| expect(result).toBe('jane@example.com'); |
There was a problem hiding this comment.
Missing coverage for the name fallback branch.
getDisplayName has an explicit name fallback after username and email, but this suite never asserts that path. Add a test where firstName/lastName/username/email are absent and name is present.
Suggested test addition
+ it('returns name when firstName, lastName, username, and email are missing', () => {
+ mockGet.mockReturnValueOnce(undefined); // firstName
+ mockGet.mockReturnValueOnce(undefined); // lastName
+ mockGet.mockReturnValueOnce(undefined); // username
+ mockGet.mockReturnValueOnce(undefined); // email
+ mockGet.mockReturnValueOnce('Jane Fullname'); // name
+ const result = getDisplayName(mergedMappings, user);
+ expect(result).toBe('Jane Fullname');
+ });📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| it('returns username when no firstName or lastName', () => { | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce('janedoe'); | |
| const result = getDisplayName(mergedMappings, user); | |
| expect(result).toBe('janedoe'); | |
| }); | |
| it('returns email when no firstName lastName or username', () => { | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce('jane@example.com'); | |
| const result = getDisplayName(mergedMappings, user); | |
| expect(result).toBe('jane@example.com'); | |
| it('returns username when no firstName or lastName', () => { | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce('janedoe'); | |
| const result = getDisplayName(mergedMappings, user); | |
| expect(result).toBe('janedoe'); | |
| }); | |
| it('returns email when no firstName lastName or username', () => { | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce(undefined); | |
| mockGet.mockReturnValueOnce('jane@example.com'); | |
| const result = getDisplayName(mergedMappings, user); | |
| expect(result).toBe('jane@example.com'); | |
| }); | |
| it('returns name when firstName, lastName, username, and email are missing', () => { | |
| mockGet.mockReturnValueOnce(undefined); // firstName | |
| mockGet.mockReturnValueOnce(undefined); // lastName | |
| mockGet.mockReturnValueOnce(undefined); // username | |
| mockGet.mockReturnValueOnce(undefined); // email | |
| mockGet.mockReturnValueOnce('Jane Fullname'); // name | |
| const result = getDisplayName(mergedMappings, user); | |
| expect(result).toBe('Jane Fullname'); | |
| }); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/vue/src/__tests__/utils/getDisplayName.test.ts` around lines 33 -
47, Add a new unit test to cover the `name` fallback in getDisplayName: in the
test file using the same helpers (`mockGet`, `mergedMappings`, `user`), set
mockGet to return undefined for firstName, lastName, username, and email (four
consecutive mockReturnValueOnce(undefined) calls) and then return a non-empty
string for `name` (e.g., 'Jane Doe'); call getDisplayName(mergedMappings, user)
and assert the result equals that `name` value so the explicit `name` branch in
getDisplayName is exercised.
Purpose
I noticed that the
getDisplayNameutility function in the@asgardeo/vuepackage had no unit tests at all. This PR adds 6 tests covering all the main code paths — including fallback behavior when user attributes are missing, and when displayAttributes are provided.Related Issues
@asgardeo/vueSDK #170Related PRs
Checklist
Security checks
Summary by CodeRabbit