diff --git a/test/customElements/renderCustomElements.test.ts b/test/customElements/renderCustomElements.test.ts new file mode 100644 index 0000000..f8f8da7 --- /dev/null +++ b/test/customElements/renderCustomElements.test.ts @@ -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() + 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 = '

Hello World

' + 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', '

HTML text

'] + 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]) + }) + }) +})