Skip to content

Commit 401b412

Browse files
linkdotnetbUnitBot
authored andcommitted
feat: remove Snapshop API
1 parent 01e9e71 commit 401b412

18 files changed

Lines changed: 9 additions & 669 deletions

MIGRATION.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Migration Guide `v1` to `v2`
2+
This document describes the changes that need to be made to migrate from bUnit 1.x to 2.x.
3+
4+
## Removal of `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods
5+
The `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods have been removed from `IRenderedComponent<TComponent>`. There is no one-to-one replacement for these methods, but the general idea is to select the HTML in question via `Find` and assert against that.
6+
7+
Alternatively, the `IRenderFragment` still offers the `OnMarkupUpdated` event, which can be used to assert against the markup after a render.

docs/samples/components/bunit.docs.samples.csproj

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
5-
<LangVersion>latest</LangVersion>
6-
<RazorLangVersion>3.0</RazorLangVersion>
4+
<TargetFramework>net8.0;net.9.0</TargetFramework>
75
<RootNamespace>Bunit.Docs.Samples</RootNamespace>
86
<ImplicitUsings>enable</ImplicitUsings>
97
<NoWarn>CA1014,NU5104</NoWarn>
108
<CheckEolTargetFramework>false</CheckEolTargetFramework>
119
</PropertyGroup>
1210

13-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
14-
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1" />
15-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1" />
16-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1" />
17-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1" />
18-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1" />
19-
</ItemGroup>
20-
21-
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
22-
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
23-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
24-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0" />
25-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0" />
26-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.0" />
27-
</ItemGroup>
28-
29-
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
30-
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
31-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
32-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.25" />
33-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.25" />
34-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="6.0.25" />
35-
</ItemGroup>
36-
37-
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
38-
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
39-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
40-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="7.0.14" />
41-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.14" />
42-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="7.0.1" />
43-
</ItemGroup>
44-
4511
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
4612
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
4713
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />

docs/samples/tests/xunit/VerifyMarkupExamples.cs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -59,72 +59,4 @@ public void FindAndFindAll()
5959
Assert.Equal(2, tableCells.Count);
6060
Assert.All(tableCells, td => td.HasAttribute("style"));
6161
}
62-
63-
[Fact]
64-
public void GetChangesSinceFirstRenderTest()
65-
{
66-
var cut = RenderComponent<Counter>();
67-
68-
// Act - increment the counter
69-
cut.Find("button").Click();
70-
71-
// Assert - find differences between first render and click
72-
var diffs = cut.GetChangesSinceFirstRender();
73-
74-
// Only expect there to be one change
75-
var diff = diffs.ShouldHaveSingleChange();
76-
// and that change should be a text
77-
// change to "Current count: 1"
78-
diff.ShouldBeTextChange("Current count: 1");
79-
}
80-
81-
[Fact]
82-
public void GetChangesSinceX()
83-
{
84-
// Arrange
85-
var cut = RenderComponent<CheckList>();
86-
var inputField = cut.Find("input");
87-
88-
// Add first item
89-
inputField.Change("First item");
90-
inputField.KeyUp(key: "Enter");
91-
92-
// Assert that first item was added correctly
93-
var diffs = cut.GetChangesSinceFirstRender();
94-
diffs.ShouldHaveSingleChange()
95-
.ShouldBeAddition("<li>First item</li>");
96-
97-
// Save snapshot of current DOM nodes
98-
cut.SaveSnapshot();
99-
100-
// Add a second item
101-
inputField.Change("Second item");
102-
inputField.KeyUp(key: "Enter");
103-
104-
// Assert that both first and second item was added
105-
// since the first render
106-
diffs = cut.GetChangesSinceFirstRender();
107-
diffs.ShouldHaveChanges(
108-
diff => diff.ShouldBeAddition("<li>First item</li>"),
109-
diff => diff.ShouldBeAddition("<li>Second item</li>")
110-
);
111-
112-
// Assert that only the second item was added
113-
// since the call to SaveSnapshot()
114-
diffs = cut.GetChangesSinceSnapshot();
115-
diffs.ShouldHaveSingleChange()
116-
.ShouldBeAddition("<li>Second item</li>");
117-
118-
// Save snapshot again of current DOM nodes
119-
cut.SaveSnapshot();
120-
121-
// Click last item to remove it from list
122-
cut.Find("li:last-child").Click();
123-
124-
// Assert that the second item was removed
125-
// since the call to SaveSnapshot()
126-
diffs = cut.GetChangesSinceSnapshot();
127-
diffs.ShouldHaveSingleChange()
128-
.ShouldBeRemoval("<li>Second item</li>");
129-
}
13062
}

docs/site/docs/verification/verify-markup.md

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -120,55 +120,6 @@ The semantic HTML comparer can be customized to make a test case even more stabl
120120

121121
Learn more about the customization options on the <xref:semantic-html-comparison> page.
122122

123-
## Finding expected differences
124-
125-
It can sometimes be easier to verify that an expected change, and only that change, has occurred in the rendered markup than it can be to specify how all the rendered markup should look after re-rendering.
126-
127-
bUnit comes with a number of ways for finding lists of `IDiff`; the representation of a difference between two HTML fragments. All of these are direct methods or extension methods on the <xref:Bunit.IRenderedFragment> type or on the `INode` or `INodeList` types:
128-
129-
- <xref:Bunit.IRenderedFragment.GetChangesSinceFirstRender> method on <xref:Bunit.IRenderedFragment>. This method returns a list of differences since the initial first render of a component.
130-
- <xref:Bunit.IRenderedFragment.GetChangesSinceSnapshot> and <xref:Bunit.IRenderedFragment.SaveSnapshot> methods on <xref:Bunit.IRenderedFragment>. These two methods combined make it possible to get a list of differences between the last time the <xref:Bunit.IRenderedFragment.SaveSnapshot> method was called and the time a call to the <xref:Bunit.IRenderedFragment.GetChangesSinceSnapshot> method is placed.
131-
- `CompareTo()` methods from <xref:Bunit.CompareToExtensions> for the <xref:Bunit.IRenderedFragment>, `INode`, and `INodeList` types. These methods return a list of differences between the two input HTML fragments.
132-
133-
In addition to this, there are a number of experimental assertion helpers for `IDiff` and `IEnumerable<IDiff>`, making it easier and more concise to declare your assertions.
134-
135-
Let's look at a few examples of using the assertion helpers. In the first one, we will use the `<Counter>` component listed below:
136-
137-
[!code-razor[Counter.razor](../../../samples/components/Counter.razor)]
138-
139-
Here is an example of using the <xref:Bunit.IRenderedFragment.GetChangesSinceFirstRender> method:
140-
141-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=67&end=79&highlight=7,10,13)]
142-
143-
This is what happens in the test:
144-
145-
- On line 8, <xref:Bunit.IRenderedFragment.GetChangesSinceFirstRender> is used to get a list of differences.
146-
- On line 11, the `ShouldHaveSingleChange()` method is used to verify that there is only one change found.
147-
- On line 14, the `ShouldBeTextChange()` method is used to verify that the single `IDiff` is a text change.
148-
149-
Testing a more **complex life cycle of a component** can be done more easily using the <xref:Bunit.IRenderedFragment.GetChangesSinceSnapshot> and <xref:Bunit.IRenderedFragment.SaveSnapshot> methods along with a host of other assert helpers.
150-
151-
This example tests the `<CheckList>` component listed below. The component allows you to add new items to the checklist by typing into the input field and hitting the `enter` key. Items can be removed from the list again by clicking on them.
152-
153-
[!code-razor[CheckList.razor](../../../samples/components/CheckList.razor)]
154-
155-
To test the end-to-end life cycle of adding and removing items from the `<CheckList>` component, do the following:
156-
157-
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=85&end=126)]
158-
159-
This is what happens in the test:
160-
161-
1. First the component is rendered and the input field is found.
162-
2. The first item is added through the input field.
163-
3. The <xref:Bunit.IRenderedFragment.GetChangesSinceFirstRender>, `ShouldHaveSingleChange()` and `ShouldBeAddition()` methods are used to verify that the item was correctly added.
164-
4. The <xref:Bunit.IRenderedFragment.SaveSnapshot> is used to save a snapshot of current DOM nodes internally in the `cut`. This reduces the number of diffs found in the following steps, simplifying verification.
165-
5. A second item is added to the check list.
166-
6. Two verifications are performed at this point, one using the <xref:Bunit.IRenderedFragment.GetChangesSinceFirstRender> method which finds two changes, and one using the <xref:Bunit.IRenderedFragment.GetChangesSinceSnapshot> method, which finds a single change. The first is only done for illustrative purposes.
167-
7. A new snapshot is saved, replacing the previous one with another call to the <xref:Bunit.IRenderedFragment.SaveSnapshot> method.
168-
8. Finally the last item in the list is found and clicked, and the <xref:Bunit.IRenderedFragment.GetChangesSinceSnapshot> method is used to find the changes, a single diff, which is verified as a removal of the second item.
169-
170-
As mentioned earlier, the `IDiff` assertion helpers are still experimental. Any feedback and suggestions for improvements should be directed to the [related issue](https://github.com/egil/bUnit/issues/84) on GitHub.
171-
172123
## Verification of raw markup
173124

174125
To access the rendered markup of a component, just use the <xref:Bunit.IRenderedFragment.Markup> property on <xref:Bunit.IRenderedFragment>. This holds the *raw* HTML from the component as a `string`.
@@ -182,4 +133,4 @@ To get the markup as a string, do the following:
182133

183134
[!code-csharp[](../../../samples/tests/xunit/VerifyMarkupExamples.cs?start=16&end=19&highlight=3)]
184135

185-
Standard string assertions can be performed against the markup string, such as checking whether it contains a value or is empty.
136+
Standard string assertions can be performed against the markup string, such as checking whether it contains a value or is empty.

src/bunit.web/Asserting/DiffAssertExtensions.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/bunit.web/Asserting/DiffChangeAssertException.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/bunit.web/Asserting/HtmlEqualException.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using AngleSharp;
22
using AngleSharp.Diffing.Core;
3-
using Bunit;
43
using Bunit.Asserting;
54

65
namespace Bunit;

src/bunit.web/Asserting/ShouldBeAdditionAssertExtensions.cs

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)