Files
site-builder/craft/src/components/legacy-light-surface.test.tsx
T
shadowdaoandClaude Opus 5.5 37ed85bf42 fix(navbar): logo on a stored light bar follows the link text in the editor
Legacy Navbar nodes store backgroundColor '#ffffff' + textColor '#3f3f46'; the
editor took the logo colour from the Site Design text (light on a dark design)
while the export already uses logoColor || textCol. Same dark-palette class as
finding 8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 13:22:22 -07:00

233 lines
11 KiB
TypeScript

import { describe, test, expect, afterEach } from 'vitest';
import { renderEditorHarness, EditorHarness } from '../test-utils/editorHarness';
import { buildNodeTree } from '../utils/craft-tree';
import { templateComponentToTreeNode } from '../templates/apply-template';
import { exportToHtml } from '../utils/html-export';
import { DEFAULT_SITE_DESIGN } from '../state/SiteDesignContext';
import { tokenRef } from '../utils/tokens';
import { ContactForm } from './forms/ContactForm';
import { SubscribeForm } from './forms/SubscribeForm';
import { Testimonials } from './sections/Testimonials';
/**
* Finding 8: nodes saved before the component-tokens work (9ab0102) carry light
* surface hex props (ContactForm inputBg '#ffffff', SubscribeForm
* style.backgroundColor '#f8fafc', Testimonials cardBg '#f8fafc'). The text on
* those surfaces is now token-driven, so on a dark Site Design palette (light
* text token, e.g. Agency textColor #fafafa) it rendered white-on-white. Text on
* a STORED LIGHT surface must keep the old dark hex; absent/token/dark surfaces
* keep the token text. Checked in both the editor render and toHtml export.
*/
// Agency-like dark palette: light text token on a dark page.
const DARK_DESIGN = { ...DEFAULT_SITE_DESIGN, backgroundColor: '#171717', textColor: '#fafafa', mutedTextColor: '#a1a1aa' };
const TEXT = tokenRef('text');
const MUTED = tokenRef('muted');
const BG = tokenRef('bg');
/** Normalise a hex to what jsdom's CSSStyleDeclaration reports. */
function rgb(hex: string): string {
const h = hex.replace('#', '');
return `rgb(${parseInt(h.slice(0, 2), 16)}, ${parseInt(h.slice(2, 4), 16)}, ${parseInt(h.slice(4, 6), 16)})`;
}
function parse(html: string): HTMLElement {
const d = document.createElement('div');
d.innerHTML = html;
return d;
}
let h: EditorHarness | null = null;
afterEach(() => {
h?.unmount();
h = null;
});
/** Render a node through the REAL editor, as a deserialized saved node: props
* listed in `drop` are deleted from the stored node (legacy nodes missing them). */
function renderInEditor(type: string, props: Record<string, unknown>, drop: string[] = []): HTMLElement {
const seed = renderEditorHarness();
seed.act(() => {
seed.actions.addNodeTree(buildNodeTree(seed.query, templateComponentToTreeNode({ type, props } as any)), 'ROOT');
});
const state = JSON.parse(seed.getSerialized());
seed.unmount();
for (const node of Object.values<any>(state)) {
if (node.type?.resolvedName === type) for (const k of drop) delete node.props[k];
}
h = renderEditorHarness({ initialState: JSON.stringify(state) });
return h.container;
}
/** Body markup of a full export under the dark palette (asserts the palette is in effect). */
function exportDark(type: string, props: Record<string, unknown>): HTMLElement {
const state = {
ROOT: { type: { resolvedName: 'Container' }, isCanvas: true, props: { style: {} }, displayName: 'Container', custom: {}, hidden: false, nodes: ['n1'], linkedNodes: {} },
n1: { type: { resolvedName: type }, isCanvas: false, props, displayName: type, custom: {}, parent: 'ROOT', hidden: false, nodes: [], linkedNodes: {} },
};
const { html } = exportToHtml(JSON.stringify(state), { design: DARK_DESIGN });
expect(html).toContain('--wsb-text:#fafafa');
return parse(html.slice(html.indexOf('<body')));
}
/* ---------------- ContactForm ---------------- */
const cfToHtml = (props: any) => parse((ContactForm as any).toHtml(props, '', 'n1').html);
function cfColors(root: HTMLElement) {
const inputs = Array.from(root.querySelectorAll('input:not([tabindex="-1"]):not([type="hidden"]), textarea, select')) as HTMLElement[];
const visible = inputs.filter((el) => !/left:-|position:absolute/.test(el.getAttribute('style') || ''));
return {
input: visible.map((el) => el.style.color),
inputBg: visible.map((el) => el.style.backgroundColor),
label: (Array.from(root.querySelectorAll('label')) as HTMLElement[]).filter((l) => l.textContent).map((l) => l.style.color),
};
}
const LEGACY_FIELDS = [
{ type: 'text', label: 'Name', name: 'name', placeholder: 'Your name', required: true },
{ type: 'textarea', label: 'Message', name: 'message', placeholder: 'Hi', required: false },
{ type: 'select', label: 'Topic', name: 'topic', placeholder: 'Pick', required: false, options: ['a'] },
];
describe('ContactForm: legacy light inputBg keeps dark input text', () => {
const legacy = { fields: LEGACY_FIELDS, inputBg: '#ffffff', labelColor: '#374151', style: { backgroundColor: '#ffffff' } };
test.each([['toHtml', (p: any) => cfToHtml(p)], ['export (dark palette)', (p: any) => exportDark('ContactForm', p)], ['editor render', (p: any) => renderInEditor('ContactForm', p)]])(
'%s: stored #ffffff inputBg -> #1f2937 input text',
(_n, run) => {
const c = cfColors(run(legacy));
expect(c.input.length).toBe(3);
for (const col of c.input) expect(col).toBe(rgb('#1f2937'));
for (const col of c.label) expect(col).toBe(rgb('#374151'));
},
);
test('shorthand / rgb() light surfaces count as light', () => {
for (const bg of ['#fff', 'rgb(248, 250, 252)']) {
for (const col of cfColors(cfToHtml({ ...legacy, inputBg: bg })).input) expect(col).toBe(rgb('#1f2937'));
}
});
test.each([['toHtml', (p: any) => cfToHtml(p)], ['editor render', (p: any) => renderInEditor('ContactForm', p, ['inputBg', 'labelColor'])]])(
'%s: absent inputBg/labelColor -> token surface + token text',
(_n, run) => {
const c = cfColors(run({ fields: LEGACY_FIELDS }));
for (const col of c.input) expect(col).toBe(TEXT);
for (const col of c.inputBg) expect(col).toBe(BG);
for (const col of c.label) expect(col).toBe(TEXT);
},
);
test.each([['toHtml', (p: any) => cfToHtml(p)], ['editor render', (p: any) => renderInEditor('ContactForm', p)]])(
'%s: dark stored inputBg keeps token text',
(_n, run) => {
const c = cfColors(run({ fields: LEGACY_FIELDS, inputBg: '#171717', labelColor: TEXT, style: { backgroundColor: '#171717' } }));
for (const col of c.input) expect(col).toBe(TEXT);
for (const col of c.label) expect(col).toBe(TEXT);
},
);
});
/* ---------------- SubscribeForm ---------------- */
const sfToHtml = (props: any) => parse((SubscribeForm as any).toHtml(props, '', 'n1').html);
const h3Color = (root: HTMLElement) => (root.querySelector('h3') as HTMLElement).style.color;
describe('SubscribeForm: legacy light wrapper keeps dark heading', () => {
const legacy = { heading: 'Join', layout: 'inline', style: { backgroundColor: '#f8fafc' } };
test.each([['toHtml', (p: any) => sfToHtml(p)], ['export (dark palette)', (p: any) => exportDark('SubscribeForm', p)], ['editor render', (p: any) => renderInEditor('SubscribeForm', p)]])(
'%s: stored #f8fafc background -> #1f2937 heading',
(_n, run) => {
expect(h3Color(run(legacy))).toBe(rgb('#1f2937'));
},
);
test.each([['toHtml', (p: any) => sfToHtml(p)], ['editor render', (p: any) => renderInEditor('SubscribeForm', p, ['style'])]])(
'%s: absent style -> token heading, token input surface/text',
(_n, run) => {
const root = run({ heading: 'Join' });
expect(h3Color(root)).toBe(TEXT);
const input = root.querySelector('input[type="email"]') as HTMLElement;
expect(input.style.backgroundColor).toBe(BG);
expect(input.style.color).toBe(TEXT);
},
);
test.each([['toHtml', (p: any) => sfToHtml(p)], ['editor render', (p: any) => renderInEditor('SubscribeForm', p)]])(
'%s: dark stored background keeps token heading',
(_n, run) => {
expect(h3Color(run({ heading: 'Join', style: { backgroundColor: '#171717' } }))).toBe(TEXT);
},
);
});
/* ---------------- Testimonials ---------------- */
const tToHtml = (props: any) => parse((Testimonials as any).toHtml(props, '').html);
const ITEMS = [{ quote: 'Great', name: 'Ann', title: 'CEO', rating: 5 }];
function cardColors(root: HTMLElement) {
const p = root.querySelector('p') as HTMLElement;
const name = p.nextElementSibling as HTMLElement;
const title = name.nextElementSibling as HTMLElement;
const card = p.parentElement as HTMLElement;
return { quote: p.style.color, name: name.style.color, title: title.style.color, card: card.style.backgroundColor };
}
describe('Testimonials: legacy light cardBg keeps dark card text', () => {
const legacy = { testimonials: ITEMS, cardBg: '#f8fafc', style: { backgroundColor: '#ffffff' } };
test.each([['toHtml', (p: any) => tToHtml(p)], ['export (dark palette)', (p: any) => exportDark('Testimonials', p)], ['editor render', (p: any) => renderInEditor('Testimonials', p)]])(
'%s: stored #f8fafc cardBg -> #374151 quote / #18181b name / #64748b title',
(_n, run) => {
const c = cardColors(run(legacy));
expect(c.quote).toBe(rgb('#374151'));
expect(c.name).toBe(rgb('#18181b'));
expect(c.title).toBe(rgb('#64748b'));
},
);
test.each([['toHtml', (p: any) => tToHtml(p)], ['editor render', (p: any) => renderInEditor('Testimonials', p, ['cardBg'])]])(
'%s: absent cardBg -> token card surface + token text',
(_n, run) => {
const c = cardColors(run({ testimonials: ITEMS }));
expect(c.card).toBe(BG);
expect(c.quote).toBe(TEXT);
expect(c.name).toBe(TEXT);
expect(c.title).toBe(MUTED);
},
);
test.each([['toHtml', (p: any) => tToHtml(p)], ['editor render', (p: any) => renderInEditor('Testimonials', p)]])(
'%s: dark stored cardBg keeps token text',
(_n, run) => {
const c = cardColors(run({ testimonials: ITEMS, cardBg: '#262626' }));
expect(c.quote).toBe(TEXT);
expect(c.name).toBe(TEXT);
expect(c.title).toBe(MUTED);
},
);
});
/* ---------------- Navbar (legacy nodes store textColor '#3f3f46') ---------------- */
describe('Navbar: legacy stored light bar stays readable on a dark palette', () => {
const legacy = { backgroundColor: '#ffffff', textColor: '#3f3f46', logoText: 'Brand', links: [{ label: 'Home', href: '/' }] };
test('export: links and logo use the stored dark text', () => {
const root = exportDark('Navbar', legacy);
const anchors = Array.from(root.querySelectorAll('nav a')) as HTMLElement[];
expect(anchors.length).toBeGreaterThan(0);
expect(anchors.map((a) => a.style.color).filter(Boolean).every((c) => c === rgb('#3f3f46'))).toBe(true);
});
test('editor: the logo follows the stored dark text, not the (light) Site Design text colour', () => {
const c = renderInEditor('Navbar', legacy, ['logoColor']);
const nav = c.querySelector('nav')!;
const logo = Array.from(nav.querySelectorAll('*')).find((el) => el.textContent === 'Brand' && (el as HTMLElement).style.color) as HTMLElement;
expect(logo).toBeTruthy();
expect(logo.style.color).toBe(rgb('#3f3f46'));
});
});