bUnit-dev / bUnit

bUnit is a testing library for Blazor components that make tests look, feel, and runs like regular unit tests. bUnit makes it easy to render and control a component under test’s life-cycle, pass parameter and inject services into it, trigger event handlers, and verify the rendered markup from the component using a built-in semantic HTML comparer.
https://bunit.dev
MIT License
1.11k stars 104 forks source link

Smarter rendering in v2 #1466

Open egil opened 3 months ago

egil commented 3 months ago

With v2, IRenderedComponent<TComponent> is inherting from ComponentState.

A component can:

During/after each render cycle (UpdateDisplayAsync):

DOM tree


NOTES and example below:

IRenderedComponent<BunitRootComponent> cut = Render(....);
IRenderedComponent<FooButton> button = cut.FindComponent<FooButton>();
button.Markup // slice of the cut.Markup
button.Nodes // 

// Go from component (tree) to element
var buttonElementFromRoot = cut.Find("button");
var buttonElementFromFooButton = button.Find("button");

// Reuse of AngleSharp DOM nodes across rendered components
ReferenceEquals(buttonElementFromRoot, buttonElementFromFooButton);

// Go from element to component tree
IRenderedComponent<FooButton> comp = buttonElementFromFooButton.GetComponent<FooButton>();

// Should be able to see node changes without re-finding for them.
elm.Click();
elm.InnerText.Should().Be("Counter: 1");

component: 4 ... 20 node: