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: add tests for normalizeCommentHTML #455

Merged
merged 2 commits into from
Mar 11, 2024
Merged
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
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
86 changes: 84 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,80 @@ describe('Normalization', () => {
)
})
})

describe('Normalization: Comment', () => {
test('quote', () => {
expectNormalizeCommentHTML(
'<blockquote><p>abc</p></blockquote>',
'<blockquote><p>abc</p></blockquote>',
)
})

test('link', () => {
expectNormalizeCommentHTML(
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="https://example.com">abc</a></p>',
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="https://example.com">abc</a></p>',
)
})

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>',
)
})
})
Loading