feat(exporter): 섹션 엣지 케이스 보강(공백 제거/줄바꿈 토큰/CTA) + 테스트
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { exportPage } from '@/lib/exporter/html'
|
||||||
|
import { pageSchema, type Page } from '@/lib/schema/page'
|
||||||
|
|
||||||
|
function makePage(overrides: Partial<Page> = {}): Page {
|
||||||
|
const base: Page = {
|
||||||
|
title: 'CTA Edge',
|
||||||
|
locale: 'en',
|
||||||
|
theme: { primaryColor: '#2563eb', fontFamily: 'Inter' },
|
||||||
|
sections: [
|
||||||
|
{ type: 'cta', props: { text: ' ', href: '#' } },
|
||||||
|
{ type: 'features', props: { items: ['One'] } },
|
||||||
|
] as Page['sections'],
|
||||||
|
form: { actionUrl: '', fields: [], spamProtection: { honeypotFieldName: '_hp', minSubmitSeconds: 2 } },
|
||||||
|
analytics: { ga4MeasurementId: '', metaPixelId: '', customHeadHtml: '' },
|
||||||
|
seo: { title: 't', description: 'd' },
|
||||||
|
}
|
||||||
|
return pageSchema.parse({ ...base, ...overrides })
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Exporter - CTA edge cases', () => {
|
||||||
|
it('drops CTA section when text is whitespace-only', () => {
|
||||||
|
const out = exportPage(makePage())
|
||||||
|
expect(out.html).not.toContain('<section class="cta"')
|
||||||
|
expect(out.html).not.toContain('<a class="btn-cta"')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders CTA when text is valid after trimming', () => {
|
||||||
|
const out = exportPage(makePage({ sections: [ { type: 'cta', props: { text: ' Buy Now ', href: '#' } } ] as Page['sections'] }))
|
||||||
|
expect(out.html).toContain('<a class="btn-cta" href="#">Buy Now</a>')
|
||||||
|
})
|
||||||
|
})
|
||||||
+26
-10
@@ -5,7 +5,12 @@ export type Exported = { html: string; css: string; js: string }
|
|||||||
export type ExportOptions = { assetManager?: AssetManager }
|
export type ExportOptions = { assetManager?: AssetManager }
|
||||||
|
|
||||||
function renderFaq(section: { props: { items: Array<{ q: string; a: string }> } }) {
|
function renderFaq(section: { props: { items: Array<{ q: string; a: string }> } }) {
|
||||||
const items: Array<{ q: string; a: string }> = Array.isArray(section.props.items) ? section.props.items : []
|
const itemsRaw: Array<{ q: string; a: string }> = Array.isArray(section.props.items) ? section.props.items : []
|
||||||
|
const isBlank = (s: string | undefined | null) => !s || s.trim().length === 0
|
||||||
|
const items = itemsRaw
|
||||||
|
.map((it) => ({ q: (it.q || '').trim(), a: (it.a || '').trim() }))
|
||||||
|
// drop when both question and answer are blank
|
||||||
|
.filter((it) => !(isBlank(it.q) && isBlank(it.a)))
|
||||||
return `
|
return `
|
||||||
<section class="faq" aria-labelledby="faq-heading">
|
<section class="faq" aria-labelledby="faq-heading">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@@ -27,7 +32,9 @@ function renderFaq(section: { props: { items: Array<{ q: string; a: string }> }
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderFeatures(section: { props: { items: string[] } }) {
|
function renderFeatures(section: { props: { items: string[] } }) {
|
||||||
const items: string[] = Array.isArray(section.props?.items) ? section.props.items : []
|
const items: string[] = (Array.isArray(section.props?.items) ? section.props.items : [])
|
||||||
|
.map((s) => String(s ?? '').trim())
|
||||||
|
.filter((s) => s.length > 0)
|
||||||
return `
|
return `
|
||||||
<section class="features" aria-labelledby="features-heading">
|
<section class="features" aria-labelledby="features-heading">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@@ -41,7 +48,9 @@ function renderFeatures(section: { props: { items: string[] } }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderBenefits(section: { props: { items: string[] } }) {
|
function renderBenefits(section: { props: { items: string[] } }) {
|
||||||
const items: string[] = Array.isArray(section.props?.items) ? section.props.items : []
|
const items: string[] = (Array.isArray(section.props?.items) ? section.props.items : [])
|
||||||
|
.map((s) => String(s ?? '').trim())
|
||||||
|
.filter((s) => s.length > 0)
|
||||||
return `
|
return `
|
||||||
<section class="benefits" aria-labelledby="benefits-heading">
|
<section class="benefits" aria-labelledby="benefits-heading">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@@ -55,9 +64,12 @@ function renderBenefits(section: { props: { items: string[] } }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderTestimonials(section: { props: { items: Array<{ author: string; quote: string }> } }) {
|
function renderTestimonials(section: { props: { items: Array<{ author: string; quote: string }> } }) {
|
||||||
const items: Array<{ author: string; quote: string }> = Array.isArray(section.props?.items)
|
const items: Array<{ author: string; quote: string }> = (Array.isArray(section.props?.items)
|
||||||
? section.props.items
|
? section.props.items
|
||||||
: []
|
: [])
|
||||||
|
.map((it) => ({ author: (it.author || '').trim(), quote: (it.quote || '').trim() }))
|
||||||
|
// drop when both are blank
|
||||||
|
.filter((it) => !(it.author.length === 0 && it.quote.length === 0))
|
||||||
return `
|
return `
|
||||||
<section class="testimonials" aria-labelledby="testimonials-heading">
|
<section class="testimonials" aria-labelledby="testimonials-heading">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@@ -66,8 +78,8 @@ function renderTestimonials(section: { props: { items: Array<{ author: string; q
|
|||||||
.map(
|
.map(
|
||||||
(it) => `
|
(it) => `
|
||||||
<figure class="testimonial">
|
<figure class="testimonial">
|
||||||
<blockquote>“${escapeHtml(it.quote || '')}”</blockquote>
|
<blockquote>“${escapeHtml(it.quote)}”</blockquote>
|
||||||
<figcaption>— ${escapeHtml(it.author || '')}</figcaption>
|
<figcaption>— ${escapeHtml(it.author)}</figcaption>
|
||||||
</figure>`
|
</figure>`
|
||||||
)
|
)
|
||||||
.join('')}
|
.join('')}
|
||||||
@@ -77,7 +89,9 @@ function renderTestimonials(section: { props: { items: Array<{ author: string; q
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderCta(section: { props: { text: string; href?: string } }) {
|
function renderCta(section: { props: { text: string; href?: string } }) {
|
||||||
const { text, href } = section.props
|
const href = section.props.href
|
||||||
|
const text = (section.props.text || '').trim()
|
||||||
|
if (text.length === 0) return ''
|
||||||
return `
|
return `
|
||||||
<section class="cta" aria-labelledby="cta-heading">
|
<section class="cta" aria-labelledby="cta-heading">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
@@ -267,10 +281,12 @@ function buildCss(page: Page) {
|
|||||||
.btn-cta{ background: var(--primary); color:#fff; padding:.75rem 1rem; border-radius:.5rem; display:inline-block; text-decoration:none }
|
.btn-cta{ background: var(--primary); color:#fff; padding:.75rem 1rem; border-radius:.5rem; display:inline-block; text-decoration:none }
|
||||||
.features{ background:#f8fafc }
|
.features{ background:#f8fafc }
|
||||||
.features-list{ list-style:none; padding:0; margin:0; display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .75rem }
|
.features-list{ list-style:none; padding:0; margin:0; display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .75rem }
|
||||||
.feature-item{ background:#fff; border:1px solid #e2e8f0; border-radius:.5rem; padding:.75rem }
|
.feature-item{ background:#fff; border:1px solid #e2e8f0; border-radius:.5rem; padding:.75rem; overflow-wrap:anywhere }
|
||||||
|
.benefits-list{ list-style:none; padding:0; margin:0 }
|
||||||
|
.benefit-item{ overflow-wrap:anywhere }
|
||||||
.testimonials{ background:#ffffff }
|
.testimonials{ background:#ffffff }
|
||||||
.testimonial{ border-left:4px solid var(--primary); padding:.5rem 1rem; margin: .75rem 0; background:#fefefe }
|
.testimonial{ border-left:4px solid var(--primary); padding:.5rem 1rem; margin: .75rem 0; background:#fefefe }
|
||||||
.testimonial blockquote{ margin:0; font-style:italic; color:#1f2937 }
|
.testimonial blockquote{ margin:0; font-style:italic; color:#1f2937; overflow-wrap:anywhere }
|
||||||
.testimonial figcaption{ color:#475569; margin-top:.25rem }
|
.testimonial figcaption{ color:#475569; margin-top:.25rem }
|
||||||
@media (max-width: 640px){ .container{ padding: 1rem } h1{ font-size: 1.5rem } }
|
@media (max-width: 640px){ .container{ padding: 1rem } h1{ font-size: 1.5rem } }
|
||||||
@media (prefers-color-scheme: dark){ body{ color:#e5e7eb; background:#0b1020 } .feature-item{ background:#0f172a; border-color:#1f2937 } a{ color:#93c5fd } a:hover{ text-decoration:underline } button{ background: var(--primary); color:#fff } .btn-cta{ background: var(--primary); color:#fff } .faq-a{ color:#cbd5e1 } }
|
@media (prefers-color-scheme: dark){ body{ color:#e5e7eb; background:#0b1020 } .feature-item{ background:#0f172a; border-color:#1f2937 } a{ color:#93c5fd } a:hover{ text-decoration:underline } button{ background: var(--primary); color:#fff } .btn-cta{ background: var(--primary); color:#fff } .faq-a{ color:#cbd5e1 } }
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { exportPage } from '@/lib/exporter/html'
|
||||||
|
import { pageSchema, type Page } from '@/lib/schema/page'
|
||||||
|
|
||||||
|
function makePage(overrides: Partial<Page> = {}): Page {
|
||||||
|
const base: Page = {
|
||||||
|
title: 'Edge',
|
||||||
|
locale: 'en',
|
||||||
|
theme: { primaryColor: '#2563eb', fontFamily: 'Inter' },
|
||||||
|
sections: [
|
||||||
|
{ type: 'features', props: { items: ['One', ' ', ' ', 'Two'] } },
|
||||||
|
{ type: 'benefits', props: { items: ['A', ' ', ' ', 'B'] } },
|
||||||
|
{ type: 'faq', props: { items: [
|
||||||
|
{ q: ' ', a: ' ' },
|
||||||
|
{ q: 'Valid Q', a: ' ' },
|
||||||
|
{ q: 'Another', a: 'Answer' },
|
||||||
|
] } },
|
||||||
|
{ type: 'testimonials', props: { items: [
|
||||||
|
{ author: ' ', quote: ' ' },
|
||||||
|
{ author: 'Jane', quote: ' Great product ' },
|
||||||
|
] } },
|
||||||
|
] as Page['sections'],
|
||||||
|
form: { actionUrl: '', fields: [], spamProtection: { honeypotFieldName: '_hp', minSubmitSeconds: 2 } },
|
||||||
|
analytics: { ga4MeasurementId: '', metaPixelId: '', customHeadHtml: '' },
|
||||||
|
seo: { title: 't', description: 'd' },
|
||||||
|
}
|
||||||
|
return pageSchema.parse({ ...base, ...overrides })
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Exporter - sections edge cases', () => {
|
||||||
|
it('filters out whitespace-only items and keeps valid ones', () => {
|
||||||
|
const out = exportPage(makePage())
|
||||||
|
const html = out.html
|
||||||
|
|
||||||
|
// features: only One, Two
|
||||||
|
expect(html).toContain('<li class="feature-item">One</li>')
|
||||||
|
expect(html).toContain('<li class="feature-item">Two</li>')
|
||||||
|
expect(html).not.toMatch(/<li class="feature-item"><\/li>/)
|
||||||
|
|
||||||
|
// benefits: only A, B
|
||||||
|
expect(html).toContain('<li class="benefit-item">A</li>')
|
||||||
|
expect(html).toContain('<li class="benefit-item">B</li>')
|
||||||
|
|
||||||
|
// faq: drop fully empty, keep items even if answer empty when question is valid
|
||||||
|
expect(html).toContain('<h3 class="faq-q">Valid Q</h3>')
|
||||||
|
// testimonials: drop fully empty, keep trimmed quote
|
||||||
|
expect(html).toContain('<blockquote>“Great product”</blockquote>')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('adds word-break tokens to avoid overflow for long words', () => {
|
||||||
|
const out = exportPage(makePage())
|
||||||
|
const css = out.css
|
||||||
|
expect(css).toMatch(/\.feature-item\{[\s\S]*(?:word-break:break-word|overflow-wrap:anywhere)/i)
|
||||||
|
expect(css).toMatch(/\.benefit-item\{[\s\S]*(?:word-break:break-word|overflow-wrap:anywhere)/i)
|
||||||
|
expect(css).toMatch(/\.testimonial blockquote\{[\s\S]*(?:word-break:break-word|overflow-wrap:anywhere)/i)
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user