Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# form-builder-sample

Two small apps built with [form-builder](https://whatsaaaa.github.io/form-builder/) `1.0.0-rc.0`, using only what a new user has: the documentation site and the packages on npm. The repository is the release-candidate trial for form-builder 1.0: it shows that real forms can be built from the public material, and records every problem met on the way. The apps take their look from the [Whatsaaaa design system](https://github.com/whatsaaaa/design-system), installed from npm as `@whatsaaaa/design-system` like any other package: the Vue app now, the React app once its own restyle lands.
Two small apps built with [form-builder](https://whatsaaaa.github.io/form-builder/) `1.0.0-rc.0`, using only what a new user has: the documentation site and the packages on npm. The repository is the release-candidate trial for form-builder 1.0: it shows that real forms can be built from the public material, and records every problem met on the way. The apps take their look from the [Whatsaaaa design system](https://github.com/whatsaaaa/design-system), installed from npm as `@whatsaaaa/design-system` like any other package.

**The record of the trial is [FRICTION-LOG.md](FRICTION-LOG.md).** Start there if you work on form-builder.

Expand All @@ -11,9 +11,9 @@ Two small apps built with [form-builder](https://whatsaaaa.github.io/form-builde
| [`vue-event-registration`](vue-event-registration) | A multi-step event registration in Vue | Steps, rows, a section and a slot, conditional fields and a conditional step, a repeating guest list, form rules, a definition loaded from a JSON file |
| [`react-checkout`](react-checkout) | A checkout in React, with a small server | A definition served by the server and validated again there, messages in English and German, a repeating list of items, conditional fields, server errors shown on the fields |

Each app is self-contained: its own `package.json`, lockfile and tooling. Both need pnpm 11. The Vue app needs Node 22.18 or a later 22, or Node 24.12 or later, as the design system does. The React app needs Node 20.19 or later.
Each app is self-contained: its own `package.json`, lockfile and tooling. Both need pnpm 11, and Node 22.18 or a later 22, or Node 24.12 or later, as the design system does.

Everything visual lives in each app's `src/ui` folder (field, layout and page components) and its one stylesheet, `src/styles.css`, so a restyle swaps those and leaves the forms alone. The Vue app is restyled on the design system: its page is the system's terminal frame, with a tab bar, a status line that switches the theme and the shortcut keys, and a dark and a light theme. The React app still looks plain until its own restyle lands.
Everything visual lives in each app's `src/ui` folder (field, layout and page components) and its one stylesheet, `src/styles.css`, so a restyle swaps those and leaves the forms alone. Both apps are restyled on the design system: their page is the system's terminal frame, with a tab bar, a status line that switches the theme and the shortcut keys, and a dark and a light theme.

## Run the Vue app

Expand Down
144 changes: 134 additions & 10 deletions react-checkout/e2e/checkout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { expect, test, type Page } from '@playwright/test'

/** Fails when the page scrolls sideways: something is wider than the screen. */
async function expectNoSidewaysScroll(page: Page) {
const overflow = await page.evaluate(
() =>
document.documentElement.scrollWidth -
document.documentElement.clientWidth,
)
expect(overflow).toBeLessThanOrEqual(0)
}
import { expectNoSidewaysScroll } from './checks'

/** Fills in an order that is valid: two mugs, shipped to Belgrade, paid by card. */
async function fillValidOrder(page: Page) {
Expand Down Expand Up @@ -87,6 +78,69 @@ test('places an order that the server validates and prices again', async ({
await expectNoSidewaysScroll(page)
})

test('keeps the order button faint while it is sent, under the pointer too', async ({
page,
}) => {
await fillValidOrder(page)

// The order waits on its way to the server, so the button stays as it is while it is sent.
let send = () => {}
const held = new Promise<void>((resolve) => {
send = resolve
})
await page.route('**/api/orders', async (route) => {
await held
await route.continue()
})
await page.getByRole('button', { name: 'Place order' }).click()

const sending = page.getByRole('button', { name: 'Placing your order...' })
await expect(sending).toBeDisabled()
const look = () =>
sending.evaluate((element) => {
const style = getComputedStyle(element)
return [style.color, style.borderColor, style.backgroundColor]
})
// The pointer is still where it clicked: on the button.
const hovered = await look()
await page.mouse.move(0, 0)
expect(hovered).toEqual(await look())

send()
await expect(
page.getByRole('heading', { name: 'Thank you for your order' }),
).toBeVisible()
})

test('shows the order as plain text when its highlighter does not load', async ({
page,
}) => {
const errors: Error[] = []
page.on('pageerror', (error) => errors.push(error))
await page.route('**/assets/highlight-*.js', (route) => route.abort())
await page.reload()
await fillValidOrder(page)
await page.getByRole('button', { name: 'Place order' }).click()

await expect(
page.getByRole('heading', { name: 'Thank you for your order' }),
).toBeVisible()
expect(await confirmedOrder(page)).toMatchObject({
email: 'jelena@example.com',
})
// Every piece of the order is in the block's own text color.
const order = page.getByTestId('order')
const colors = await order.evaluate((element) => [
...new Set(
[element, ...element.querySelectorAll('span')].map(
(span) => getComputedStyle(span).color,
),
),
])
expect(colors).toHaveLength(1)
expect(errors).toEqual([])
})

test('stops an invalid order in the browser, before it reaches the server', async ({
page,
}) => {
Expand Down Expand Up @@ -282,3 +336,73 @@ test('the server rejects invalid data sent straight to its API', async ({
]),
)
})

// 320 px is the width a page has to work at without scrolling sideways (WCAG's reflow), 390 px a
// common phone.
for (const [width, colorScheme] of [
[320, 'dark'],
[320, 'light'],
[390, 'dark'],
[390, 'light'],
] as const) {
test.describe(`on a ${String(width)} px screen, in the ${colorScheme} theme`, () => {
test.use({ viewport: { width, height: 844 }, colorScheme })

test('fits the screen in English and in German, with its errors', async ({
page,
}) => {
// The page follows the system's theme, and names it in the status line.
const name = colorScheme === 'dark' ? 'rose-pine' : 'rose-pine-dawn'
await expect(
page.getByRole('button', {
name: `Switch theme, now ${name}`,
exact: true,
}),
).toBeVisible()
await expectNoSidewaysScroll(page)

await page.getByRole('button', { name: 'Place order' }).click()
await expect(page.getByText('Enter your email')).toBeVisible()
await expectNoSidewaysScroll(page)
await fillValidOrder(page)
await page
.getByLabel('Note for the courier')
.fill('Zvonite kod Đorđevića, ulaz iz dvorišta. Čuvar je u prizemlju.')
await expectNoSidewaysScroll(page)

// German labels, buttons and messages are longer.
await page.getByRole('button', { name: 'Deutsch' }).click()
await expect(page.getByRole('heading', { name: 'Kasse' })).toBeVisible()
await expectNoSidewaysScroll(page)

// The server's messages on the fields, as in the test of a changed order above.
await page.route('**/api/orders', async (route) => {
const body = route.request().postDataJSON() as {
input: {
lines: { quantity: unknown }[]
shipping: { zip: string }
}
}
const line = body.input.lines[0]
if (line) line.quantity = 99
body.input.shipping.zip = 'ABC'
await route.continue({ postData: JSON.stringify(body) })
})
await page.getByRole('button', { name: 'Bestellung aufgeben' }).click()
await expect(
page.getByText('Bestellen Sie höchstens zehn Stück eines Produkts'),
).toBeVisible()
await expect(
page.getByText('Verwenden Sie 4 oder 5 Ziffern'),
).toBeVisible()
await expectNoSidewaysScroll(page)

await page.unroute('**/api/orders')
await page.getByRole('button', { name: 'Bestellung aufgeben' }).click()
await expect(
page.getByRole('heading', { name: 'Vielen Dank für Ihre Bestellung' }),
).toBeVisible()
await expectNoSidewaysScroll(page)
})
})
}
112 changes: 112 additions & 0 deletions react-checkout/e2e/checks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { expect, type Page } from '@playwright/test'

/**
* Fails when the page scrolls sideways, or when a part of the window cuts something off at its
* side. The window, its bars and its code block hide what is wider than they are, so that would
* not make the page scroll.
*/
export async function expectNoSidewaysScroll(page: Page, message?: string) {
const overflowing = await page.evaluate(() =>
[
'html',
'.ws-term',
'.page__main',
'.ws-tab-bar',
'.ws-status-line',
'.ws-code',
'.ws-code > pre',
].filter((selector) =>
[...document.querySelectorAll(selector)].some(
(element) => element.scrollWidth > element.clientWidth,
),
),
)
expect(overflowing, message).toEqual([])
}

/**
* Fails when the status line cuts what it holds. The mode, the keys switch and the name of the
* theme are whole and inside it, and its path shows whole or not at all: when it shows, "~…" fits
* in its text box, and the text has as much room before the keys switch as after the mode. It is
* gone only when "~…" and its padding have no room there. Says whether the path showed.
*/
export async function expectStatusLineWhole(page: Page, message?: string) {
const line = await page.locator('.ws-status-line').evaluate((line) => {
const find = (selector: string) => {
const element = line.querySelector(selector)
if (!element) throw new Error(`The status line has no ${selector}`)
return element
}
const edge = line.getBoundingClientRect()
const items = {
mode: '.ws-status-line__mode',
keys: '.ws-status-line__switch',
theme: '.status-line__theme',
}
const cut = Object.entries(items).flatMap(([name, selector]) => {
const shown = [...line.querySelectorAll(selector)].filter((element) =>
element.checkVisibility(),
)
if (shown.length !== 1) return [`${name}: ${String(shown.length)} shown`]
const element = shown[0] as Element
const box = element.getBoundingClientRect()
// Half a pixel for the rounding of fractional layout on a phone.
const inside =
box.left >= edge.left - 0.5 && box.right <= edge.right + 0.5
const whole = element.scrollWidth <= element.clientWidth
return inside && whole ? [] : [`${name}: cut`]
})
const path = find('.ws-status-line__file')
const text = find('.ws-status-line__file > span')
const pathStyle = getComputedStyle(path)
const textStyle = getComputedStyle(text)
const probe = document.createElement('span')
probe.style.position = 'absolute'
probe.style.font = textStyle.font
probe.textContent = '~…'
document.body.append(probe)
const shortest = probe.getBoundingClientRect().width
probe.remove()
const mode = find('.ws-status-line__mode').getBoundingClientRect().right
const keys = find('.ws-status-line__switch').getBoundingClientRect().left
const box = text.getBoundingClientRect()
const left = box.left + parseFloat(textStyle.paddingLeft)
const right = box.right - parseFloat(textStyle.paddingRight)
return {
cut,
overflow: line.scrollWidth - line.clientWidth,
shown: path.checkVisibility(),
room: keys - mode,
needed:
shortest +
parseFloat(pathStyle.paddingLeft) +
parseFloat(pathStyle.paddingRight) +
parseFloat(textStyle.paddingLeft) +
parseFloat(textStyle.paddingRight),
text: right - left,
shortest,
before: left - mode,
after: keys - right,
}
})
const at = message ? ` ${message}` : ''
expect(line.cut, `a working item is cut${at}`).toEqual([])
expect(
line.overflow,
`the status line cuts its end${at}`,
).toBeLessThanOrEqual(0)
if (line.shown) {
expect(line.text, `the path shows a cut glyph${at}`).toBeGreaterThanOrEqual(
line.shortest,
)
expect(
line.after,
`the path runs into the keys switch${at}`,
).toBeGreaterThanOrEqual(line.before)
} else {
expect(line.room, `the path is gone with room for "~…"${at}`).toBeLessThan(
line.needed,
)
}
return line.shown
}
Loading
Loading