From 898e51b11eeee7b0cc2f2d086ed2b4b7d56b2d13 Mon Sep 17 00:00:00 2001 From: luetm Date: Wed, 21 Feb 2024 16:32:35 +0100 Subject: [PATCH] Tested `Validate` in addition to `ValidateAsync` --- .../AsyncComponent.razor} | 4 +- .../AsyncTests.cs} | 19 ++--- .../Readme.md | 0 .../DirectValidation/SyncComponent.razor | 39 ++++++++++ .../DirectValidation/SyncTests.cs | 73 +++++++++++++++++++ 5 files changed, 124 insertions(+), 11 deletions(-) rename tests/Blazored.FluentValidation.Tests/{AsyncValidation/Component.razor => DirectValidation/AsyncComponent.razor} (91%) rename tests/Blazored.FluentValidation.Tests/{AsyncValidation/Tests.cs => DirectValidation/AsyncTests.cs} (77%) rename tests/Blazored.FluentValidation.Tests/{AsyncValidation => DirectValidation}/Readme.md (100%) create mode 100644 tests/Blazored.FluentValidation.Tests/DirectValidation/SyncComponent.razor create mode 100644 tests/Blazored.FluentValidation.Tests/DirectValidation/SyncTests.cs diff --git a/tests/Blazored.FluentValidation.Tests/AsyncValidation/Component.razor b/tests/Blazored.FluentValidation.Tests/DirectValidation/AsyncComponent.razor similarity index 91% rename from tests/Blazored.FluentValidation.Tests/AsyncValidation/Component.razor rename to tests/Blazored.FluentValidation.Tests/DirectValidation/AsyncComponent.razor index 2acb1a0..c0cafe7 100644 --- a/tests/Blazored.FluentValidation.Tests/AsyncValidation/Component.razor +++ b/tests/Blazored.FluentValidation.Tests/DirectValidation/AsyncComponent.razor @@ -1,4 +1,4 @@ - + @@ -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; diff --git a/tests/Blazored.FluentValidation.Tests/AsyncValidation/Tests.cs b/tests/Blazored.FluentValidation.Tests/DirectValidation/AsyncTests.cs similarity index 77% rename from tests/Blazored.FluentValidation.Tests/AsyncValidation/Tests.cs rename to tests/Blazored.FluentValidation.Tests/DirectValidation/AsyncTests.cs index 453cec4..634efe3 100644 --- a/tests/Blazored.FluentValidation.Tests/AsyncValidation/Tests.cs +++ b/tests/Blazored.FluentValidation.Tests/DirectValidation/AsyncTests.cs @@ -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(); + var cut = RenderComponent(); var person = _fixture.ValidPerson(); // Act @@ -22,10 +23,10 @@ public void AsyncValidate_PersonIsValid_ResultIsValid() } [Fact] - public void AsyncValidate_AgeNegative_ResultIsError() + public void ValidateAsync_AgeNegative_ResultIsError() { // Arrange - var cut = RenderComponent(); + var cut = RenderComponent(); var person = _fixture.ValidPerson() with { Age = -5 }; // Act @@ -38,10 +39,10 @@ public void AsyncValidate_AgeNegative_ResultIsError() } [Fact] - public void AsyncValidate_AgeNegative_ValidationMessagesPresent() + public void ValidateAsync_AgeNegative_ValidationMessagesPresent() { // Arrange - var cut = RenderComponent(); + var cut = RenderComponent(); var person = _fixture.ValidPerson() with { Age = -5 }; // Act @@ -54,7 +55,7 @@ public void AsyncValidate_AgeNegative_ValidationMessagesPresent() cut.Find("li.validation-message").TextContent.Should().Contain(PersonValidator.AgeMin); } - private void FillForm(IRenderedComponent cut, Person person) + private void FillForm(IRenderedComponent cut, Person person) { cut.Find("input[name=FirstName]").Change(person.FirstName); cut.Find("input[name=LastName]").Change(person.LastName); diff --git a/tests/Blazored.FluentValidation.Tests/AsyncValidation/Readme.md b/tests/Blazored.FluentValidation.Tests/DirectValidation/Readme.md similarity index 100% rename from tests/Blazored.FluentValidation.Tests/AsyncValidation/Readme.md rename to tests/Blazored.FluentValidation.Tests/DirectValidation/Readme.md diff --git a/tests/Blazored.FluentValidation.Tests/DirectValidation/SyncComponent.razor b/tests/Blazored.FluentValidation.Tests/DirectValidation/SyncComponent.razor new file mode 100644 index 0000000..00a73e9 --- /dev/null +++ b/tests/Blazored.FluentValidation.Tests/DirectValidation/SyncComponent.razor @@ -0,0 +1,39 @@ + + + + +

+ + +

+ +

+ + +

+ +

+ + +

+ +

+ + +

+ + +
+ +@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; + } +} \ No newline at end of file diff --git a/tests/Blazored.FluentValidation.Tests/DirectValidation/SyncTests.cs b/tests/Blazored.FluentValidation.Tests/DirectValidation/SyncTests.cs new file mode 100644 index 0000000..cd48fcb --- /dev/null +++ b/tests/Blazored.FluentValidation.Tests/DirectValidation/SyncTests.cs @@ -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(); + 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(); + 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(); + 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 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 = "john.doe@blazored.com" + }; + } +} \ No newline at end of file