AI-native design editor. Open-source Figma alternative.
import { afterAll, describe, expect, test } from 'bun:test'
import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import {
accordionComponent,
buttonSet,
collapsibleSet,
componentSet,
invalidCheckboxSet,
labelledButtonSet,
numberFieldComponent,
plainBadgeSet,
progressComponent,
sliderComponent,
textareaComponent,
textFieldSet,
radioGroupComponent,
settingsSectionSet,
switchSet,
tabsComponent,
tabsOfTabSets,
toggleGroupComponent
} from '#dom-css-tests/behaviours/fixtures'
import { shaderBackdropSet, shaderHeroSet } from '#dom-css-tests/export/components/shader-fixtures'
import { cssRules, fileText } from '#dom-css-tests/helpers'
import type { ComponentStyling } from '#dom-css/export'
import { exportStorybook } from '#dom-css/index'
import { createSSRApp, h, type Component } from 'vue'
import { compileScript, parse } from 'vue/compiler-sfc'
import { renderToString } from 'vue/server-renderer'
import { emptyBehaviour } from '@open-pencil/scene-graph/behaviours'
/** The packages generated code imports, linked so it resolves them as an app would. */
const APP_PACKAGES = ['vue', 'reka-ui', '@iconify/vue', 'storybook', 'shaders']
const output = await mkdtemp(join(tmpdir(), 'open-pencil-vue-'))
await mkdir(join(output, 'node_modules'))
await mkdir(join(output, 'node_modules', '@iconify'))
for (const name of APP_PACKAGES)
await symlink(
dirname(Bun.resolveSync(`${name}/package.json`, import.meta.dir)),
join(output, 'node_modules', name)
)
afterAll(() => rm(output, { recursive: true, force: true }))
/** Exports a fixture's set to Vue stories and loads the component and stories it writes. */
async function generate(fixture: ReturnType<typeof switchSet>, styling?: ComponentStyling) {
const files = await exportStorybook(fixture.graph, { framework: 'vue', styling })
const source = (suffix: string) =>
String(files.find((file) => file.path.endsWith(suffix))?.content)
const name = fixture.set.name
// Bun caches a folder's listing once it imports from it, so each export gets its own.
const folder = await mkdtemp(join(output, `${name}-`))
// `./Name.vue` resolves to `Name.vue.ts`, so components and stories import compiled ones.
for (const file of files.filter((item) => item.path.endsWith('.vue'))) {
const id = file.path.slice(0, -'.vue'.length)
const { descriptor, errors } = parse(String(file.content), { filename: file.path })
expect(errors).toEqual([])
const script = compileScript(descriptor, { id, inlineTemplate: true })
await writeFile(join(folder, `${file.path}.ts`), script.content)
}
await writeFile(join(folder, `${name}.stories.ts`), source('.stories.ts'))
const component: { default: Component } = await import(join(folder, `${name}.vue.ts`))
const stories: Record<string, { args?: Record<string, unknown>; play?: unknown }> = await import(
join(folder, `${name}.stories.ts`)
)
return { files, component: component.default, stories }
}
function render(component: Component, props: Record<string, unknown> = {}): Promise<string> {
return renderToString(createSSRApp({ render: () => h(component, props) }))
}
describe('generated Vue components', () => {
test('a switch is a Reka switch whose state follows its model', async () => {
const { files, component } = await generate(switchSet())
expect(files.map((file) => file.path).sort()).toEqual(['Switch.stories.ts', 'Switch.vue'])
const off = await render(component)
expect(off).toContain('role="switch"')
expect(off).toContain('data-state="unchecked"')
expect(off).toMatch(/class="switch"/)
const on = await render(component, { checked: true })
expect(on).toContain('data-state="checked"')
expect(on).toContain('aria-checked="true"')
expect(await render(component, { disabled: true })).toContain('data-disabled')
})
test('a collapsible shows its content when open', async () => {
const { component } = await generate(collapsibleSet())
const content = (html: string) => /<div class="disclosure__content"[^>]*>/.exec(html)?.[0] ?? ''
// Reka keeps closed content in the document, hidden; the state styles key on the root.
const closed = await render(component)
expect(content(closed)).toContain(' hidden ')
const open = await render(component, { open: true })
expect(open).toMatch(/^<div data-state="open" class="disclosure"/)
expect(content(open)).not.toContain(' hidden ')
})
test('a text property is a prop the bound layer draws, the design value by default', async () => {
const { component, stories } = await generate(labelledButtonSet())
expect(await render(component)).toMatch(/>Save</)
const sent = await render(component, { label: 'Send <now>' })
expect(sent).toMatch(/>Send <now></)
expect(sent).not.toContain('Save')
const meta = stories.default as {
args: Record<string, unknown>
argTypes: Record<string, unknown>
}
expect(meta.args).toMatchObject({ label: 'Save' })
expect(meta.argTypes).toMatchObject({ label: { control: 'text' } })
})
test('an instance of another generated component uses it, even in open-only content, and an icon uses Iconify', async () => {
const { files, component } = await generate(settingsSectionSet())
const source = String(files.find((file) => file.path === 'Section.vue')?.content)
expect(source).toContain(`import Switch from './Switch.vue'`)
expect(source).toContain('@iconify/vue')
const html = await render(component, { open: true })
// The switch is the generated one, drawn on as the design places it, and only once.
expect(html.match(/role="switch"/g)?.length).toBe(1)
expect(html).toContain('data-state="checked"')
})
test('tabs show the panel of the chosen tab, by the slug of its label', async () => {
const { component } = await generate(tabsComponent())
const first = await render(component)
expect(first).toContain('role="tablist"')
expect(first).toContain('Account settings')
expect(first).not.toContain('Password settings')
const second = await render(component, { value: 'password' })
expect(second).toContain('Password settings')
expect(second).not.toContain('Account settings')
})
test('tabs drawn as Tab instances take the selected look when active and keep a disabled tab disabled', async () => {
const { files, component } = await generate(tabsOfTabSets())
const html = await render(component)
const trigger = (name: string) =>
new RegExp(`<button class="settings__${name}"[^>]*>`).exec(html)?.[0] ?? ''
expect(trigger('account')).toContain('data-state="active"')
expect(trigger('billing')).toContain('data-disabled')
expect(trigger('password')).not.toContain('data-disabled')
const css = cssRules(String(files.find((file) => file.path === 'Settings.vue')?.content))
expect(css.get('.settings .settings__password[data-state="active"]')).toEqual(
css.get('.settings .settings__account[data-state="active"]')
)
})
test('a radio group writes an item component and chooses among its labelled items', async () => {
const { files, component } = await generate(radioGroupComponent())
expect(files.map((file) => file.path)).toContain('PlanItem.vue')
const checked = (html: string) =>
[...html.matchAll(/<button[^>]*role="radio"[^>]*>/g)].map((tag) =>
tag[0].includes('aria-checked="true"')
)
// The design draws the second item chosen.
const drawn = await render(component)
expect(checked(drawn)).toEqual([false, true, false])
expect(drawn).toContain('Team')
expect(checked(await render(component, { value: 'team' }))).toEqual([false, false, true])
})
test('an item takes the words its label property assigns, before its layer is synced', async () => {
const fixture = radioGroupComponent()
const { graph } = fixture
const [items] = graph.getChildren(fixture.set.id)
const last = graph.getChildren(items?.id ?? '').at(-1)
if (!last) throw new Error('Expected the last item')
graph.updateNode(last.id, {
componentPropertyAssignments: { ...last.componentPropertyAssignments, label: 'Scale' }
})
const { component } = await generate(fixture)
expect(await render(component, { value: 'scale' })).toContain('Scale')
})
test('an item without its label assigned shows the words its layer reads', async () => {
const fixture = radioGroupComponent()
const { graph } = fixture
const [items] = graph.getChildren(fixture.set.id)
const last = graph.getChildren(items?.id ?? '').at(-1)
const label = graph.getChildren(last?.id ?? '').find((layer) => layer.type === 'TEXT')
if (!last || !label) throw new Error('Expected the item to show its label')
graph.updateNode(last.id, { componentPropertyAssignments: {} })
graph.updateNode(label.id, { text: 'Enterprise' })
const { component } = await generate(fixture)
const html = await render(component, { value: 'enterprise' })
expect(html).toContain('Enterprise')
expect(html).not.toContain('Team')
expect(html).toMatch(
/<button[^>]*aria-checked="true"[^>]*value="enterprise"|value="enterprise"[^>]*aria-checked="true"/
)
})
test("a group's item component is named apart from another component's file", async () => {
const fixture = radioGroupComponent()
const page = fixture.graph.getPages()[0].id
fixture.graph.createNode('COMPONENT', page, { name: 'PlanItem' })
const files = await exportStorybook(fixture.graph, { framework: 'vue' })
const paths = files.map((file) => file.path)
expect(paths).toContain('PlanItem.stories.ts')
expect(paths).toContain('PlanItem2.vue')
expect(String(files.find((file) => file.path === 'Plan.vue')?.content)).toContain(
`import PlanItem2 from './PlanItem2.vue'`
)
})
test("a toggle group's items are the group's item, not the standalone toggle", async () => {
const { files, component } = await generate(toggleGroupComponent())
const group = String(files.find((file) => file.path === 'Plan.vue')?.content)
expect(group).toContain('PlanItem')
expect(group).not.toMatch(/import \{? ?Toggle\b/)
const html = await render(component)
expect([...html.matchAll(/data-state="(on|off)"/g)].map((match) => match[1])).toEqual([
'on',
'off'
])
})
test('an accordion opens the item it is given and starts with none', async () => {
const { component } = await generate(accordionComponent())
const states = (html: string) =>
[...html.matchAll(/<button[^>]*aria-expanded="(true|false)"/g)].map((match) => match[1])
expect(states(await render(component))).toEqual(['false', 'false'])
expect(states(await render(component, { value: 'usage' }))).toEqual(['false', 'true'])
// The heading around each trigger keeps its level but not its own margins.
expect(await render(component)).toMatch(/<h3[^>]*style="margin:0;font:inherit;?"/)
})
test('a button sets its other variant properties as data attributes', async () => {
const { component } = await generate(buttonSet())
const large = await render(component, { size: 'Large' })
expect(large).toMatch(/^<button[^>]*type="button"/)
expect(large).toContain('data-size="Large"')
})
})
/** The rules of a single-file component's scoped stylesheet, by selector. */
const styleOf = (files: { path: string; content: string | Uint8Array }[], path: string) =>
cssRules(
/<style scoped>([\s\S]*)<\/style>/.exec(
String(files.find((file) => file.path === path)?.content)
)?.[1] ?? ''
)
describe('generated Vue components styled with Tailwind', () => {
test('carry their states as variants in place of a style block', async () => {
const { files, component } = await generate(switchSet(), 'tailwind')
const sfc = fileText(files, 'Switch.vue')
expect(sfc).not.toContain('<style')
expect(sfc).toMatch(
/<SwitchRoot class="group\/switch [^"]*data-\[state=checked\]:bg-\[#4F45E6\]/
)
expect(sfc).toContain('group-data-[state=checked]/switch:left-5')
const on = await render(component, { checked: true })
expect(on).toContain('data-state="checked"')
expect(on).toMatch(/class="group\/switch [^"]*"/)
})
test('play a shader behind the layer, placed by utilities', async () => {
const { files } = await generate(shaderHeroSet(), 'tailwind')
expect(fileText(files, 'Hero.vue')).toMatch(
/<Shader[^>]* class="pointer-events-none absolute inset-0 -z-10 rounded-\[inherit\]"/
)
})
})
describe('generated Vue plain components', () => {
test('take variant, text, and boolean properties as props, and slots as slots', async () => {
const { component } = await generate(plainBadgeSet())
const rest = await render(component)
expect(rest).toContain('data-tone="Neutral"')
expect(rest).toContain('New')
// The icon the design hides at rest shows only while its boolean is on.
expect(rest).not.toContain('badge__dot')
expect(await render(component, { icon: true })).toContain('badge__dot')
expect(await render(component, { tone: 'Danger', label: 'Hot' })).toMatch(
/data-tone="Danger"[\s\S]*Hot/
)
// A slot shows the design's content unless the caller passes its own.
expect(rest).toContain('Note')
const filled = await renderToString(
createSSRApp({ render: () => h(component, {}, { extra: () => h('b', 'Custom') }) })
)
expect(filled).toContain('<b>Custom</b>')
expect(filled).not.toContain('Note')
})
})
describe('generated Vue shader fills', () => {
test('play the shader behind the layer, with telemetry off', async () => {
const { files, component } = await generate(shaderHeroSet())
const source = fileText(files, 'Hero.vue')
expect(source).toMatch(
/import \{\s*Shader as ShaderCanvas,\s*Aurora as ShaderAurora,\s*FilmGrain as ShaderFilmGrain\s*\} from ["']shaders\/vue["']/
)
expect(source).toContain('<ShaderCanvas class="hero-shader" :disable-telemetry="true">')
expect(source).toMatch(/<ShaderAurora[^>]*color-a="#ff3300"[^>]*:curtain-count="2"/)
expect(source).toContain(':center="({ x: 0.5, y: 0 })"')
const styles = [...cssRules(source.slice(source.indexOf('<style'))).values()]
expect(styles).toContainEqual(expect.objectContaining({ isolation: 'isolate' }))
// The shader comes first, so the title draws over it.
const html = await render(component)
expect(html.indexOf('hero-shader')).toBeLessThan(html.indexOf('Northern lights'))
})
})
describe('generated Vue shader backdrops', () => {
test('a layer with nothing in it plays its shader, not an image of it', async () => {
const { files } = await generate(shaderBackdropSet())
const source = fileText(files, 'Card.vue')
expect(source).not.toContain('<img')
expect(source).not.toContain('data:image')
expect(source).toContain('<ShaderCanvas')
})
})
describe('generated Vue form controls', () => {
test('a slider binds one number within its range, the thumb and range placed by Reka', async () => {
const { files, component } = await generate(sliderComponent())
// Reka shows its thumb once mounted; its range already reaches the value on the server.
const range = (html: string) => /style="([^"]*)" class="volume__range"/.exec(html)?.[1]
expect(range(await render(component))).toBe('left:0%;right:40%;')
expect(range(await render(component, { value: 25 }))).toBe('left:0%;right:75%;')
expect(await render(component)).toContain('aria-valuemax="100"')
const css = styleOf(files, 'Volume.vue')
// Reka sets where they are from the value, so the place the design draws them at goes.
expect(css.get('.volume .volume__thumb')?.left).toBeUndefined()
expect(css.get('.volume .volume__range')?.width).toBeUndefined()
// Its root is a span, which lays out as the block the design draws.
expect(css.get('.volume')?.display).toBe('block')
expect(css.get('.volume .volume__thumb')?.['box-sizing']).toBe('border-box')
})
test("a progress bar's indicator reaches the value's share of its range", async () => {
const { component } = await generate(progressComponent())
expect(await render(component)).toMatch(
/class="upload__indicator"[^>]*style="width:25%;"|style="width:25%;"[^>]*class="upload__indicator"/
)
expect(await render(component, { value: 150 })).toContain('width:75%;')
// A value past the end fills the indicator, and no further.
expect(await render(component, { value: 500 })).toContain('width:100%')
})
test('a number field shows its value in its input, and its root hugs it', async () => {
const { files, component } = await generate(numberFieldComponent())
expect(await render(component)).toMatch(/<input[^>]*value="2"/)
expect(await render(component, { value: 7 })).toMatch(/<input[^>]*value="7"/)
expect(styleOf(files, 'Quantity.vue').get('.quantity')?.width).toBe('fit-content')
})
test('a text field starts empty with its placeholder, and looks filled while it has words', async () => {
const { files, component } = await generate(textFieldSet())
const html = await render(component)
expect(html).toMatch(/<input[^>]*placeholder="Email"/)
expect(html).not.toContain('[email protected]')
expect(await render(component, { value: '[email protected]' })).toContain(
'value="[email protected]"'
)
const css = styleOf(files, 'Email.vue')
const input = [...css].find(([selector]) => /^\.email \.email__value[^:]*$/.test(selector))?.[1]
const placeholder = [...css].find(([selector]) => selector.endsWith('::placeholder'))?.[1]
// Typed words take the filled look; the empty look is the placeholder's.
expect(input?.color).toBe('#121726')
expect(placeholder?.color).toBe('#9CA3B0')
})
// As the React field spreads its props on the input and gives its root the caller's className.
test("a field's input takes the caller's attributes, and its root the caller's class", async () => {
for (const fixture of [textFieldSet(), textFieldSet({ invalid: true }), textareaComponent()]) {
const { component } = await generate(fixture)
const html = await render(component, {
name: 'email',
required: true,
placeholder: '[email protected]',
'aria-invalid': 'true',
class: 'wide'
})
const root = /^<div[^>]*>/.exec(html)?.[0] ?? ''
const input = /<(?:input|textarea)[^>]*>/.exec(html)?.[0] ?? ''
expect(root).toContain('wide')
expect(root).not.toContain('name=')
expect(root).not.toContain('aria-invalid')
expect(input).toContain('name="email"')
expect(input).toContain('required')
expect(input).toContain('aria-invalid="true"')
expect(input).not.toContain('wide')
if (fixture.set.name === 'Email') expect(input).toContain('placeholder="[email protected]"')
}
})
test('a field with no text bound yet draws no input, and its root takes the attributes', async () => {
const fixture = componentSet(
'Message',
{ Size: ['Default'] },
emptyBehaviour('textarea'),
(graph, variant) => {
graph.createNode('TEXT', variant, { name: 'Hint', text: 'Tell us more' })
}
)
const { files, component } = await generate(fixture)
expect(fileText(files, 'Message.vue')).not.toContain('inheritAttrs')
const html = await render(component, { 'data-testid': 'message', class: 'wide' })
expect(html).toMatch(/^<div[^>]*data-testid="message"/)
expect(html).toMatch(/^<div[^>]*class="[^"]*wide/)
})
test('a field whose input sits in a slot keeps its attributes on the root', async () => {
const fixture = componentSet(
'Message',
{ Size: ['Default'] },
{ ...emptyBehaviour('textarea'), texts: { value: { propertyId: 'message' } } },
(graph, variant) => {
const body = graph.createNode('FRAME', variant, {
name: 'Body',
width: 200,
height: 60,
componentPropertyReferences: [{ propertyId: 'body', field: 'SLOT_CONTENT' }]
})
graph.createNode('TEXT', body.id, {
name: 'Value',
text: 'Tell us more',
componentPropertyReferences: [{ propertyId: 'message', field: 'TEXT' }]
})
}
)
fixture.graph.updateNode(fixture.set.id, {
componentPropertyDefinitions: [
...fixture.set.componentPropertyDefinitions,
{ id: 'message', name: 'Message', type: 'TEXT', defaultValue: 'Tell us more' },
{ id: 'body', name: 'Body', type: 'SLOT', defaultValue: '' }
]
})
const { files, component } = await generate(fixture)
const source = fileText(files, 'Message.vue')
// The slot's own content replaces the input, so the attributes stay where Vue puts them.
expect(source).toContain('<slot name="body">')
expect(source).not.toContain('inheritAttrs')
expect(await render(component, { 'data-testid': 'message' })).toMatch(
/^<div[^>]*data-testid="message"/
)
})
test('a textarea starts with its words', async () => {
const { component } = await generate(textareaComponent())
expect(await render(component)).toMatch(/<textarea[^>]*>Tell us more<\/textarea>/)
})
test('an invalid checkbox is marked for assistive technology and its styles', async () => {
const { component, stories } = await generate(invalidCheckboxSet())
const valid = await render(component)
expect(valid).not.toContain('aria-invalid')
expect(valid).not.toContain('data-invalid')
const invalid = await render(component, { invalid: true })
expect(invalid).toMatch(/<button[^>]*aria-invalid="true"[^>]*role="checkbox"/)
expect(invalid).toContain('data-invalid="true"')
expect(Object.keys(stories)).toContain('Invalid')
})
test("an invalid field marks its input, and its root for the field's styles", async () => {
const { component } = await generate(textFieldSet({ invalid: true }))
const html = await render(component, { invalid: true })
expect(html).toMatch(/^<div[^>]*data-invalid="true"/)
expect(html).toMatch(/<input[^>]*aria-invalid="true"/)
expect(html.match(/aria-invalid/g)).toHaveLength(1)
// An `aria-invalid` the caller sets reaches the input too, not just the root it falls to.
const passed = await render(component, { 'aria-invalid': 'true' })
expect(passed).toMatch(/<input[^>]*aria-invalid="true"/)
})
})
describe('generated Vue stories', () => {
test('show each state and operate the control', async () => {
const { component, stories } = await generate(switchSet())
const meta = stories.default as { component: Component; args: Record<string, unknown> }
expect(meta.component).toBe(component)
expect(meta.args).toEqual({ checked: false, disabled: false })
// A module lists its exports alphabetically, not in the order they're written.
expect(Object.keys(stories).filter((key) => key !== 'default')).toEqual([
'Checked',
'Default',
'Disabled'
])
expect(stories.Checked?.args).toEqual({ checked: true })
expect(typeof stories.Default?.play).toBe('function')
})
test('tag every state after Default, so Storybook can show one story per component', async () => {
const { stories } = await generate(switchSet())
const tags = (name: string) => (stories[name] as { tags?: string[] } | undefined)?.tags
expect((stories.default as { tags: string[] }).tags).toContain('openpencil')
expect(tags('Default')).toBeUndefined()
expect(tags('Checked')).toEqual(['variant'])
expect(tags('Disabled')).toEqual(['variant'])
})
})