Skip to content

Commit

Permalink
Tested Validate in addition to ValidateAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
luetm committed Feb 21, 2024
1 parent d472eaa commit 898e51b
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<EditForm Model="@_person" OnSubmit="SubmitFormAsync">
<EditForm Model="@_person" OnSubmit="SubmitAsync">
<FluentValidationValidator @ref="_fluentValidationValidator"/>
<ValidationSummary/>

Expand Down Expand Up @@ -31,7 +31,7 @@

public ValidationResultType? Result { get; private set; }

private async Task SubmitFormAsync()
private async Task SubmitAsync()
{
var result = await _fluentValidationValidator!.ValidateAsync();
Result = result ? ValidationResultType.Valid : ValidationResultType.Error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Blazored.FluentValidation.Tests.Model;
namespace Blazored.FluentValidation.Tests.AsyncValidation;

public class Tests : TestContext
namespace Blazored.FluentValidation.Tests.DirectValidation;

public class AsyncTests : TestContext
{
private readonly Fixture _fixture = new();

[Fact]
public void AsyncValidate_PersonIsValid_ResultIsValid()
public void ValidateAsync_PersonIsValid_ResultIsValid()
{
// Arrange
var cut = RenderComponent<Component>();
var cut = RenderComponent<AsyncComponent>();
var person = _fixture.ValidPerson();

// Act
Expand All @@ -22,10 +23,10 @@ public void AsyncValidate_PersonIsValid_ResultIsValid()
}

[Fact]
public void AsyncValidate_AgeNegative_ResultIsError()
public void ValidateAsync_AgeNegative_ResultIsError()
{
// Arrange
var cut = RenderComponent<Component>();
var cut = RenderComponent<AsyncComponent>();
var person = _fixture.ValidPerson() with { Age = -5 };

// Act
Expand All @@ -38,10 +39,10 @@ public void AsyncValidate_AgeNegative_ResultIsError()
}

[Fact]
public void AsyncValidate_AgeNegative_ValidationMessagesPresent()
public void ValidateAsync_AgeNegative_ValidationMessagesPresent()
{
// Arrange
var cut = RenderComponent<Component>();
var cut = RenderComponent<AsyncComponent>();
var person = _fixture.ValidPerson() with { Age = -5 };

// Act
Expand All @@ -54,7 +55,7 @@ public void AsyncValidate_AgeNegative_ValidationMessagesPresent()
cut.Find("li.validation-message").TextContent.Should().Contain(PersonValidator.AgeMin);
}

private void FillForm(IRenderedComponent<Component> cut, Person person)
private void FillForm(IRenderedComponent<AsyncComponent> cut, Person person)
{
cut.Find("input[name=FirstName]").Change(person.FirstName);
cut.Find("input[name=LastName]").Change(person.LastName);
Expand Down
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;
}
}
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]"
};
}
}

0 comments on commit 898e51b

Please sign in to comment.