From aee7cd88487c37413e631c5afc097adccd55428f Mon Sep 17 00:00:00 2001 From: quentingrchr Date: Fri, 21 Jun 2024 17:48:12 +0200 Subject: [PATCH 1/2] refacto test, move test folder --- .../src/utils/link.test.js | 2 +- __tests__/src/utils/newsletter.test.js | 58 +++++++++++++++++++ __tests__/src/utils/url.test.js | 14 +++++ src/utils/{link/index.ts => link.ts} | 0 .../{newsletter/index.ts => newsletter.ts} | 2 +- src/utils/newsletter/index.test.js | 54 ----------------- 6 files changed, 74 insertions(+), 56 deletions(-) rename src/utils/link/index.test.js => __tests__/src/utils/link.test.js (98%) create mode 100644 __tests__/src/utils/newsletter.test.js create mode 100644 __tests__/src/utils/url.test.js rename src/utils/{link/index.ts => link.ts} (100%) rename src/utils/{newsletter/index.ts => newsletter.ts} (98%) delete mode 100644 src/utils/newsletter/index.test.js diff --git a/src/utils/link/index.test.js b/__tests__/src/utils/link.test.js similarity index 98% rename from src/utils/link/index.test.js rename to __tests__/src/utils/link.test.js index 9234893..e35870e 100644 --- a/src/utils/link/index.test.js +++ b/__tests__/src/utils/link.test.js @@ -1,5 +1,5 @@ +import { getTweetId, isTwitterLink } from '@/utils/link'; import { describe, expect, test } from '@jest/globals'; -import { isTwitterLink, getTweetId } from './index'; describe('isTwitterLink', () => { test('url to user should return true', () => { diff --git a/__tests__/src/utils/newsletter.test.js b/__tests__/src/utils/newsletter.test.js new file mode 100644 index 0000000..cfb31fb --- /dev/null +++ b/__tests__/src/utils/newsletter.test.js @@ -0,0 +1,58 @@ +import { describe, expect, test } from '@jest/globals'; +import { applyInlineStyleToRawHtml } from '@/utils/newsletter'; + +describe('applyInlineStyleToRawHtml', () => { + test('should apply style to h1 tag', () => { + const theme = { + h1: { + color: 'red', + }, + }; + const rawHtml = '

Test

'; + const expected = '

Test

'; + const result = applyInlineStyleToRawHtml(rawHtml, theme); + expect(result).toBe(expected); + }); + + test('should apply a complex style to only h1 tag', () => { + const theme = { + h1: { + color: 'red', + fontSize: '2rem', + fontFamily: 'Arial', + fontWeight: 'bold', + border: '1px solid black', + borderRadius: '5px', + padding: '10px', + margin: '10px', + paddingTop: '20px', + }, + }; + const rawHtml = + '

Test

Test

Test

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, voluptatum.

random imageGoogle'; + const expected = + '

Test

Test

Test

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, voluptatum.

random imageGoogle'; + const result = applyInlineStyleToRawHtml(rawHtml, theme); + expect(result).toBe(expected); + }); + + test('should not apply style on , tags as they might have attributes that will be erased by the function', () => { + const theme = { + h1: { + color: 'red', + }, + img: { + color: 'red', + }, + a: { + color: 'red', + }, + }; + const rawHtml = + 'random imageGoogle'; + const expected = + 'random imageGoogle'; + const result = applyInlineStyleToRawHtml(rawHtml, theme); + expect(result).toBe(expected); + }); +}); diff --git a/__tests__/src/utils/url.test.js b/__tests__/src/utils/url.test.js new file mode 100644 index 0000000..3f36403 --- /dev/null +++ b/__tests__/src/utils/url.test.js @@ -0,0 +1,14 @@ +import { getDomainFromUrl } from '@/utils/url'; +import { describe, expect } from '@jest/globals'; + +describe('getDomainFromUrl', () => { + it('should extract domain from a valid URL', () => { + const url = 'https://www.example.com/path/to/resource'; + expect(getDomainFromUrl(url)).toBe('example.com'); + }); + + it('should handle URL with protocol', () => { + const url = 'http://subdomain.example.org'; + expect(getDomainFromUrl(url)).toBe('example.org'); + }); +}); diff --git a/src/utils/link/index.ts b/src/utils/link.ts similarity index 100% rename from src/utils/link/index.ts rename to src/utils/link.ts diff --git a/src/utils/newsletter/index.ts b/src/utils/newsletter.ts similarity index 98% rename from src/utils/newsletter/index.ts rename to src/utils/newsletter.ts index 71fbfc2..b9bbcc6 100644 --- a/src/utils/newsletter/index.ts +++ b/src/utils/newsletter.ts @@ -1,4 +1,4 @@ -import baseTheme from '../../emails/theme'; +import baseTheme from '@/emails/theme'; type Theme = { [key: string]: { diff --git a/src/utils/newsletter/index.test.js b/src/utils/newsletter/index.test.js deleted file mode 100644 index 3ced95d..0000000 --- a/src/utils/newsletter/index.test.js +++ /dev/null @@ -1,54 +0,0 @@ -import {describe, expect, test} from '@jest/globals'; -import { applyInlineStyleToRawHtml } from './index'; - -describe('applyInlineStyleToRawHtml', () => { - test('should apply style to h1 tag', () => { - const theme = { - h1: { - color: 'red', - }, - }; - const rawHtml = '

Test

'; - const expected = '

Test

'; - const result = applyInlineStyleToRawHtml(rawHtml, theme); - expect(result).toBe(expected); - }) - - test('should apply a complex style to only h1 tag', () => { - const theme = { - h1: { - color: 'red', - fontSize: '2rem', - fontFamily: 'Arial', - fontWeight: 'bold', - border: '1px solid black', - borderRadius: '5px', - padding: '10px', - margin: '10px', - paddingTop: '20px', - }, - }; - const rawHtml = '

Test

Test

Test

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, voluptatum.

random imageGoogle'; - const expected = '

Test

Test

Test

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, voluptatum.

random imageGoogle'; - const result = applyInlineStyleToRawHtml(rawHtml, theme); - expect(result).toBe(expected); - }) - - test('should not apply style on , tags as they might have attributes that will be erased by the function', () => { - const theme = { - h1: { - color: 'red', - }, - img: { - color: 'red', - }, - a: { - color: 'red', - } - }; - const rawHtml = 'random imageGoogle'; - const expected = 'random imageGoogle'; - const result = applyInlineStyleToRawHtml(rawHtml, theme); - expect(result).toBe(expected); - }) -}); \ No newline at end of file From 68fe77b306fe956c2a875f298b69fe36527afa1c Mon Sep 17 00:00:00 2001 From: quentingrchr Date: Fri, 21 Jun 2024 18:11:41 +0200 Subject: [PATCH 2/2] update tests --- __tests__/src/utils/string.test.js | 42 ++++++++++++++++++++++++++++++ __tests__/src/utils/url.test.js | 21 ++++++++++++++- src/utils/string.ts | 2 +- src/utils/url.ts | 11 +++++++- 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 __tests__/src/utils/string.test.js diff --git a/__tests__/src/utils/string.test.js b/__tests__/src/utils/string.test.js new file mode 100644 index 0000000..583c85f --- /dev/null +++ b/__tests__/src/utils/string.test.js @@ -0,0 +1,42 @@ +import { isStringEmpty, makeHidden } from '@/utils/string'; +import { describe, expect } from '@jest/globals'; + +describe('isStringEmpty', () => { + it('returns true for empty strings', () => { + expect(isStringEmpty('')).toBe(true); + }); + + it('returns true for strings with only spaces', () => { + expect(isStringEmpty(' ')).toBe(true); + }); + + it('returns false for non-empty strings', () => { + expect(isStringEmpty('Hello')).toBe(false); + }); + + it('returns false for strings with spaces', () => { + expect(isStringEmpty(' Hello ')).toBe(false); + }); +}); + +describe('makeHidden', () => { + it('replaces each character with *', () => { + expect(makeHidden('hello')).toBe('*****'); + }); + + it('works with empty string', () => { + expect(makeHidden('')).toBe(''); + }); + + it('replaces each character including spaces', () => { + expect(makeHidden('Hello, world!')).toBe('*************'); + }); + + it('handles Unicode characters', () => { + expect(makeHidden('😊🌟')).toBe('**'); + }); + + it('handles long strings', () => { + expect(makeHidden('a'.repeat(100))).toBe('*'.repeat(100)); + }); +}); diff --git a/__tests__/src/utils/url.test.js b/__tests__/src/utils/url.test.js index 3f36403..64befa7 100644 --- a/__tests__/src/utils/url.test.js +++ b/__tests__/src/utils/url.test.js @@ -1,4 +1,4 @@ -import { getDomainFromUrl } from '@/utils/url'; +import { getDomainFromUrl, isPdfUrl } from '@/utils/url'; import { describe, expect } from '@jest/globals'; describe('getDomainFromUrl', () => { @@ -12,3 +12,22 @@ describe('getDomainFromUrl', () => { expect(getDomainFromUrl(url)).toBe('example.org'); }); }); + +describe('isPdfUrl', () => { + it('should return true for valid PDF URLs', () => { + // Test cases where the URL ends with '.pdf' + expect(isPdfUrl('https://example.com/file.pdf')).toBe(true); + expect(isPdfUrl('http://www.test.com/doc.pdf')).toBe(true); + expect(isPdfUrl('ftp://ftp.site.com/report.pdf')).toBe(true); + }); + + it('should return false for invalid PDF URLs', () => { + // Test cases where the URL does not end with '.pdf' + expect(isPdfUrl('https://example.com/document.pdfx')).toBe(false); + expect(isPdfUrl('http://www.test.com/file.PDF')).toBe(false); // Case-sensitive check + expect(isPdfUrl('ftp://ftp.site.com/reportpdf')).toBe(false); + expect(isPdfUrl('https://example.com/pdf')).toBe(false); + expect(isPdfUrl('https://example.com/.pdf')).toBe(false); + expect(isPdfUrl('https://example.com/pdf.')).toBe(false); + }); +}); diff --git a/src/utils/string.ts b/src/utils/string.ts index f86beb0..3ac0424 100644 --- a/src/utils/string.ts +++ b/src/utils/string.ts @@ -12,7 +12,7 @@ export function isStringEmpty(str: string): boolean { * Function that returns a hidden string, i.e. a string with all characters replaced by '*' */ export function makeHidden(str: string): string { - return str.replace(/./g, '*'); + return str.replace(/[\s\S]/gu, '*'); } /** diff --git a/src/utils/url.ts b/src/utils/url.ts index 7ba5d57..276703b 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -5,4 +5,13 @@ export const getDomainFromUrl = (url: string) => { return domain; }; -export const isPdfUrl = (url: string) => url.indexOf('.pdf') > -1; +export const isPdfUrl = (url: string): boolean => { + const urlObject = new URL(url); + const pathname = urlObject.pathname; + const hasExtension = pathname.endsWith('.pdf'); + if (!hasExtension) return false; + // Edge case for urls like https://example.com/.pdf should not be considered as pdf + const hasFileName = pathname.split('/').pop() !== '.pdf'; + if (!hasFileName) return false; + return hasExtension && hasFileName; +}; \ No newline at end of file