Skip to content

Can't Find Elements in ChildContent #512

@thalaeg

Description

@thalaeg

Finding elements in ChildComponents

When trying to find elements in AddChildContent the system will error (first test example). You have to instead look directly in the cut (second test example)

Say you had a parent element (HeadlessDisclosure) that is a div and cascades itself to a child (HeadlessDisclosureButton) which is a button. I want to grab the button element with the Find(cssSelector).

The expected razor

<HeadlessDisclosure>
    <HeadlessDisclosureButton />
</HeadlessDisclosure>

would result in

<div>
    <button />
<div>

Here is the first test (that fails). The Node of the disclosure has 1 element (the div), but not direct children. Instead, if you look in the Node's content, you will see the button.

var disclosure = RenderComponent<HeadlessDisclosure>(parameters => parameters
    .AddChildContent<HeadlessDisclosureButton>()
);

var button = disclosure.Find("button"); // Bunit.ElementNotFoundException : No elements were found that matches the selector 'button'
// do stuff with button

Test that works:

var disclosure = RenderComponent<HeadlessDisclosure>();
var cut = RenderComponent<HeadlessDisclosureButton>(parameters => parameters
    .AddCascadingValue(disclosure.Instance)
);

var button = cut.Find("button");
// do stuff with button

Expected behavior:

I want the top test to be able to find elements

Version info:

  • bUnit version: <PackageReference Include="bunit.web" Version="1.2.49" />
  • .NET Runtime and Blazor version: <TargetFramework>net6.0</TargetFramework>
  • OS type and version: Windows

Additional Info

Here is a screen grab of the markup of disclosure. So I know it is getting the correct markup.
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions