Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/VerifyTests/Verify.Blazor

Support for rendering a Blazor Component to a verified file via bunit or raw Blazor rendering.
https://github.com/VerifyTests/Verify.Blazor

blazor bunit snapshot-testing testing

Last synced: 3 months ago
JSON representation

Support for rendering a Blazor Component to a verified file via bunit or raw Blazor rendering.

Awesome Lists containing this project

README

        

# Verify.Blazor

[![Discussions](https://img.shields.io/badge/Verify-Discussions-yellow?svg=true&label=)](https://github.com/orgs/VerifyTests/discussions)
[![Build status](https://ci.appveyor.com/api/projects/status/spyere4ubpl1tca8?svg=true)](https://ci.appveyor.com/project/SimonCropp/Verify-Blazor)
[![NuGet Status](https://img.shields.io/nuget/v/Verify.Blazor.svg?label=Verify.Blazor)](https://www.nuget.org/packages/Verify.Blazor/)

Support for rendering a [Blazor Component](https://docs.microsoft.com/en-us/aspnet/core/blazor/#components) 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](https://github.com/VerifyTests/Verify.Bunit), however it does not provide many of the other features, for example [trigger event handlers](https://bunit.egilhansen.com/docs/interaction/trigger-event-handlers.html).

**See [Milestones](../../milestones?state=closed) for release notes.**

## Component

The below samples use the following Component:


```razor


@Title


@Person.Name


MyButton

@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;
}

}
```
snippet source | anchor

## NuGet package

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

## Usage

### Render using ParameterView

This test:


```cs
[Fact]
public Task PassingParameters()
{
var parameters = ParameterView.FromDictionary(
new Dictionary
{
{
"Title", "The Title"
},
{
"Person", new Person
{
Name = "Sam"
}
}
});

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

return Verify(target);
}
```
snippet source | anchor

### Render using template instance

This test:


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

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

return Verify(target);
}
```
snippet source | anchor

### Result

Both will produce:

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


```html


The Title


Sam


MyButton

```
snippet source | anchor

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


```txt
{
Instance: {
Intitialized: true,
Title: The Title,
Person: {
Name: Sam
}
}
}
```
snippet source | anchor

## Scrubbing

### Integrity check

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

```

```

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


```cs
// 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("<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](https://thenounproject.com/term/helmet/9554/) designed
by [Leonidas Ikonomou](https://thenounproject.com/alterego) from [The Noun Project](https://thenounproject.com).