Home

Awesome

<!-- GENERATED FILE - DO NOT EDIT This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets). Source File: /readme.source.md To change this file edit the source file and then run MarkdownSnippets. -->

<img src="/src/icon.png" height="30px"> Verify.Blazor

Discussions Build status NuGet Status

Support for rendering a Blazor Component to a verified file via Blazor rendering.

Verify.Blazor uses the Blazor APIs to take a snapshot (metadata and html) of the current state of a Blazor component. It has fewer dependencies and is a simpler API than Verify.Bunit approach, however it does not provide many of the other features, for example trigger event handlers.

See Milestones for release notes.

Component

The below samples use the following Component:

<!-- snippet: BlazorApp/TestComponent.razor -->

<a id='snippet-BlazorApp/TestComponent.razor'></a>

<div>
    <h1>@Title</h1>
    <p>@Person.Name</p>
    <button>MyButton</button>
</div>

@code {

    [Parameter]
    public string Title { get; set; } = "My Test Component";

    [Parameter]
    public Person Person { get; set; }

    public bool Intitialized;

    protected override Task OnInitializedAsync()
    {
        Intitialized = true;
        return Task.CompletedTask;
    }

}

<sup><a href='/src/BlazorApp/TestComponent.razor#L1-L23' title='Snippet source file'>snippet source</a> | <a href='#snippet-BlazorApp/TestComponent.razor' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

NuGet package

https://nuget.org/packages/Verify.Blazor/

Usage

Render using ParameterView

This test:

<!-- snippet: BlazorComponentTestWithParameters -->

<a id='snippet-BlazorComponentTestWithParameters'></a>

[Fact]
public Task PassingParameters()
{
    var parameters = ParameterView.FromDictionary(
        new Dictionary<string, object?>
        {
            {
                "Title", "The Title"
            },
            {
                "Person", new Person
                {
                    Name = "Sam"
                }
            }
        });

    var target = Render.Component<TestComponent>(parameters: parameters);

    return Verify(target);
}

<sup><a href='/src/Tests/Samples.cs#L16-L40' title='Snippet source file'>snippet source</a> | <a href='#snippet-BlazorComponentTestWithParameters' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Render using template instance

This test:

<!-- snippet: BlazorComponentTestWithTemplateInstance -->

<a id='snippet-BlazorComponentTestWithTemplateInstance'></a>

[Fact]
public Task PassingTemplateInstance()
{
    var template = new TestComponent
    {
        Title = "The Title",
        Person = new()
        {
            Name = "Sam"
        }
    };

    var target = Render.Component(template: template);

    return Verify(target);
}

<sup><a href='/src/Tests/Samples.cs#L42-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-BlazorComponentTestWithTemplateInstance' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Result

Both will produce:

The component rendered as html ...verified.html:

<!-- snippet: Tests/Samples.PassingParameters.verified.html -->

<a id='snippet-Tests/Samples.PassingParameters.verified.html'></a>

<div>
  <h1>The Title</h1>
  <p>Sam</p>
  <button>MyButton</button>
</div>

<sup><a href='/src/Tests/Samples.PassingParameters.verified.html#L1-L5' title='Snippet source file'>snippet source</a> | <a href='#snippet-Tests/Samples.PassingParameters.verified.html' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

And the current model rendered as txt ...verified.txt:

<!-- snippet: Tests/Samples.PassingParameters.verified.txt -->

<a id='snippet-Tests/Samples.PassingParameters.verified.txt'></a>

{
  Instance: {
    Intitialized: true,
    Title: The Title,
    Person: {
      Name: Sam
    }
  }
}

<sup><a href='/src/Tests/Samples.PassingParameters.verified.txt#L1-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-Tests/Samples.PassingParameters.verified.txt' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Scrubbing

Integrity check

In Blazor an integrity check is applied to the dotnet.*.js file.

<script src="_framework/dotnet.5.0.2.js" defer="" integrity="sha256-AQfZ6sKmq4EzOxN3pymKJ1nlGQaneN66/2mcbArnIJ8=" crossorigin="anonymous"></script>

This line will change when the dotnet SDK is updated.

Noise in rendered template

Blazor uses <!--!--> to delineate components in the resulting html. Some empty lines can be rendered when components are stitched together.

Resulting scrubbing

<!-- snippet: scrubbers -->

<a id='snippet-scrubbers'></a>

// remove some noise from the html snapshot
VerifierSettings.ScrubEmptyLines();
BlazorScrubber.ScrubCommentLines();
VerifierSettings.ScrubLinesWithReplace(
    line =>
    {
        var scrubbed = line.Replace("<!--!-->", "");
        if (string.IsNullOrWhiteSpace(scrubbed))
        {
            return null;
        }

        return scrubbed;
    });
HtmlPrettyPrint.All();
VerifierSettings.ScrubLinesContaining("<script src=\"_framework/dotnet.");

<sup><a href='/src/Tests/ModuleInitializer.cs#L10-L29' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrubbers' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Icon

Helmet designed by Leonidas Ikonomou from The Noun Project.