Component testing
Donkeh.Testing mounts a component headlessly, runs the real
CSS and layout pipeline, and dispatches synthetic input. Tests can assert
on rendered text, attributes, computed styles and box geometry without
opening a window.
Adding the library
Reference the testing project from a test project. Existing Donkeh applications already depend on the UILab, LayoutLab, DataEngine and Windowing assemblies the harness uses.
<ItemGroup>
<ProjectReference Include="..\..\Donkeh\Testing\Source\Donkeh.Testing.csproj" />
</ItemGroup>
Mounting a component
ComponentTest.Render is the entry point. It mounts the
virtual tree, compiles the user-agent sheet plus every
[Stylesheet(...)] declaration found in loaded assemblies,
and performs an initial layout.
using Donkeh.Testing;
using static Donkeh.UILab._;
var page = ComponentTest.Render(C<Counter>());
Assert.Equal("Counter: 0", page.Find(".counter-btn").Text);
page.Click(".counter-btn");
Assert.Equal("Counter: 1", page.Find(".counter-btn").Text);
Pass a tree directly when the test does not need a component:
var page = ComponentTest.Render(
H("button", new Props { ClassName = "save" }, Text("Save")));
page.Click(".save");
Options and stylesheets
The optional configure callback edits a
ComponentTestOptions instance. It controls the viewport,
root font size, text measurer, declared-style loading and extra CSS.
var page = ComponentTest.Render(
C<Card>(),
options =>
{
options.ViewportWidth = 640;
options.ViewportHeight = 480;
options.Stylesheets.Add(
".card { border: 1px solid #d0d7de; }");
});
AddStylesheet appends CSS after mount and
Resize changes the viewport. Both recompile or relayout as
needed:
page.AddStylesheet(".card.is-wide { width: 480px; }");
page.Resize(800, 600);
Updating props
Update patches the mounted root with a new virtual
description. Component instances and hook state survive when the root
node kind does not change, which is what makes prop-change tests behave
like a parent re-render.
var page = ComponentTest.Render<Badge, BadgeProps>(
new BadgeProps { Label = "New" });
page.Update(C<Badge, BadgeProps>(
new BadgeProps { Label = "Archived" }));
Queries
Find accepts a CSS selector and throws
ComponentQueryException when nothing matches.
TryFind returns null instead. Queries return
TestElement wrappers with text, attributes, classes,
computed style and bounds.
var button = page.Find(".toolbar button.primary");
var optional = page.TryFind("#missing");
string text = button.Text;
string? title = button.GetAttribute("title");
bool visible = button.IsVisible;
float width = button.Bounds.Width;
var style = button.Style;
var allRows = page.FindAll(".row");
var byText = page.FindByText("No files yet.");
Dispatching events
Event helpers dispatch to the target or the nearest ancestor handler,
matching the window's bubbling contract. Handlers are invoked with typed
event payloads when they accept one, or with the legacy
Action, Action<string> and
Action<float, float> shapes otherwise. Exceptions are
not swallowed.
| Helper | Event |
|---|---|
Click | click |
MouseDown, MouseUp | mousedown, mouseup |
PointerDown, PointerMove, PointerUp | Pointer events |
Hover, Unhover | mouseover, mouseout plus the CSS hover chain |
Scroll | scroll and an updated scroll offset |
Input, Type | input with the control value |
Change | change |
Paste, Submit, KeyDown | Their matching events |
Focus, Blur | focus / blur plus interaction state |
Dispatch | Any custom Event instance |
page.Input(".search", "quarterly report");
page.Type(".search", " final");
page.Scroll(".results", 0, 120);
page.KeyDown(".search", "Enter");
Every helper calls Settle after dispatching. That flushes
queued component state and reruns layout, so the next assertion sees the
committed result. When a test mutates a node directly, call
page.Settle() before querying.
page.Find(".details").Element.SetAttribute("open", "");
page.Settle();
Assert.True(page.Find(".details").HasAttribute("open"));
ComponentTestOptions.TextMeasurer when a test needs a
specific font metric; do not depend on installed fonts.