Skip to content

Commit 554d01f

Browse files
authored
1.0.0-preview-01
Preview 1.0.0
2 parents 84ad062 + c6230a8 commit 554d01f

File tree

175 files changed

+4588
-2032
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+4588
-2032
lines changed

.editorconfig

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,26 @@ dotnet_diagnostic.BL0002.severity = none
3838
dotnet_diagnostic.BL0003.severity = none
3939
dotnet_diagnostic.BL0004.severity = none
4040
dotnet_diagnostic.BL0005.severity = none
41-
dotnet_diagnostic.BL0006.severity = none
41+
dotnet_diagnostic.BL0006.severity = none
42+
43+
## Code analysis configuration
44+
45+
[*.cs]
46+
47+
dotnet_diagnostic.IDE0022.severity = silent # IDE0022: Use block body for methods
48+
dotnet_diagnostic.IDE0090.severity = silent # IDE0090: Use 'new(...)'
49+
dotnet_diagnostic.S125.severity = suggestion # S125: Sections of code should not be commented out
50+
dotnet_diagnostic.S927.severity = suggestion # S927: Parameter names should match base declaration and other partial definitions
51+
dotnet_diagnostic.S1075.severity = suggestion # S1075: URIs should not be hardcoded
52+
dotnet_diagnostic.S1186.severity = silent # S1186: Methods should not be empty
53+
dotnet_diagnostic.S1199.severity = suggestion # S1199: Nested code blocks should not be used
54+
dotnet_diagnostic.S3925.severity = suggestion # S3925: "ISerializable" should be implemented correctly
55+
56+
[src/bunit.web/EventDispatchExtensions/**.cs]
57+
dotnet_diagnostic.S107.severity = none # S107: Methods should not have too many parameters
58+
59+
[tests/**.cs]
60+
dotnet_diagnostic.S125.severity = none # S125: Sections of code should not be commented out
61+
dotnet_diagnostic.CA2012.severity = none # CA2012: Use ValueTasks correctly
62+
dotnet_diagnostic.S3459.severity = none # S3459: Unassigned members should be removed
63+
dotnet_diagnostic.S1186.severity = none # S1186: Methods should not be empty

.github/workflows/main.yml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: "CI/CD"
22

33
on:
44
pull_request:
5+
branches:
6+
- dev
7+
- main
58
types:
69
- opened
710
- synchronize
@@ -229,21 +232,13 @@ jobs:
229232
- name: Creating library package for release
230233
if: github.event_name == 'release'
231234
run: |
232-
dotnet pack -c Release -o ${GITHUB_WORKSPACE}/packages -p:RepositoryBranch=main -p:ContinuousIntegrationBuild=true /p:PublicRelease=true
233-
dotnet pack src/bunit/ -c Release -o ${GITHUB_WORKSPACE}/packages -p:RepositoryBranch=main -p:ContinuousIntegrationBuild=true /p:PublicRelease=true
234-
dotnet pack src/bunit.template/ -c Release -o ${GITHUB_WORKSPACE}/packages -p:RepositoryBranch=main -p:ContinuousIntegrationBuild=true /p:PublicRelease=true
235+
dotnet pack -c Release -o ${GITHUB_WORKSPACE}/packages -p:RepositoryBranch=$BRANCH -p:ContinuousIntegrationBuild=true /p:PublicRelease=true
236+
dotnet pack src/bunit/ -c Release -o ${GITHUB_WORKSPACE}/packages -p:RepositoryBranch=$BRANCH -p:ContinuousIntegrationBuild=true /p:PublicRelease=true
237+
dotnet pack src/bunit.template/ -c Release -o ${GITHUB_WORKSPACE}/packages -p:RepositoryBranch=$BRANCH -p:ContinuousIntegrationBuild=true /p:PublicRelease=true
235238
236239
- name: Push packages to GitHub Package Registry
237-
run: |
238-
for f in ${GITHUB_WORKSPACE}/packages/*.nupkg
239-
do
240-
curl -vX PUT -u "egil:${{ secrets.GITHUB_TOKEN }}" -F package=@$f https://nuget.pkg.github.com/egil/
241-
done
242-
shell: bash
240+
run: dotnet nuget push ${GITHUB_WORKSPACE}/packages/'*.nupkg' -k ${{ secrets.GITHUB_TOKEN }} -s https://nuget.pkg.github.com/egil/index.json --skip-duplicate --no-symbols true
243241

244242
- name: Push packages to NuGet
245243
if: github.event_name == 'release'
246244
run: dotnet nuget push ${GITHUB_WORKSPACE}/packages/'*.nupkg' -k ${{ secrets.NUGET_KEY }} -s https://api.nuget.org/v3/index.json --skip-duplicate --no-symbols true
247-
248-
# - name: Push packages to GitHub Package Registry
249-
# run: dotnet nuget push ${GITHUB_WORKSPACE}/packages/'*.nupkg' --skip-duplicate

CHANGELOG.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,138 @@ All notable changes to **bUnit** will be documented in this file. The project ad
44

55
<!-- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) -->
66

7+
## UNRELEASED [1.0.0 preview 01]
8+
9+
The following section list all changes in 1.0.0 preview 01.
10+
11+
### Added
12+
List of new features.
13+
14+
- Added support for casting `BUnitJSRuntime` to `IJSInProcessRuntime` and `IJSUnmarshalledRuntime`. By [@KristofferStrube](https://github.com/KristofferStrube) in [#279](https://github.com/egil/bUnit/pull/279)
15+
16+
- Added support for triggering `@ontoggle` event handlers through a dedicated `Toggle()` method. By [@egil](https://github.com/egil) in [#256](https://github.com/egil/bUnit/pull/256).
17+
18+
- Added out of the box support for `<Virtualize>` component. When a `<Virtualize>` component is used in a component under test, it's JavaScript interop-calls are faked by bUnits JSInterop, and it should result in all items being rendered immediately. By [@egil](https://github.com/egil) in [#240](https://github.com/egil/bUnit/issues/240).
19+
20+
- Added support for components that call `ElementReference.FocusAsync`. These calls are handled by the bUnits JSInterop, that also allows you to verify that `FocusAsync` has been called for a specific element. For example, if a component has rendered an `<input>` element, then the following code will verify that it has been focused using `FocusAsync`:
21+
22+
```csharp
23+
var cut = RenderComponent<FocusingComponent>();
24+
25+
var input = cut.Find("input");
26+
27+
JSInterop.VerifyFocusAsyncInvoke()
28+
.Arguments[0] // the first argument is the ElemenetReference
29+
.ShouldBeElementReferenceTo(input);
30+
```
31+
32+
By [@egil](https://github.com/egil) in [#260](https://github.com/egil/bUnit/pull/260).
33+
34+
- Added `Render(RenderFragment)` and `Render<TComponent>(RenderFragment)` methods to `TestContext`, as well as various overloads to the `MarkupMatches` methods, that also takes a `RenderFragment` as the expected value.
35+
36+
The difference between the generic `Render` method and the non-generic one is that the generic returns an `IRenderedComponent<TComponent>`, whereas the non-generic one returns a `IRenderedFragment`.
37+
38+
Calling `Render<TComponent>(RenderFragent)` is equivalent to calling `Render(RenderFragment).FindComponent<TComponent>()`, e.g. it returns the first component in the render tree of type `TComponent`. This is different from the `RenderComponent<TComponent>()` method, where `TComponent` _is_ the root component of the render tree.
39+
40+
The main usecase for these are when writing tests inside .razor files. Here the inline syntax for declaring render fragments make these methods very useful.
41+
42+
For example, to tests the `<Counter>` page/component that is part of new Blazor apps, do the following (inside a `CounterTest.razor` file):
43+
44+
```cshtml
45+
@code
46+
{
47+
[Fact]
48+
public void Counter_Increments_When_Button_Is_Clicked()
49+
{
50+
using var ctx = new TestContext();
51+
var cut = ctx.Render(@<Counter />);
52+
53+
cut.Find("button").Click();
54+
55+
cut.Find("p").MarkupMatches(@<p>Current count: 1</p>);
56+
}
57+
}
58+
```
59+
60+
Note: This example uses xUnit, but NUnit or MSTest works equally well.
61+
62+
In addition to the new `Render` methods, a empty `BuildRenderTree` method has been added to the `TestContext` type. This makes it possible to inherit from the `TestContext` type in test components, removing the need for newing up the `TestContext` in each test.
63+
64+
This means the test component above ends up looking like this:
65+
66+
```cshtml
67+
@inherts TestContext
68+
@code
69+
{
70+
[Fact]
71+
public void Counter_Increments_When_Button_Is_Clicked()
72+
{
73+
var cut = Render(@<Counter />);
74+
75+
cut.Find("button").Click();
76+
77+
cut.Find("p").MarkupMatches(@<p>Current count: 1</p>);
78+
}
79+
}
80+
```
81+
82+
Tip: If you have multiple test components in the same folder, you can add a `_Imports.razor` file inside it and add the `@inherits TestContext` statement in that, removing the need to add it to every test component.
83+
84+
By [@egil](https://github.com/egil) in [#262](https://github.com/egil/bUnit/pull/262).
85+
86+
- Added support for `IJSRuntime.InvokeAsync<IJSObjectReference>(...)` calls from components. There is now a new setup helper methods for configuring how invocations towards JS modules should be handled. This is done with the various `SetupModule` methods available on the `BunitJSInterop` type available through the `TestContext.JSInterop` property. For example, to set up a module for handling calls to `foo.js`, do the following:
87+
88+
```c#
89+
using var ctx = new TestContext();
90+
var moduleJsInterop = ctx.JSInterop.SetupModule("foo.js");
91+
```
92+
93+
The returned `moduleJsInterop` is a `BunitJSInterop` type, which means all the normal `Setup<TResult>` and `SetupVoid` methods can be used to configure it to handle calls to the module from a component. For example, to configure a handler for a call to `hello` in the `foo.js` module, do the following:
94+
95+
```c#
96+
moduleJsInterop.SetupVoid("hello");
97+
```
98+
99+
By [@egil](https://github.com/egil) in [#288](https://github.com/egil/bUnit/pull/288).
100+
101+
- Added support for registering services in bUnits `Services` collection that implements `IAsyncDisposable`. Suggested by [@jmaillet](https://github.com/jmaillet) in [#249](https://github.com/egil/bUnit/issues/249).
102+
103+
### Changed
104+
List of changes in existing functionality.
105+
106+
- bUnit's mock IJSRuntime has been moved to an "always on" state by default, in strict mode, and is now available through `TestContext`'s `JSInterop` property. This makes it possible for first party Blazor components like the `<Virtualize>` component, which depend on JSInterop, to "just work" in tests.
107+
108+
**Compatible with previous releases:** To get the same effect as calling `Services.AddMockJSRuntime()` in beta-11, which used to add the mock IJSRuntime in "loose" mode, you now just need to change the mode of the already on JSInterop, i.e. `ctx.JSInterop.Mode = JSRuntimeMode.Loose`.
109+
110+
**Inspect registered handlers:** Since the new design allows registering invoke handlers in the context of the `TestContext`, you might need to get already registered handlers in your individual tests. This can be done with the `TryGetInvokeHandler()` method, that will return handler that can handle the parameters passed to it. E.g. to get a handler for a `IJSRuntime.InvokaAsync<string>("getValue")`, call `ctx.JSInterop.TryGetInvokeHandler<string>("getValue")`.
111+
112+
Learn more [issue #237](https://github.com/egil/bUnit/issues/237). By [@egil](https://github.com/egil) in [#247](https://github.com/egil/bUnit/pull/247).
113+
114+
- The `Setup<TResult>(string identifier, Func<IReadOnlyList<object?>, bool> argumentsMatcher)` and `SetupVoid(string identifier, Func<IReadOnlyList<object?>, bool> argumentsMatcher)` methods in bUnits JSInterop/MockJSRuntime has a new second parameter, an `InvocationMatcher`.
115+
116+
The `InvocationMatcher` type is a delegate that receives a `JSRuntimeInvoation` and returns true. The `JSRuntimeInvoation` type contains the arguments of the invocation and the identifier for the invocation. This means old code using the `Setup` and `SetupVoid` methods should be updated to use the arguments list in `JSRuntimeInvoation`, e.g., change the following call:
117+
118+
`ctx.JSInterop.Setup<string>("foo", args => args.Count == 2)` to this:
119+
`ctx.JSInterop.Setup<string>("foo", invocation => invocation.Arguments.Count == 2)`.
120+
121+
Changed added in relation to [#240](https://github.com/egil/bUnit/issues/240) in [#257](https://github.com/egil/bUnit/issues/257) by [@egil](https://github.com/egil).
122+
123+
- Changed `AddTestAuthorization` such that it works in Razor-based test contexts, i.e. on the `Fixture` and `SnapshotTest` types.
124+
125+
### Removed
126+
List of now removed features.
127+
128+
- A few bUnit internal xUnit assert helper methods, the custom `ShouldAllBe` methods, has mistakingly been part of the bunit.xunit package. These have been removed.
129+
130+
### Fixed
131+
List of any bug fixes.
132+
133+
- When an `Add` call to the component parameter collection builder was used to select a parameter that was inherited from a base component, the builder incorrectly reported the selected property/parameter as missing on the type. Reported by [@nickmuller](https://github.com/nickmuller) in [#250](https://github.com/egil/bUnit/issues/250).
134+
135+
- When an element, found in the DOM tree using the `Find()`, method was removed because of an event handler trigger on it, e.g. an `cut.Find("button").Click()` event trigger method, an `ElementNotFoundException` was thrown. Reported by [@nickmuller](https://github.com/nickmuller) in [#251](https://github.com/egil/bUnit/issues/251).
136+
137+
- In the built-in fake authentication system in bUnit, roles and claims were not available in components through the a cascading parameter of type `Task<AuthenticationState>`. Reported by [@AFAde](https://github.com/AFAde) in [#253](https://github.com/egil/bUnit/discussions/253) and fixed in [#291](https://github.com/egil/bUnit/pull/291) by [@egil](https://github.com/egil).
138+
7139
## [1.0.0-beta 11] - 2020-10-26
8140

9141
The following section list all changes in beta-11.

docs/samples/components/UserRights.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</AuthorizeView>
88
<ul>
99
<AuthorizeView>
10-
@foreach (var claim in @context.User.FindAll(x => x.Type != ClaimTypes.Name))
10+
@foreach (var claim in @context.User.FindAll(x => x.Type != ClaimTypes.Name && x.Type != ClaimTypes.Role))
1111
{
1212
<li>@GetClaimName(claim): @claim.Value</li>
1313
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

3-
<PropertyGroup>
3+
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
5-
<RazorLangVersion>3.0</RazorLangVersion>
6-
<RootNamespace>Bunit.Docs.Samples</RootNamespace>
7-
</PropertyGroup>
5+
<RazorLangVersion>3.0</RazorLangVersion>
6+
<RootNamespace>Bunit.Docs.Samples</RootNamespace>
7+
</PropertyGroup>
88

99
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
1010
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1" />
1111
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1" />
1212
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1" />
1313
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1" />
14-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1" />
14+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1" />
1515
</ItemGroup>
1616

1717
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
18-
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0-rc.1.*" />
19-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0-rc.1.*" />
20-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0-rc.1.*" />
21-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-rc.1.*" />
22-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.0-rc.1.*" />
18+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
19+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
20+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0" />
21+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0" />
22+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.0" />
2323
</ItemGroup>
2424

2525
</Project>

docs/site/docs/getting-started/fixture-details.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ In the example above, the setup and test methods are declared in a `@code { }` b
3131

3232
You can have the methods anywhere inside the test component you want, which can be useful. For example, if you have the same setup steps for multiple tests, they can be placed in a common setup method that the tests in the same test component can share, avoiding code duplication.
3333

34-
The methods can also be declared in the parameter directly, e.g.: `<Fixture Setup="f => f.Services.AddMockJsRuntime()" ...>`, to save space and when they are very short.
35-
36-
> [!TIP]
37-
> Learn more about mocking and `AddMockJsRuntime()` on the <xref:mocking-ijsruntime> page.
34+
TODO EGIL
3835

3936
**Other parameters**
4037

0 commit comments

Comments
 (0)