|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | +import { h } from "@vue/runtime-core"; |
| 4 | + |
| 5 | +import { firstChild, mountComponent, toTreeSnapshot } from "../testing/mount.js"; |
| 6 | +import { Box } from "./Box.js"; |
| 7 | + |
| 8 | +// Box declares Boolean props, so Vue defaults them to false and the render |
| 9 | +// function forwards them onto the host node; they are inert downstream |
| 10 | +// (treeToRenderNodes drops falsy appearance) but part of the output tree. |
| 11 | +const boxDefaults = { borderDimColor: false, "aria-hidden": false }; |
| 12 | + |
| 13 | +void test("renders a box node and forwards children", () => { |
| 14 | + const mounted = mountComponent(Box, {}, () => h("text", { text: "inside" })); |
| 15 | + |
| 16 | + assert.deepEqual(toTreeSnapshot(firstChild(mounted)), { |
| 17 | + type: "box", |
| 18 | + props: { style: {}, ...boxDefaults }, |
| 19 | + children: [{ type: "text", props: { text: "inside" } }], |
| 20 | + }); |
| 21 | + mounted.unmount(); |
| 22 | +}); |
| 23 | + |
| 24 | +void test("maps layout props onto the style object", () => { |
| 25 | + const mounted = mountComponent(Box, { |
| 26 | + flexDirection: "column", |
| 27 | + justifyContent: "center", |
| 28 | + alignItems: "flex-end", |
| 29 | + flexGrow: 1, |
| 30 | + flexShrink: 0, |
| 31 | + width: 40, |
| 32 | + height: "50%", |
| 33 | + minWidth: 5, |
| 34 | + gap: 2, |
| 35 | + overflow: "hidden", |
| 36 | + }); |
| 37 | + |
| 38 | + assert.deepEqual(firstChild(mounted).props.style, { |
| 39 | + flexDirection: "column", |
| 40 | + justifyContent: "center", |
| 41 | + alignItems: "flex-end", |
| 42 | + flexGrow: 1, |
| 43 | + flexShrink: 0, |
| 44 | + width: "40", |
| 45 | + height: "50%", |
| 46 | + minWidth: "5", |
| 47 | + gap: 2, |
| 48 | + overflow: "hidden", |
| 49 | + }); |
| 50 | + mounted.unmount(); |
| 51 | +}); |
| 52 | + |
| 53 | +void test("expands padding and margin axis shorthands per side", () => { |
| 54 | + const mounted = mountComponent(Box, { |
| 55 | + padding: 1, |
| 56 | + paddingX: 2, |
| 57 | + paddingTop: 3, |
| 58 | + marginY: 4, |
| 59 | + marginLeft: 5, |
| 60 | + }); |
| 61 | + |
| 62 | + assert.deepEqual(firstChild(mounted).props.style, { |
| 63 | + padding: 1, |
| 64 | + paddingTop: 3, |
| 65 | + paddingRight: 2, |
| 66 | + // paddingBottom stays unset: per-side keys materialize only when that |
| 67 | + // side or its axis shorthand is given; the base padding covers the rest. |
| 68 | + paddingLeft: 2, |
| 69 | + marginTop: 4, |
| 70 | + marginBottom: 4, |
| 71 | + marginLeft: 5, |
| 72 | + }); |
| 73 | + mounted.unmount(); |
| 74 | +}); |
| 75 | + |
| 76 | +void test("normalizes border style names and prefers the Ink alias", () => { |
| 77 | + const withBorder = (props: Record<string, unknown>) => { |
| 78 | + const mounted = mountComponent(Box, props); |
| 79 | + const border = firstChild(mounted).props.border; |
| 80 | + mounted.unmount(); |
| 81 | + return border; |
| 82 | + }; |
| 83 | + |
| 84 | + assert.equal(withBorder({ border: "single" }), "single"); |
| 85 | + assert.equal(withBorder({ border: "round" }), "rounded"); |
| 86 | + assert.equal(withBorder({ border: "bold" }), "heavy"); |
| 87 | + assert.equal(withBorder({ borderStyle: "double", border: "single" }), "double"); |
| 88 | + assert.equal(withBorder({}), undefined); |
| 89 | +}); |
| 90 | + |
| 91 | +void test("resolves color aliases with Fresco names winning", () => { |
| 92 | + const mounted = mountComponent(Box, { |
| 93 | + fg: "cyan", |
| 94 | + color: "red", |
| 95 | + backgroundColor: "blue", |
| 96 | + borderColor: "green", |
| 97 | + }); |
| 98 | + |
| 99 | + const props = firstChild(mounted).props; |
| 100 | + assert.equal(props.fg, "cyan"); |
| 101 | + assert.equal(props.bg, "blue"); |
| 102 | + assert.equal(props.borderColor, "green"); |
| 103 | + mounted.unmount(); |
| 104 | +}); |
| 105 | + |
| 106 | +void test("hides aria-hidden subtrees only in screen reader mode", () => { |
| 107 | + const visible = mountComponent(Box, { "aria-hidden": true }, () => h("text", { text: "x" })); |
| 108 | + assert.equal(firstChild(visible).type, "box"); |
| 109 | + assert.equal(firstChild(visible).props["aria-hidden"], true); |
| 110 | + visible.unmount(); |
| 111 | + |
| 112 | + const hidden = mountComponent(Box, { "aria-hidden": true }, () => h("text", { text: "x" }), { |
| 113 | + screenReader: true, |
| 114 | + }); |
| 115 | + assert.deepEqual(toTreeSnapshot(firstChild(hidden)), { type: "text" }); |
| 116 | + hidden.unmount(); |
| 117 | +}); |
| 118 | + |
| 119 | +void test("replaces children with the aria-label in screen reader mode", () => { |
| 120 | + const mounted = mountComponent( |
| 121 | + Box, |
| 122 | + { "aria-label": "status panel" }, |
| 123 | + () => h("text", { text: "42%" }), |
| 124 | + { screenReader: true }, |
| 125 | + ); |
| 126 | + |
| 127 | + assert.deepEqual(toTreeSnapshot(firstChild(mounted)), { |
| 128 | + type: "box", |
| 129 | + props: { style: {}, "aria-label": "status panel", ...boxDefaults }, |
| 130 | + children: [{ type: "text", props: { text: "status panel" } }], |
| 131 | + }); |
| 132 | + mounted.unmount(); |
| 133 | +}); |
0 commit comments