1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-15 05:19:58 +00:00
elk/tests/nuxt/content-rich.test.ts

269 lines
14 KiB
TypeScript
Raw Permalink Normal View History

/* eslint perfectionist/sort-imports: "off" */
// to prevent error when importing '@nuxt/test-utils/runtime' before 'vitest'
// ref. #2984 chore(deps): update dependency @antfu/eslint-config to v3 by renovate[bot]
// https://github.com/elk-zone/elk/pull/2984
2023-01-18 15:59:37 +00:00
import type { mastodon } from 'masto'
import { format } from 'prettier'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { mockComponent } from '@nuxt/test-utils/runtime'
import { renderToString } from 'vue/server-renderer'
2023-01-29 13:20:37 +00:00
import type { ContentParseOptions } from '~/composables/content-parse'
import { contentToVNode } from '~/composables/content-render'
2022-11-25 08:25:56 +00:00
2024-02-24 16:46:14 +00:00
beforeEach(() => {
publicServer.value = useRuntimeConfig().public.defaultServer
})
describe('content-rich', () => {
2022-11-25 08:56:14 +00:00
it('empty', async () => {
const { formatted } = await render('')
expect(formatted).toMatchSnapshot()
})
2023-01-16 20:41:37 +00:00
it('plain text', async () => {
const { formatted } = await render('hello there', { collapseMentionLink: true })
expect(formatted).toMatchSnapshot()
})
2022-11-25 08:56:14 +00:00
it('link + mention', async () => {
// https://fosstodon.org/@ayo/109383002937620723
const { formatted } = await render('<p>Happy 🤗 were now using <span class="h-card"><a href="https://webtoo.ls/@vitest" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>vitest</span></a></span> (migrated from chai+mocha) <a href="https://github.com/ayoayco/astro-reactive-library/pull/203" rel="nofollow noopener noreferrer" target="_blank"><span class="invisible">https://</span><span class="ellipsis">github.com/ayoayco/astro-react</span><span class="invisible">ive-library/pull/203</span></a></p>')
2022-11-25 08:56:14 +00:00
expect(formatted).toMatchSnapshot()
})
it ('block with backticks', async () => {
const { formatted } = await render('<p>```<br />[(`number string) (`tag string)]<br />```</p>')
expect(formatted).toMatchSnapshot()
})
it('group mention', async () => {
2023-01-13 00:08:56 +00:00
const { formatted } = await render('<p><span class="h-card"><a href="https://lemmy.ml/c/pilipinas" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>pilipinas</span></a></span></p>', {
mentions: [{ id: '', username: 'pilipinas', url: 'https://lemmy.ml/c/pilipinas', acct: 'pilipinas@lemmy.ml' }],
})
expect(formatted).toMatchSnapshot('html')
})
it('inline code with link', async () => {
const { formatted } = await render('<p>Inline code with link: `<a href="https://api.iconify.design/noto.css?icons=1st-place-medal,2nd-place-medal" target="_blank" rel="nofollow noopener noreferrer" class="status-link unhandled-link" title="https://api.iconify.design/noto.css?icons=1st-place-medal,2nd-place-medal"><span class="invisible">https://</span><span class="ellipsis">api.iconify.design/noto.css?ic</span><span class="invisible">ons=1st-place-medal,2nd-place-medal</span></a>`</p>')
expect(formatted).toMatchSnapshot()
})
it('handles html within code blocks', async () => {
const { formatted } = await render('<p>HTML block code:<br>```html<br>&lt;span class="icon--noto icon--noto--1st-place-medal"&gt;&lt;/span&gt;<br>&lt;span class="icon--noto icon--noto--2nd-place-medal-medal"&gt;&lt;/span&gt;<br>```</p>')
expect(formatted).toMatchSnapshot()
})
it('handles formatting from servers', async () => {
const { formatted } = await render('<h1>Fedi HTML Support Survey</h1><p>Does the following formatting come through accurately for you?</p><ul><li>This is an indented bulleted list (not just asterisks).</li><li><strong>This line is bold.</strong></li><li><em>This line is italic.</em></li></ul><ol><li>This list...</li><li>...is numbered and indented</li></ol><h1>This line is larger.</h1>')
expect(formatted).toMatchSnapshot()
})
2022-11-25 08:56:14 +00:00
it('custom emoji', async () => {
const { formatted } = await render('Daniel Roe :nuxt:', {
2023-01-13 00:08:56 +00:00
emojis: {
nuxt: {
shortcode: 'nuxt',
url: 'https://media.webtoo.ls/custom_emojis/images/000/000/366/original/73330dfc9dda4078.png',
staticUrl: 'https://media.webtoo.ls/custom_emojis/images/000/000/366/original/73330dfc9dda4078.png',
2023-01-13 00:08:56 +00:00
visibleInPicker: true,
},
2022-11-25 08:56:14 +00:00
},
})
expect(formatted).toMatchSnapshot()
})
it('code frame', async () => {
// https://webtoo.ls/@antfu/109396489827394721
2022-11-25 08:56:14 +00:00
const { formatted } = await render('<p>Testing code block</p><p>```ts<br />import { useMouse, usePreferredDark } from &#39;@vueuse/core&#39;</p><p>// tracks mouse position<br />const { x, y } = useMouse()</p><p>// is the user prefers dark theme<br />const isDark = usePreferredDark()<br />```</p>')
expect(formatted).toMatchSnapshot()
})
2022-11-27 06:16:02 +00:00
it('code frame 2', async () => {
const { formatted } = await render('<p><span class=\"h-card\"><a href=\"https://webtoo.ls/@antfu\" class=\"u-url mention\">@<span>antfu</span></a></span> Testing<br />```ts<br />const a = hello<br />```</p>')
2022-11-27 06:16:02 +00:00
expect(formatted).toMatchSnapshot()
})
it('code frame no lang', async () => {
const { formatted } = await render('<p>```<br />hello world<br />```<br />no lang</p>')
expect(formatted).toMatchSnapshot()
})
it('code frame empty', async () => {
const { formatted } = await render('<p>```<br /><br />```<br /></p>')
expect(formatted).toMatchSnapshot()
})
2023-01-13 00:08:56 +00:00
2023-01-18 07:01:50 +00:00
it('collapse mentions', async () => {
2023-01-13 00:08:56 +00:00
const { formatted } = await render('<p><span class="h-card"><a href="https://m.webtoo.ls/@elk" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>elk</span></a></span> <span class="h-card"><a href="https://m.webtoo.ls/@elk" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>elk</span></a></span> content <span class="h-card"><a href="https://m.webtoo.ls/@antfu" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>antfu</span></a></span> <span class="h-card"><a href="https://mastodon.roe.dev/@daniel" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>daniel</span></a></span> <span class="h-card"><a href="https://m.webtoo.ls/@sxzz" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>sxzz</span></a></span> <span class="h-card"><a href="https://m.webtoo.ls/@patak" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>patak</span></a></span> content</p>', {
collapseMentionLink: true,
})
expect(formatted).toMatchSnapshot()
2023-01-13 00:08:56 +00:00
})
2023-01-18 15:59:37 +00:00
it('hides collapsed mentions', async () => {
const { formatted } = await render('<p><span class="h-card"><a href="https://m.webtoo.ls/@elk" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>elk</span></a></span> content</p>', {
collapseMentionLink: true,
inReplyToStatus: { account: { acct: 'elk@webtoo.ls' }, mentions: [] as mastodon.v1.StatusMention[] } as mastodon.v1.Status,
})
expect(formatted).toMatchSnapshot()
2023-01-18 15:59:37 +00:00
})
it('shows some collapsed mentions inline', async () => {
2023-01-18 15:59:37 +00:00
const { formatted } = await render('<p><span class="h-card"><a href="https://m.webtoo.ls/@elk" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>elk</span></a></span> <span class="h-card"><a href="https://m.webtoo.ls/@antfu" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>antfu</span></a></span> content</p>', {
collapseMentionLink: true,
inReplyToStatus: { account: { acct: 'elk@webtoo.ls' }, mentions: [] as mastodon.v1.StatusMention[] } as mastodon.v1.Status,
})
expect(formatted).toMatchSnapshot()
})
it('shows some collapsed mentions grouped', async () => {
const { formatted } = await render('<p><span class="h-card"><a href="https://m.webtoo.ls/@elk" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>elk</span></a></span> <span class="h-card"><a href="https://m.webtoo.ls/@antfu" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>antfu</span></a></span> <span class="h-card"><a href="https://m.webtoo.ls/@patak" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>patak</span></a></span> <span class="h-card"><a href="https://m.webtoo.ls/@sxzz" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>sxzz</span></a></span>content</p>', {
collapseMentionLink: true,
inReplyToStatus: { account: { acct: 'elk@webtoo.ls' }, mentions: [] as mastodon.v1.StatusMention[] } as mastodon.v1.Status,
})
expect(formatted).toMatchSnapshot()
2023-01-18 15:59:37 +00:00
})
it ('block with injected html, without language', async () => {
const { formatted } = await render(`
<pre>
<code>
&lt;a href="javascript:alert(1)">click me&lt;/a>
</code>
</pre>
`)
expect(formatted).toMatchSnapshot()
})
it ('block with injected html, with an unknown language', async () => {
const { formatted } = await render(`
<pre>
<code class="language-xyzzy">
&lt;a href="javascript:alert(1)">click me&lt;/a>
</code>
</pre>
`)
expect(formatted).toMatchSnapshot()
})
it ('block with injected html, with a known language', async () => {
const { formatted } = await render(`
<pre>
<code class="language-js">
&lt;a href="javascript:alert(1)">click me&lt;/a>
</code>
</pre>
`)
expect(formatted).toMatchSnapshot()
})
it ('hashtag adds bdi', async () => {
const { formatted } = await render(`
<p>Testing bdi is added <a href="https://universeodon.com/tags/turkey" class="mention hashtag" rel="tag">#<span>turkey</span></a></p>
`)
expect(formatted).toMatchSnapshot()
})
// REVIEW: there is something wrong with this test in the rendered output, missing bdi content, ultrahtml parses it correctly
it ('hashtag doesn\'t add 2 bdi', async () => {
const { formatted } = await render(`
<p>Testing bdi not added <a href="https://universeodon.com/tags/turkey" class="mention hashtag" rel="tag"><bdi>#<span>turkey</span></bdi></a></p>
`)
expect(formatted).toMatchSnapshot()
})
it ('root p includes dir="auto" attr when mixed content', async () => {
const { formatted } = await render(`
<p>هذا اختبار جديد 🐦🐤 <span class="h-card"><a href="https://strangeobject.space/@bebatjof" class="u-url mention">@<span>bebatjof</span></a></span> <br />أنا أحب الطريقة التي يتم بها دعم النموذج المزدوج العربي. تمت ترجمة الكلمة الأخيرة بشكل خاطئ وأحاول العثور على كيفية إصلاحها. 🐦🐤 ;). كما أن النموذج الخاص بـ 0 يحتاج إلى إصلاح <a href="https://m.webtoo.ls/tags/turkey" class="mention hashtag" rel="tag">#<span>turkey</span></a> <a href="https://m.webtoo.ls/tags/%D8%A7%D9%84%D8%B9%D8%B1%D8%A8%D9%8A%D8%A9" class="mention hashtag" rel="tag">#<span>العربية</span></a> .</p><p>This is a new test 🐦🐤 <span class="h-card"><a href="https://strangeobject.space/@bebatjof" class="u-url mention">@<span>bebatjof</span></a></span> <br />I like how the arabic dual form is supported. The last one is mistranslated and I&#39;m trying to find how to fix it. 🐦🐤 ;). Also, the form for 0 needs to be fixed <a href="https://m.webtoo.ls/tags/turkey" class="mention hashtag" rel="tag">#<span>turkey</span></a> <a href="https://m.webtoo.ls/tags/%D8%A7%D9%84%D8%B9%D8%B1%D8%A8%D9%8A%D8%A9" class="mention hashtag" rel="tag">#<span>العربية</span></a> .</p>
`)
expect(formatted).toMatchSnapshot()
})
it ('p moved to div and text children replaced with p[dir="auto"] tags: br children removed', async () => {
const { formatted } = await render(`
<p><span class="h-card"><a href="https://strangeobject.space/@bebatjof" class="u-url mention">@<span>bebatjof</span></a></span> هذا اختبار:<br />أنا أحب الطريقة التي يتم بها دعم النموذج المزدوج العربي. تمت ترجمة الكلمة الأخيرة بشكل خاطئ وأحاول العثور على كيفية إصلاحها. أيضًا، يجب إصلاح نموذج 0.</p><p>This is a test:<br />I like how the arabic dual form is supported. The last one is mistranslated and I&#39;m trying to find how to fix it. Also, the form for 0 needs to be fixed.</p>
`)
expect(formatted).toMatchSnapshot()
})
2022-11-25 08:56:14 +00:00
})
describe('editor', () => {
it('transform mentions', () => {
const ast = parseMastodonHTML('<p><span class="h-card"><a href="https://m.webtoo.ls/@elk" class="u-url mention">@<span>elk</span></a></span> Hello</p>')
const transformed = treeToText(ast)
expect(transformed).toMatchSnapshot()
})
})
2023-01-13 00:08:56 +00:00
async function render(content: string, options?: ContentParseOptions) {
const vnode = contentToVNode(content, options)
2022-11-25 08:25:56 +00:00
const html = (await renderToString(vnode))
.replace(/<!--[[\]]-->/g, '')
2022-11-27 06:16:02 +00:00
let formatted = ''
try {
formatted = await format(html, {
2022-11-27 06:16:02 +00:00
parser: 'html',
})
}
catch {
2022-11-27 06:16:02 +00:00
formatted = html
}
2022-11-25 08:56:14 +00:00
2022-11-25 08:25:56 +00:00
return {
vnode,
html,
2022-11-25 08:56:14 +00:00
formatted,
2022-11-25 08:25:56 +00:00
}
}
2022-11-25 08:56:14 +00:00
// mocks
2023-08-01 10:36:19 +01:00
vi.mock('vue-router', async () => {
2023-01-26 11:46:13 +00:00
const { defineComponent, h } = await import('vue')
2022-11-25 08:56:14 +00:00
return {
RouterLink: defineComponent({
setup(props, { slots }) {
return () => h('a', props, { default: () => slots?.default?.() })
},
2022-11-25 08:56:14 +00:00
}),
}
})
vi.mock('@vueuse/shared', async () => {
const vueuseShared = await import('@vueuse/shared')
// mock pausableWatch and watchPausable: vitest process hangs from time to time
return {
...vueuseShared,
pausableWatch: () => {
return {
stop: () => {},
pause: () => {},
resume: () => {},
isActive: readonly(ref(true)),
}
},
watchPausable: () => {
return {
stop: () => {},
pause: () => {},
resume: () => {},
isActive: readonly(ref(true)),
}
},
}
})
2023-01-30 09:36:39 +00:00
mockComponent('ContentMentionGroup', {
setup(props, { slots }) {
return () => h('mention-group', null, { default: () => slots?.default?.() })
},
2023-01-13 00:08:56 +00:00
})
2023-01-29 13:20:37 +00:00
mockComponent('AccountHoverWrapper', {
props: ['handle', 'class'],
setup(_, { slots }) {
return () => slots?.default?.()
},
2022-11-30 07:08:10 +00:00
})