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

https://github.com/verifytests/verify.bunit


https://github.com/verifytests/verify.bunit

Last synced: 11 months ago
JSON representation

Awesome Lists containing this project

README

          

# Verify.Bunit

[![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-Bunit)
[![NuGet Status](https://img.shields.io/nuget/v/Verify.Bunit.svg?label=Verify.Bunit)](https://www.nuget.org/packages/Verify.Bunit/)

Support for rendering a [Blazor Component](https://docs.microsoft.com/en-us/aspnet/core/blazor/#components) to a verified file via [bunit](https://bunit.egilhansen.com). Verify.Bunit uses the bUnit APIs to take a snapshot (metadata and html) of the current state of a Blazor component. Since it leverages the bUnit API, snapshots can be on a component that has been manipulated using the full bUnit feature set, for example [trigger event handlers](https://bunit.egilhansen.com/docs/interaction/trigger-event-handlers.html).

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

## Sponsors

### Entity Framework Extensions

[Entity Framework Extensions](https://entityframework-extensions.net/) is a major sponsor and is proud to contribute to the development this project.

[![Entity Framework Extensions](docs/zzz.png)](https://entityframework-extensions.net)

## 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

* https://nuget.org/packages/Verify.Bunit

## Usage

Enable at startup:


```cs
[ModuleInitializer]
public static void Initialize() =>
VerifyBunit.Initialize();
```
snippet source | anchor

This test:


```cs
[Fact]
public Task Component()
{
using var context = new TestContext();
var component = context.RenderComponent(
builder =>
{
builder.Add(
_ => _.Title,
"New Title");
builder.Add(
_ => _.Person,
new()
{
Name = "Sam"
});
});
return Verify(component);
}

[Fact]
public Task MarkupFormattable_NodeList()
{
using var context = new TestContext();
var component = context.RenderComponent(
builder =>
{
builder.Add(
_ => _.Title,
"New Title");
builder.Add(
_ => _.Person,
new()
{
Name = "Sam"
});
});
return Verify(component.Nodes);
}

[Fact]
public Task MarkupFormattable_single_Element()
{
using var context = new TestContext();
var component = context.RenderComponent(
builder =>
{
builder.Add(
_ => _.Title,
"New Title");
builder.Add(
_ => _.Person,
new()
{
Name = "Sam"
});
});
return Verify(component.Nodes.First()
.FirstChild);
}
```
snippet source | anchor

Will produce:

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


```html


New Title


Sam


MyButton
snippet source | anchor

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


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

## Exclude Component

Rendering of the Component state (Samples.Component.verified.txt from above) can be excluded by
using `excludeComponent`.


```cs
[ModuleInitializer]
public static void Initialize() =>
VerifyBunit.Initialize(excludeComponent: true);
```
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#L16-L35' 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).