From 1dfe898bd1bc919b197e56086261262fa24d4363 Mon Sep 17 00:00:00 2001 From: Wolfgang Ziegler Date: Sun, 29 Dec 2024 22:08:39 -0600 Subject: [PATCH] refactor: #281: convert component default handling test coverage to unit tests (#286) * refactor: #281: convert component default handling test coverage to unit tests * remove old ones --- .../defaultFallbackRenders.test.ts | 16 ----- test/customElements/nonDefaultRenders.test.ts | 25 ------- .../renderCustomElements-defaults.test.ts | 68 +++++++++++++++++++ 3 files changed, 68 insertions(+), 41 deletions(-) delete mode 100644 test/customElements/defaultFallbackRenders.test.ts delete mode 100644 test/customElements/nonDefaultRenders.test.ts create mode 100644 test/unit/renderCustomElements-defaults.test.ts diff --git a/test/customElements/defaultFallbackRenders.test.ts b/test/customElements/defaultFallbackRenders.test.ts deleted file mode 100644 index f2da2d7..0000000 --- a/test/customElements/defaultFallbackRenders.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { fileURLToPath } from 'node:url' -import { describe, it, expect } from 'vitest' -import { setup, $fetch } from '@nuxt/test-utils' - -describe('Missing custom element with default fallback', async () => { - await setup({ - rootDir: fileURLToPath(new URL('../../playground', import.meta.url)), - configFile: 'nuxt.config4test', - port: 3001, - }) - it('fallback is rendered correctly', async () => { - const html = await $fetch('/node/4') - expect(html).toContain('
node--default') - }) -}) diff --git a/test/customElements/nonDefaultRenders.test.ts b/test/customElements/nonDefaultRenders.test.ts deleted file mode 100644 index 459616c..0000000 --- a/test/customElements/nonDefaultRenders.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { fileURLToPath } from 'node:url' -import { describe, it, expect } from 'vitest' -import { setup, $fetch } from '@nuxt/test-utils' -import DrupalCe from '../..' - -describe('Non-default custom element', async () => { - await setup({ - rootDir: fileURLToPath(new URL('../fixtures/debug', import.meta.url)), - nuxtConfig: { - modules: [ - DrupalCe, - ], - drupalCe: { - drupalBaseUrl: 'http://127.0.0.1:3103', - ceApiEndpoint: '/api', - }, - }, - port: 3103, - }) - it('non-default custom element is rendered correctly', async () => { - const html = await $fetch('/node/4') - expect(html).toContain('
{ + const NodeArticleDemo = defineComponent({ + name: 'NodeArticleDemo', + props: { content: String }, + template: `

Article Demo Component

{{ content }}
` + }) + + const NodeArticleDefault = defineComponent({ + name: 'NodeArticleDefault', + props: { content: String }, + template: `

Article Default Component

{{ content }}
` + }) + + const NodeDefault = defineComponent({ + name: 'NodeDefault', + props: { content: String }, + template: `

Node Default Component

{{ content }}
` + }) + + const app = useNuxtApp() + app.vueApp.component('NodeArticleDemo', NodeArticleDemo) + app.vueApp.component('NodeArticleDefault', NodeArticleDefault) + app.vueApp.component('NodeDefault', NodeDefault) + + const createTestComponent = (elementData) => defineComponent({ + setup() { + const { renderCustomElements } = useDrupalCe() + return { component: renderCustomElements(elementData) } + }, + template: '' + }) + + it('renders specific component when available', async () => { + const elementData = { + element: 'node-article-demo', + content: 'Article demo' + } + const wrapper = await mountSuspended(createTestComponent(elementData)) + expect(wrapper.html()).toContain('Article demo') + expect(wrapper.findComponent(NodeArticleDemo).exists()).toBe(true) + }) + + it('falls back to node-article--default when specific component not found', async () => { + const elementData = { + element: 'node-article-custom', + content: 'Testing intermediate fallback' + } + const wrapper = await mountSuspended(createTestComponent(elementData)) + expect(wrapper.html()).toContain('Testing intermediate fallback') + expect(wrapper.findComponent(NodeArticleDefault).exists()).toBe(true) + }) + + it('falls back to node--default when no intermediate default found', async () => { + const elementData = { + element: 'node-special-custom', + content: 'Testing final fallback' + } + const wrapper = await mountSuspended(createTestComponent(elementData)) + expect(wrapper.html()).toContain('Testing final fallback') + expect(wrapper.findComponent(NodeDefault).exists()).toBe(true) + }) +})