add unit test to addProtocol

This commit is contained in:
Reinaldo Neto 2023-02-15 11:26:53 -03:00
parent dbef194fff
commit 67811bb36b
3 changed files with 29 additions and 7 deletions

View File

@ -0,0 +1,20 @@
import addProtocol from './addProtocol';
describe('Add the protocol https at the begin of the URL', () => {
it('return the link as original when sent with https at the begin', () => {
const linkHttps = 'https://www.google.com';
expect(addProtocol(linkHttps)).toBe(linkHttps);
});
it('return the link as original when sent with http at the begin', () => {
const linkHttp = 'http://www.google.com';
expect(addProtocol(linkHttp)).toBe(linkHttp);
});
it("return a new link with protocol at the begin when there isn't the protocol at the begin", () => {
const linkWithoutProtocol = 'www.google.com';
expect(addProtocol(linkWithoutProtocol)).toBe('https://www.google.com');
});
it('return the link correctly when the original starts with double slash, because the server is returning that', () => {
const linkWithDoubleSlashAtBegin = '//www.google.com';
expect(addProtocol(linkWithDoubleSlashAtBegin)).toBe('https://www.google.com');
});
});

View File

@ -0,0 +1,8 @@
const addProtocol = (url: string): string => {
if (!url.toLowerCase().startsWith('http')) {
return `https://${url.replace('//', '')}`;
}
return url;
};
export default addProtocol;

View File

@ -5,6 +5,7 @@ import parse from 'url-parse';
import { themes } from '../../constants'; import { themes } from '../../constants';
import { TSupportedThemes } from '../../../theme'; import { TSupportedThemes } from '../../../theme';
import UserPreferences from '../userPreferences'; import UserPreferences from '../userPreferences';
import addProtocol from './addProtocol';
export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY'; export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY';
@ -36,13 +37,6 @@ const appSchemeURL = (url: string, browser: string): string => {
return schemeUrl; return schemeUrl;
}; };
const addProtocol = (url: string): string => {
if (!url.toLowerCase().startsWith('http')) {
return `https://${url.replace('//', '')}`;
}
return url;
};
const openLink = async (url: string, theme: TSupportedThemes = 'light'): Promise<void> => { const openLink = async (url: string, theme: TSupportedThemes = 'light'): Promise<void> => {
url = addProtocol(url); url = addProtocol(url);
try { try {