Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
fago committed Dec 22, 2024
1 parent bdf4eec commit 0c9cd84
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions test/customElements/renderCustomElements.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// test/customElements/renderCustomElements.test.ts
import { describe, it, expect } from 'vitest'
import { setup } from '@nuxt/test-utils'
import { fileURLToPath } from 'url'
import { useNuxtApp } from '#app'

describe('renderCustomElements', async () => {
await setup({
rootDir: fileURLToPath(new URL('../../playground', import.meta.url)),
configFile: 'nuxt.config4test',
port: 3001,
})

it('handles null input', async () => {
const { $drupalCe } = useNuxtApp()
const result = $drupalCe.renderCustomElements(null)
expect(result).toBe(null)
})

it('handles undefined input', async () => {
const { $drupalCe } = useNuxtApp()
const result = $drupalCe.renderCustomElements(undefined)
expect(result).toBe(null)
})

it('handles empty object input', async () => {
const { $drupalCe } = useNuxtApp()
const result = $drupalCe.renderCustomElements({})
expect(result).toBe(null)
})

it('handles plain string input', async () => {
const { $drupalCe } = useNuxtApp()
const result = $drupalCe.renderCustomElements('Hello World')
expect(result).toMatchObject({
template: expect.stringContaining('v-html'),
data: expect.any(Function)
})
expect(result.data().content).toBe('Hello World')
})

it('handles HTML string input', async () => {
const { $drupalCe } = useNuxtApp()
const htmlString = '<p>Hello <strong>World</strong></p>'
const result = $drupalCe.renderCustomElements(htmlString)
expect(result).toMatchObject({
template: expect.stringContaining('v-html'),
data: expect.any(Function)
})
expect(result.data().content).toBe(htmlString)
})

it('handles single custom element object', async () => {
const { $drupalCe } = useNuxtApp()
const customElement = {
element: 'test-component',
props: { foo: 'bar' }
}
const result = $drupalCe.renderCustomElements(customElement)
expect(result).toBeTruthy()
expect(result).toHaveProperty('type')
expect(result).toHaveProperty('props')
expect(result.props).toMatchObject({
foo: 'bar'
})
})

it('handles array of custom elements', async () => {
const { $drupalCe } = useNuxtApp()
const customElements = [
{
element: 'test-component',
props: { foo: 'bar' }
},
'Plain text element',
{
element: 'another-component',
props: { baz: 'qux' }
}
]
const result = $drupalCe.renderCustomElements(customElements)
expect(Array.isArray(result)).toBe(true)
expect(result).toHaveLength(3)

expect(result[0]).toHaveProperty('type')
expect(result[0]).toHaveProperty('props')
expect(result[0].props).toMatchObject({
foo: 'bar'
})

expect(result[1]).toMatchObject({
template: expect.stringContaining('v-html'),
data: expect.any(Function)
})
expect(result[1].data().content).toBe('Plain text element')

expect(result[2]).toHaveProperty('type')
expect(result[2]).toHaveProperty('props')
expect(result[2].props).toMatchObject({
baz: 'qux'
})
})

it('handles array with only string elements', async () => {
const { $drupalCe } = useNuxtApp()
const elements = ['Text 1', '<p>HTML text</p>']
const result = $drupalCe.renderCustomElements(elements)
expect(Array.isArray(result)).toBe(true)
expect(result).toHaveLength(2)

result.forEach((component, index) => {
expect(component).toMatchObject({
template: expect.stringContaining('v-html'),
data: expect.any(Function)
})
expect(component.data().content).toBe(elements[index])
})
})
})

0 comments on commit 0c9cd84

Please sign in to comment.