Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix older safari launch app issue #33238

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libs/UserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ function getDefaultAvatar(accountID = -1, avatarURL?: string): React.FC<SvgProps
// But the avatar link still corresponds to the original ID-generated link. So we extract the SVG image number from the backend's link instead of using the user ID directly
let accountIDHashBucket: AvatarRange;
if (avatarURL) {
const match = avatarURL.match(/(?<=default-avatar_)\d+(?=\.)/);
const lastDigit = match && parseInt(match[0], 10);
const match = avatarURL.match(/(default-avatar_)(\d+)(?=\.)/);
Copy link
Contributor

@situchan situchan Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add unit test? I think this can be easily broken in the future
btw, not blocker

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I agree. Would you mind doing that @rojiphil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Let me check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unit test

const lastDigit = match && parseInt(match[2], 10);
accountIDHashBucket = lastDigit as AvatarRange;
} else {
accountIDHashBucket = ((accountID % CONST.DEFAULT_AVATAR_COUNT) + 1) as AvatarRange;
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/UserUtilsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as UserUtils from '../../src/libs/UserUtils';

describe('UserUtils', () => {
it('should return the default avatar from the avatar url', () => {
const avatarURL = 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png';
const defaultAvatar = UserUtils.getDefaultAvatar(1, avatarURL);
expect(typeof defaultAvatar).toBe('function');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useless. I was expecting accountIDHashBucket to be returned. Also add some more cases including invalid ones.

Copy link
Contributor Author

@rojiphil rojiphil Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useless. I was expecting accountIDHashBucket to be returned.

@situchan
Not sure if I understood your expectation correctly here as the function getDefaultAvatar does not return accountIDHashBucket.
Can you please explain more about your expectation? Maybe, pointing to an existing unit test case of a similar kind can also help. Maybe I am missing something simple here.

Also add some more cases including invalid ones.

When the regex fails, the function will return undefined. I can add a case for the invalid one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the purpose of this test was to evaluate regex.
But you reused this function (getDefaultAvatar) which doesn't detect regex is right or not.
If no other way, better to remove this redundant test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, since our issue here was due to lack of lookbehind support in older safari versions, I am wondering how a unit test case will detect this.
I am not sure if the test cases are run against specific versions of browser.
I would like to know your thoughts on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar concern: #25742 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create lint rule something like https://eslint.org/docs/latest/rules/no-useless-backreference but out of scope for this PR.
I am fine to merge as is for now.
cc: @Julesssss

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create lint rule something like https://eslint.org/docs/latest/rules/no-useless-backreference but out of scope for this PR.

I think this is an interesting approach and worth considering.
But yes, I also think this is out of scope for this PR.

});
});
Loading