From bd187da8a531e470aa53b16057313044f589ba8e Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sat, 21 Sep 2024 14:20:24 -0500 Subject: [PATCH] Add html test coverage --- __tests__/html.test.ts | 17 +++++++++++++++++ src/photo/PhotoGridSidebar.tsx | 4 ++-- src/utility/html.ts | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 __tests__/html.test.ts diff --git a/__tests__/html.test.ts b/__tests__/html.test.ts new file mode 100644 index 00000000..9468d976 --- /dev/null +++ b/__tests__/html.test.ts @@ -0,0 +1,17 @@ +import { htmlHasBrParagraphBreaks, safelyParseFormattedHtml } from '@/utility/html'; +import { parameterize } from '@/utility/string'; + +describe('HTML', () => { + it('safely parses', () => { + expect(safelyParseFormattedHtml('

TEXT

')).toBe('TEXT'); + expect(safelyParseFormattedHtml('TEXT')).toBe('TEXT'); + }); + it('detects br-style paragraph breaks', () => { + expect(htmlHasBrParagraphBreaks('TEXT

')).toBeTruthy(); + expect(htmlHasBrParagraphBreaks('TEXT

')).toBeTruthy(); + expect(htmlHasBrParagraphBreaks('TEXT

')).toBeTruthy(); + expect(htmlHasBrParagraphBreaks('TEXT')).toBeFalsy(); + expect(htmlHasBrParagraphBreaks('TEXT
')).toBeFalsy(); + expect(htmlHasBrParagraphBreaks('TEXT
')).toBeFalsy(); + }); +}); diff --git a/src/photo/PhotoGridSidebar.tsx b/src/photo/PhotoGridSidebar.tsx index 0d1f7b2c..b45dbfac 100644 --- a/src/photo/PhotoGridSidebar.tsx +++ b/src/photo/PhotoGridSidebar.tsx @@ -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), }} />]} />} diff --git a/src/utility/html.ts b/src/utility/html.ts index 0f3422f7..9f977a44 100644 --- a/src/utility/html.ts +++ b/src/utility/html.ts @@ -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, });