-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tested
Validate
in addition to ValidateAsync
- Loading branch information
Showing
5 changed files
with
124 additions
and
11 deletions.
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
File renamed without changes.
39 changes: 39 additions & 0 deletions
39
tests/Blazored.FluentValidation.Tests/DirectValidation/SyncComponent.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,39 @@ | ||
<EditForm Model="@_person" OnSubmit="Submit"> | ||
<FluentValidationValidator @ref="_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> | ||
|
||
<button type="submit">Save</button> | ||
</EditForm> | ||
|
||
@code { | ||
private readonly Person _person = new(); | ||
private FluentValidationValidator? _fluentValidationValidator; | ||
|
||
public ValidationResultType? Result { get; private set; } | ||
|
||
private void Submit() | ||
{ | ||
var result = _fluentValidationValidator!.Validate(); | ||
Result = result ? ValidationResultType.Valid : ValidationResultType.Error; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
tests/Blazored.FluentValidation.Tests/DirectValidation/SyncTests.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,73 @@ | ||
using Blazored.FluentValidation.Tests.Model; | ||
|
||
namespace Blazored.FluentValidation.Tests.DirectValidation; | ||
|
||
public class SyncTests : TestContext | ||
{ | ||
private readonly Fixture _fixture = new(); | ||
|
||
[Fact] | ||
public void Validate_PersonIsValid_ResultIsValid() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<SyncComponent>(); | ||
var person = _fixture.ValidPerson(); | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Valid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_AgeNegative_ResultIsError() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<SyncComponent>(); | ||
var person = _fixture.ValidPerson() with { Age = -5 }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
[Fact] | ||
public void Validate_AgeNegative_ValidationMessagesPresent() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<SyncComponent>(); | ||
var person = _fixture.ValidPerson() with { Age = -5 }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Find(".validation-errors>.validation-message").TextContent.Should().Contain(PersonValidator.AgeMin); | ||
cut.Find("li.validation-message").TextContent.Should().Contain(PersonValidator.AgeMin); | ||
} | ||
|
||
private void FillForm(IRenderedComponent<SyncComponent> cut, Person person) | ||
{ | ||
cut.Find("input[name=FirstName]").Change(person.FirstName); | ||
cut.Find("input[name=LastName]").Change(person.LastName); | ||
cut.Find("input[name=Age]").Change(person.Age.ToString()); | ||
cut.Find("input[name=EmailAddress]").Change(person.EmailAddress); | ||
} | ||
|
||
private class Fixture | ||
{ | ||
public Person ValidPerson() => new() | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Age = 30, | ||
EmailAddress = "[email protected]" | ||
}; | ||
} | ||
} |