From 67811bb36b0f12fa1a6bfa25f8bb07d5781ef382 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Wed, 15 Feb 2023 11:26:53 -0300 Subject: [PATCH] add unit test to addProtocol --- app/lib/methods/helpers/addProtocol.test.ts | 20 ++++++++++++++++++++ app/lib/methods/helpers/addProtocol.ts | 8 ++++++++ app/lib/methods/helpers/openLink.ts | 8 +------- 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 app/lib/methods/helpers/addProtocol.test.ts create mode 100644 app/lib/methods/helpers/addProtocol.ts diff --git a/app/lib/methods/helpers/addProtocol.test.ts b/app/lib/methods/helpers/addProtocol.test.ts new file mode 100644 index 000000000..a4991792c --- /dev/null +++ b/app/lib/methods/helpers/addProtocol.test.ts @@ -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'); + }); +}); diff --git a/app/lib/methods/helpers/addProtocol.ts b/app/lib/methods/helpers/addProtocol.ts new file mode 100644 index 000000000..cbdd1b9c0 --- /dev/null +++ b/app/lib/methods/helpers/addProtocol.ts @@ -0,0 +1,8 @@ +const addProtocol = (url: string): string => { + if (!url.toLowerCase().startsWith('http')) { + return `https://${url.replace('//', '')}`; + } + return url; +}; + +export default addProtocol; diff --git a/app/lib/methods/helpers/openLink.ts b/app/lib/methods/helpers/openLink.ts index 217c54d72..19bff6e37 100644 --- a/app/lib/methods/helpers/openLink.ts +++ b/app/lib/methods/helpers/openLink.ts @@ -5,6 +5,7 @@ import parse from 'url-parse'; import { themes } from '../../constants'; import { TSupportedThemes } from '../../../theme'; import UserPreferences from '../userPreferences'; +import addProtocol from './addProtocol'; export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY'; @@ -36,13 +37,6 @@ const appSchemeURL = (url: string, browser: string): string => { 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 => { url = addProtocol(url); try {