Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: proper use of slots for the node default template #283

Merged
merged 17 commits into from
Dec 28, 2024
32 changes: 17 additions & 15 deletions playground/components/global/node--default.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<template>
<div class="node node--default">
<h2>Node: {{ title }}</h2>
<div
v-if="image"
v-html="image.content"
/>
<div
v-if="body"
v-html="body"
/>
<div class="node">
<h2 v-if="title">Node: {{ title }}</h2>
<slot name="image">
<component :is="useDrupalCe().renderCustomElements($attrs.image)" />
</slot>
<slot name="body">
<component :is="useDrupalCe().renderCustomElements($attrs.body)" />
</slot>
</div>
</template>

<script setup lang="ts">
defineSlots<{
body();
image();
}>()
defineProps<{
title: string
type: string
created: number | string
body?: string[]
image?: object
title?: string;
type?: string;
created?: number | string;
// Layout-builder support.
sections?: object;
}>()
</script>
3 changes: 2 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default defineNuxtConfig({
],
compatibilityDate: '2024-12-16',
drupalCe: {
drupalBaseUrl: 'https://8080-drunomics-lupusdecouple-x5qe3h51r0o.ws-eu110.gitpod.io',
drupalBaseUrl: 'http://127.0.0.1:3000',
ceApiEndpoint: '/api',
},
i18n: {
locales: ['en', 'de'],
Expand Down
43 changes: 43 additions & 0 deletions playground/server/api/node/5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// This file is only there for manual testing and used in actual test cases.
export default defineEventHandler(() => {
return {
breadcrumbs: [
{
frontpage: true,
url: '/',
label: 'Home'
}
],
content: {
element: 'node-article',
created: '1734839102',
title: 'Test article',
body: '<p>Some <b>example</b> body.</p>',
comment: {},
image: ' <img loading="lazy" src="https://placehold.co/600x400" width="600" height="400" alt="test" />\n\n\n'
},
content_format: 'json',
local_tasks: {},
messages: [],
metatags: {
meta: [
{
name: 'title',
content: 'tes adfa fdfdsf | Drush Site-Install'
},
{
name: 'description',
content: 'asdf asdfasdf'
}
],
link: [
{
rel: 'canonical',
href: 'https://lupus-nuxt.ddev.site/custom-error'
}
]
},
page_layout: 'default',
title: 'tes adfa fdfdsf'
}
})
2 changes: 1 addition & 1 deletion test/customElements/defaultFallbackRenders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Missing custom element with default fallback', async () => {
})
it('fallback is rendered correctly', async () => {
const html = await $fetch('/node/4')
expect(html).toContain('<div class="node node--default"')
expect(html).toContain('<div class="node"')
expect(html).toContain('This node is testing custom elements fallback. node-article-demo --> node--default')
})
})
52 changes: 52 additions & 0 deletions test/unit/components/node--default.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @vitest-environment nuxt
import { describe, it, expect } from 'vitest'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { defineComponent } from 'vue'
import { useDrupalCe } from '../../../src/runtime/composables/useDrupalCe'
import NodeDefault from '../../../playground/components/global/node--default.vue'

describe('node--default custom element', () => {
const nodeData = {
element: 'node',
title: 'Test article',
body: '<p>Some <b>example</b> body.</p>',
image: '<img loading="lazy" src="https://placehold.co/600x400" width="600" height="400" alt="test" />'
}

const createNodeComponent = (data = nodeData) => defineComponent({
components: { 'node': NodeDefault },
setup() {
const { renderCustomElements } = useDrupalCe()
return { component: renderCustomElements(data) }
},
template: '<component :is="component" />'
})

it('renders node with all attributes', async () => {
const wrapper = await mountSuspended(createNodeComponent())
expect(wrapper.find('h2').text()).toBe('Node: Test article')
expect(wrapper.html()).toContain('Some <b>example</b> body')
expect(wrapper.html()).toContain('<img loading="lazy" src="https://placehold.co/600x400"')
})

it('renders node without title', async () => {
const wrapper = await mountSuspended(createNodeComponent({
...nodeData,
title: undefined
}))
expect(wrapper.find('h2').exists()).toBe(false)
expect(wrapper.html()).toContain('Some <b>example</b> body')
expect(wrapper.html()).toContain('<img loading="lazy" src="https://placehold.co/600x400"')
})

it('renders node with only title while body and image are undefined', async () => {
const wrapper = await mountSuspended(createNodeComponent({
element: 'node',
title: 'Just a title'
}))
expect(wrapper.find('h2').text()).toBe('Node: Just a title')
expect(wrapper.find('.node').attributes('element')).toBe('node')
expect(wrapper.find('img').exists()).toBe(false)
expect(wrapper.find('p').exists()).toBe(false)
})
})
3 changes: 1 addition & 2 deletions test/unit/renderCustomElements.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// test/customElements/renderCustomElements.test.ts
// @vitest-environment nuxt
import { describe, it, expect } from 'vitest'
import { mountSuspended } from '@nuxt/test-utils/runtime'
Expand All @@ -13,7 +12,7 @@ describe('renderCustomElements', () => {
props: {
foo: String
},
template: '<div>Test Component: {{ foo || "" }}</div>'
template: '<div>Test Component: {{ foo }}</div>'
})

const AnotherComponent = defineComponent({
Expand Down
23 changes: 18 additions & 5 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { defineVitestConfig
} from '@nuxt/test-utils/config'
// vitest.config.ts
import { fileURLToPath } from 'node:url'
import { defineVitestConfig } from '@nuxt/test-utils/config'

export default defineVitestConfig
({
// any custom Vitest config if needed
export default defineVitestConfig({
test: {
environmentOptions: {
nuxt: {
rootDir: fileURLToPath(new URL('.', import.meta.url)),
overrides: {
modules: ['./dist/module.mjs'],
drupalCe: {
drupalBaseUrl: 'http://127.0.0.1:3001',
ceApiEndpoint: '/api',
}
}
}
}
}
})
Loading