Add html test coverage

This commit is contained in:
Sam Becker 2024-09-21 14:20:24 -05:00
parent b48697288e
commit bd187da8a5
3 changed files with 20 additions and 3 deletions

17
__tests__/html.test.ts Normal file
View File

@ -0,0 +1,17 @@
import { htmlHasBrParagraphBreaks, safelyParseFormattedHtml } from '@/utility/html';
import { parameterize } from '@/utility/string';
describe('HTML', () => {
it('safely parses', () => {
expect(safelyParseFormattedHtml('<p>TEXT</p>')).toBe('TEXT');
expect(safelyParseFormattedHtml('<b>TEXT</b>')).toBe('<b>TEXT</b>');
});
it('detects br-style paragraph breaks', () => {
expect(htmlHasBrParagraphBreaks('TEXT<br><br>')).toBeTruthy();
expect(htmlHasBrParagraphBreaks('TEXT<br /><br />')).toBeTruthy();
expect(htmlHasBrParagraphBreaks('TEXT<br><br />')).toBeTruthy();
expect(htmlHasBrParagraphBreaks('TEXT')).toBeFalsy();
expect(htmlHasBrParagraphBreaks('TEXT<br/>')).toBeFalsy();
expect(htmlHasBrParagraphBreaks('TEXT<br />')).toBeFalsy();
});
});

View File

@ -16,7 +16,7 @@ import { useAppState } from '@/state/AppState';
import { useMemo } from 'react';
import HiddenTag from '@/tag/HiddenTag';
import { SITE_ABOUT } from '@/site/config';
import { htmlHasBrParagraphBreaks, safelyParseFormattedHTML } from '@/utility/html';
import { htmlHasBrParagraphBreaks, safelyParseFormattedHtml } from '@/utility/html';
import { clsx } from 'clsx/lite';
export default function PhotoGridSidebar({
@ -50,7 +50,7 @@ export default function PhotoGridSidebar({
htmlHasBrParagraphBreaks(SITE_ABOUT) && 'pb-2',
)}
dangerouslySetInnerHTML={{
__html: safelyParseFormattedHTML(SITE_ABOUT),
__html: safelyParseFormattedHtml(SITE_ABOUT),
}}
/>]}
/>}

View File

@ -2,7 +2,7 @@ import sanitizeHtml from 'sanitize-html';
const ALLOWED_FORMATTING_TAGS = ['b', 'strong', 'i', 'em', 'u', 'br'];
export const safelyParseFormattedHTML = (text: string) =>
export const safelyParseFormattedHtml = (text: string) =>
sanitizeHtml(text, {
allowedTags: ALLOWED_FORMATTING_TAGS,
});