-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created tests for the first to features in the README.
- Loading branch information
Showing
14 changed files
with
435 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/Blazored.FluentValidation.Tests/AssemblyScanning/Component.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
@using Blazored.FluentValidation.Tests.Model | ||
<EditForm | ||
Model="@_person" | ||
OnValidSubmit="ValidSubmit" | ||
OnInvalidSubmit="InvalidSubmit"> | ||
|
||
@if (DisableAssemblyScanning is null) | ||
{ | ||
<FluentValidationValidator /> | ||
} | ||
else | ||
{ | ||
<FluentValidationValidator DisableAssemblyScanning="DisableAssemblyScanning.Value" /> | ||
} | ||
|
||
<ValidationSummary/> | ||
|
||
<p> | ||
<label>First name: </label> | ||
<InputText name="@nameof(_person.FirstName)" @bind-Value="@_person.FirstName"/> | ||
</p> | ||
|
||
<button type="submit">Save</button> | ||
</EditForm> | ||
|
||
@code { | ||
[Parameter] public bool? DisableAssemblyScanning { get; set; } | ||
private readonly Person _person = new(); | ||
|
||
internal ValidationResultType Result { get; private set; } = ValidationResultType.Valid; | ||
|
||
private void ValidSubmit() => Result = ValidationResultType.Valid; | ||
private void InvalidSubmit() => Result = ValidationResultType.Error; | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Blazored.FluentValidation.Tests/AssemblyScanning/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### What does this test? | ||
This test checks if the assembly scanning works. It leverages, that this test | ||
assembly does not register any `AbstractValidator` by default. | ||
|
||
- Setting the `DisableAssemblyScanning` to `true` should not find any validators and ignore errors. | ||
- Setting the `DisableAssemblyScanning` to `false` or not setting the attribute at all, should | ||
find the validators in the assembly and validate normally. |
64 changes: 64 additions & 0 deletions
64
tests/Blazored.FluentValidation.Tests/AssemblyScanning/Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Blazored.FluentValidation.Tests.Model; | ||
|
||
namespace Blazored.FluentValidation.Tests.AssemblyScanning; | ||
|
||
public class Tests : TestContext | ||
{ | ||
private readonly Fixture _fixture = new(); | ||
|
||
[Fact] | ||
public void DisableAssemblyScanning_SetToTrue_NoValidationHappens() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(p => p.Add(c => c.DisableAssemblyScanning, true)); | ||
var person = _fixture.InvalidPerson(); | ||
|
||
// Act | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Valid); | ||
} | ||
|
||
[Fact] | ||
public void DisableAssemblyScanning_SetToFalse_ValidationHappens() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(p => p.Add(c => c.DisableAssemblyScanning, false)); | ||
var person = _fixture.InvalidPerson(); | ||
|
||
// Act | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
[Fact] | ||
public void DisableAssemblyScanning_NotSet_ValidationHappens() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(p => p.Add(c => c.DisableAssemblyScanning, null)); | ||
var person = _fixture.InvalidPerson(); | ||
|
||
// Act | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
private class Fixture | ||
{ | ||
public Person InvalidPerson() => new() | ||
{ | ||
FirstName = "", | ||
LastName = "Doe", | ||
EmailAddress = "[email protected]", | ||
Age = 30 | ||
}; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
tests/Blazored.FluentValidation.Tests/BasicValidation/Component.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@using Blazored.FluentValidation.Tests.Model | ||
<EditForm | ||
Model="@_person" | ||
OnValidSubmit="ValidSubmit" | ||
OnInvalidSubmit="InvalidSubmit"> | ||
|
||
<FluentValidationValidator/> | ||
<ValidationSummary/> | ||
|
||
<p> | ||
<label>First name: </label> | ||
<InputText name="@nameof(_person.FirstName)" @bind-Value="@_person.FirstName"/> | ||
</p> | ||
|
||
<p> | ||
<label>Last name: </label> | ||
<InputText name="@nameof(_person.LastName)" @bind-Value="@_person.LastName"/> | ||
</p> | ||
|
||
<p> | ||
<label>Age: </label> | ||
<InputNumber name="@nameof(_person.Age)" @bind-Value="@_person.Age"/> | ||
</p> | ||
|
||
<p> | ||
<label>Email Address: </label> | ||
<InputText name="@nameof(_person.EmailAddress)" @bind-Value="@_person.EmailAddress"/> | ||
</p> | ||
|
||
<p> | ||
<label>Address Line 1: </label> | ||
<InputText name="@nameof(_person.Address.Line1)" @bind-Value="@_person.Address.Line1"/> | ||
</p> | ||
|
||
<button type="submit">Save</button> | ||
</EditForm> | ||
|
||
@code { | ||
private readonly Person _person = new(); | ||
internal ValidationResultType Result { get; private set; } = ValidationResultType.Valid; | ||
|
||
private void ValidSubmit() => Result = ValidationResultType.Valid; | ||
private void InvalidSubmit() => Result = ValidationResultType.Error; | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Blazored.FluentValidation.Tests/BasicValidation/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### What does this test? | ||
This test checks if the basic validation works. | ||
|
||
- Does a valid model pass the validation? | ||
- Do basic validation rules get picked up? | ||
- Do validation errors get displayed correctly in the UI? | ||
- Are nested rules validated correctly? |
110 changes: 110 additions & 0 deletions
110
tests/Blazored.FluentValidation.Tests/BasicValidation/Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Blazored.FluentValidation.Tests.Model; | ||
|
||
namespace Blazored.FluentValidation.Tests.BasicValidation; | ||
|
||
public class Tests : TestContext | ||
{ | ||
private readonly Fixture _fixture = new(); | ||
|
||
[Fact] | ||
public void Validate_DataIsValid_ValidSubmit() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson(); | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Valid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_FirstNameMissing_InvalidSubmit() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { FirstName = string.Empty }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
[Fact] | ||
public void Validate_FirstNameMissing_ValidationErrorsPresent() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { FirstName = string.Empty }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Find(".validation-errors>.validation-message").TextContent.Should().Contain(PersonValidator.FirstNameRequired); | ||
cut.Find("li.validation-message").TextContent.Should().Contain(PersonValidator.FirstNameRequired); | ||
} | ||
|
||
[Fact] | ||
public void Validate_AgeTooOld_ValidationErrorsPresent() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { Age = 250 }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Find(".validation-errors>.validation-message").TextContent.Should().Contain(PersonValidator.AgeMax); | ||
} | ||
|
||
[Fact] | ||
public void Validate_AddressLine1Missing_ValidationErrorsPresent() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { Address = new() { Line1 = string.Empty } }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Find(".validation-errors>.validation-message").TextContent.Should().Contain(AddressValidator.Line1Required); | ||
} | ||
|
||
private static void FillForm(IRenderedComponent<Component> cut, Person person) | ||
{ | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find($"input[name={nameof(Person.LastName)}]").Change(person.LastName); | ||
cut.Find($"input[name={nameof(Person.EmailAddress)}]").Change(person.EmailAddress); | ||
cut.Find($"input[name={nameof(Person.Age)}]").Change(person.Age.ToString()); | ||
cut.Find($"input[name={nameof(Person.Address.Line1)}]").Change(person.Address.Line1); | ||
} | ||
|
||
private class Fixture | ||
{ | ||
public Person ValidPerson() => new() | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
EmailAddress = "[email protected]", | ||
Age = 30, | ||
Address = new() | ||
{ | ||
Line1 = "123 Main St", | ||
Town = "Springfield", | ||
Postcode = "12345" | ||
} | ||
}; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Blazored.FluentValidation.Tests/Blazored.FluentValidation.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Bunit" /> | ||
<Using Include="Bunit.TestDoubles" /> | ||
<Using Include="Microsoft.Extensions.DependencyInjection" /> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="bunit" Version="1.26.64" /> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="FluentValidation" Version="11.9.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="xunit" Version="2.6.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Blazored.FluentValidation\Blazored.FluentValidation.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global using System.Threading.Tasks; | ||
global using FluentAssertions; | ||
global using FluentValidation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Blazored.FluentValidation.Tests.Model | ||
{ | ||
public record Address | ||
{ | ||
public string? Line1 { get; set; } | ||
public string? Line2 { get; set; } | ||
public string? Town { get; set; } | ||
public string? County { get; set; } | ||
public string? Postcode { get; set; } | ||
} | ||
|
||
public class AddressValidator : AbstractValidator<Address> | ||
{ | ||
public const string Line1Required = "You must enter Line 1"; | ||
public const string TownRequired = "You must enter a town"; | ||
public const string CountyRequired = "You must enter a county"; | ||
public const string PostcodeRequired = "You must enter a postcode"; | ||
|
||
public AddressValidator() | ||
{ | ||
RuleFor(p => p.Line1).NotEmpty().WithMessage(Line1Required); | ||
RuleFor(p => p.Town).NotEmpty().WithMessage(TownRequired); | ||
RuleFor(p => p.County).NotEmpty().WithMessage(CountyRequired); | ||
RuleFor(p => p.Postcode).NotEmpty().WithMessage(PostcodeRequired); | ||
} | ||
} | ||
} |
Oops, something went wrong.