[FIX] Possibility to use the External Provider URI with query

This commit is contained in:
Reinaldo Neto 2023-03-08 17:35:54 -03:00
parent 58391964cb
commit 8d720bd000
2 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,27 @@
import { formatUrl } from './getAvatarUrl';
jest.mock('react-native', () => ({ PixelRatio: { get: () => 1 } }));
describe('Test the formatURL', () => {
it('format as default to get the avatar from an user', () => {
const url = 'https://mobile.rocket.chat/avatar/reinaldoneto';
const size = 30;
const query = '&extraparam=true';
const expectedResult = `https://mobile.rocket.chat/avatar/reinaldoneto?format=png&size=30&extraparam=true`;
expect(formatUrl(url, size, query)).toBe(expectedResult);
});
it('format external provider uri to get the avatar from an user', () => {
const url = 'https://open.rocket.chat/avatar/reinaldoneto';
const size = 30;
const query = undefined;
const expectedResult = `https://open.rocket.chat/avatar/reinaldoneto?format=png&size=30`;
expect(formatUrl(url, size, query)).toBe(expectedResult);
});
it('format external provider uri that already adds a query to get the avatar from an user', () => {
const url = 'https://open.rocket.chat/avatar?rcusername=reinaldoneto';
const size = 30;
const query = undefined;
const expectedResult = `https://open.rocket.chat/avatar?rcusername=reinaldoneto&format=png&size=30`;
expect(formatUrl(url, size, query)).toBe(expectedResult);
});
});

View File

@ -4,7 +4,10 @@ import { SubscriptionType } from '../../../definitions';
import { IAvatar } from '../../../containers/Avatar/interfaces';
import { compareServerVersion } from './compareServerVersion';
const formatUrl = (url: string, size: number, query?: string) => `${url}?format=png&size=${PixelRatio.get() * size}${query}`;
export const formatUrl = (url: string, size: number, query?: string) => {
const hasQuestionMark = /\/[^\/?]+\?/.test(url);
return `${url}${hasQuestionMark ? '&' : '?'}format=png&size=${PixelRatio.get() * size}${query || ''}`;
};
export const getAvatarURL = ({
type,