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 27bd3e0
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions test/customElements/renderCustomElements.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// test/customElements/renderCustomElements.test.ts
import { describe, test, expect } from 'vitest'
import { setup, createPage } from '@nuxt/test-utils'
import { fileURLToPath } from 'url'

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

const { $drupalCe } = useNuxtApp()

Check failure on line 13 in test/customElements/renderCustomElements.test.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

test/customElements/renderCustomElements.test.ts

ReferenceError: useNuxtApp is not defined ❯ test/customElements/renderCustomElements.test.ts:13:25
const { renderCustomElements } = $drupalCe

// ... rest of test cases remain the same ...
test('handles null input', () => {
const result = renderCustomElements(null)
expect(result).toBe(null)
})

test('handles undefined input', () => {
const result = renderCustomElements(undefined)
expect(result).toBe(null)
})

test('handles empty object input', () => {
const result = renderCustomElements({})
expect(result).toBe(null)
})

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

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

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

test('handles array of custom elements', () => {
const customElements = [
{
element: 'test-component',
props: { foo: 'bar' }
},
'Plain text element',
{
element: 'another-component',
props: { baz: 'qux' }
}
]
const result = 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'
})
})

test('handles array with only string elements', () => {
const elements = ['Text 1', '<p>HTML text</p>']
const result = 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 27bd3e0

Please sign in to comment.