https://github.com/verifytests/verify.bunit
https://github.com/verifytests/verify.bunit
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/verifytests/verify.bunit
- Owner: VerifyTests
- License: mit
- Created: 2024-07-24T09:30:05.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-23T17:19:19.000Z (over 1 year ago)
- Last Synced: 2024-12-29T01:53:45.838Z (over 1 year ago)
- Language: C#
- Homepage:
- Size: 409 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license.txt
- Code of conduct: code_of_conduct.md
Awesome Lists containing this project
README
#
Verify.Bunit
[](https://github.com/orgs/VerifyTests/discussions)
[](https://ci.appveyor.com/project/SimonCropp/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.
[](https://entityframework-extensions.net)
## Component
The below samples use the following Component:
@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`:
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).