Skip to content

Commit

Permalink
feat: add tests for normalizeCommentHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Mar 11, 2024
1 parent fed703c commit 7b454ae
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/matters-editor",
"version": "0.2.3-alpha.0",
"version": "0.2.3-alpha.1",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
72 changes: 70 additions & 2 deletions src/transformers/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { describe, expect, test } from 'vitest'

import { normalizeArticleHTML } from './normalize'
import { normalizeArticleHTML, normalizeCommentHTML } from './normalize'

const expectNormalizeArticleHTML = (input: string, output: string) => {
const result = normalizeArticleHTML(input)
expect(result.trim()).toBe(output)
}

export const expectNormalizeCommentHTML = (input: string, output: string) => {
const result = normalizeCommentHTML(input)
expect(result.trim()).toBe(output)
}

/**
* Tests
*/
describe('Normalization', () => {
describe('Normalization: Article', () => {
test('bolds', () => {
expectNormalizeArticleHTML(
'<p><strong>abc</strong></p>',
Expand Down Expand Up @@ -279,3 +284,66 @@ describe('Normalization', () => {
)
})
})

describe('Normalization: Comment', () => {
test('bolds is not supported', () => {
expectNormalizeCommentHTML('<p><strong>abc</strong></p>', '<p>abc</p>')
expectNormalizeCommentHTML('<p><b>abc</b></p>', '<p>abc</p>')
})

test('strikethrough is not supported', () => {
expectNormalizeCommentHTML('<p><s>abc</s></p>', '<p>abc</p>')
expectNormalizeCommentHTML('<p><del>abc</del></p>', '<p>abc</p>')
expectNormalizeCommentHTML('<p><strike>abc</strike></p>', '<p>abc</p>')
})

test('italic is not supported', () => {
expectNormalizeCommentHTML('<p>abc</p>', '<p>abc</p>')

expectNormalizeCommentHTML('<p><i>abc</i></p>', '<p>abc</p>')
})

test('underline is not supported', () => {
expectNormalizeCommentHTML('<p><u>abc</u></p>', '<p>abc</p>')
})

test('self-closed tags', () => {
expectNormalizeCommentHTML('<p />', '<p></p>')

expectNormalizeCommentHTML('<br></br>', '<p><br class="smart"></p>')

expectNormalizeCommentHTML('<hr/>', '<p></p>')

// <img /> -> <img>
expectNormalizeCommentHTML(
'<figure class="image"><img src="https://assets.matters.news/embed/c40d5045-0c03-44b6-afe6-93a285ffd1bb.jpeg" /><figcaption>左:女反派。右:女主。</figcaption></figure>',
'<p>左:女反派。右:女主。</p>',
)

// <iframe /> -> <iframe></iframe>
expectNormalizeCommentHTML(
'<figure class="embed" data-provider="youtube"><div class="iframe-container"><iframe src="https://www.youtube.com/embed/Zk7DppcfaMY?rel=0" loading="lazy" allowfullscreen frameborder="0" /></div><figcaption></figcaption></figure>',
'<p></p>',
)
})

test('figures are not supported', () => {
// image
expectNormalizeCommentHTML(
'<figure class="image"><img src="https://assets.matters.news/embed/c40d5045-0c03-44b6-afe6-93a285ffd1bb.jpeg"><figcaption>左:女反派。右:女主。</figcaption></figure>',
'<p>左:女反派。右:女主。</p>',
)

// audio
expectNormalizeCommentHTML(
'<figure class="audio"><audio controls><source src="https://assets.matters.news/embedaudio/0a45d56a-d19a-4300-bfa4-305639fd5a82/點數經濟-讓過路客成為回頭客.mp3" type="audio/mp3"></audio><div class="player"><header><div class="meta"><h4 class="title">點數經濟:讓過路客成為回頭客</h4><div class="time"><span class="current" data-time="00:00"></span><span class="duration" data-time="--:--"></span></div></div><span class="play"></span></header><footer><div class="progress-bar"><span></span></div></footer></div><figcaption>區塊勢 Podcast</figcaption></figure>',
'<p>點數經濟:讓過路客成為回頭客</p><p>區塊勢 Podcast</p>',
)

// video
expectNormalizeCommentHTML(
'<figure class="embed embed-video" data-provider="youtube"><div class="iframe-container"><iframe src="https://www.youtube.com/embed/Zk7DppcfaMY?rel=0" loading="lazy" allowfullscreen frameborder="0"></iframe></div><figcaption></figcaption></figure>',
'<p></p>',
)
})
})

0 comments on commit 7b454ae

Please sign in to comment.