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

Feat: Refacto test #139

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
58 changes: 58 additions & 0 deletions __tests__/src/utils/newsletter.test.js
Original file line number Diff line number Diff line change
@@ -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 = '<h1>Test</h1>';
const expected = '<h1 style="color: red;">Test</h1>';
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 =
'<body><div><h1><span>Test</span></h1></div><h2>Test</h2><h3>Test</h3><p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, voluptatum.</p><img src="https://picsum.photos/200/300" alt="random image" /><a href="https://www.google.com">Google</a>';
const expected =
'<body><div><h1 style="color: red; font-size: 2rem; font-family: Arial; font-weight: bold; border: 1px solid black; border-radius: 5px; padding: 10px; margin: 10px; padding-top: 20px;"><span>Test</span></h1></div><h2>Test</h2><h3>Test</h3><p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, voluptatum.</p><img src="https://picsum.photos/200/300" alt="random image" /><a href="https://www.google.com">Google</a>';
const result = applyInlineStyleToRawHtml(rawHtml, theme);
expect(result).toBe(expected);
});

test('should not apply style on <img />, <a> 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 =
'<img src="https://picsum.photos/200/300" alt="random image" /><a href="https://www.google.com">Google</a>';
const expected =
'<img src="https://picsum.photos/200/300" alt="random image" /><a href="https://www.google.com">Google</a>';
const result = applyInlineStyleToRawHtml(rawHtml, theme);
expect(result).toBe(expected);
});
});
42 changes: 42 additions & 0 deletions __tests__/src/utils/string.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
});
33 changes: 33 additions & 0 deletions __tests__/src/utils/url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getDomainFromUrl, isPdfUrl } 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');
});
});

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);
});
});
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utils/newsletter/index.ts → src/utils/newsletter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import baseTheme from '../../emails/theme';
import baseTheme from '@/emails/theme';

type Theme = {
[key: string]: {
Expand Down
54 changes: 0 additions & 54 deletions src/utils/newsletter/index.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '*');
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Loading