Skip to content

Commit

Permalink
Tests for SingleChoice field type
Browse files Browse the repository at this point in the history
  • Loading branch information
andymantell committed Sep 27, 2024
1 parent 52cc1ef commit 9b03f8f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using CO.CDP.OrganisationApp.Models;
using CO.CDP.OrganisationApp.Pages.Forms;
using FluentAssertions;
using System.ComponentModel.DataAnnotations;

namespace CO.CDP.OrganisationApp.Tests.Pages.Forms;

public class FormElementSingleChoiceModelTest
{
private readonly FormElementSingleChoiceModel _model;

public FormElementSingleChoiceModelTest()
{
_model = new FormElementSingleChoiceModel();
_model.Options = new FormQuestionOptions() { Choices = ["Option 1", "Option 2", "Option 3"] } ;
}

[Theory]
[InlineData(null, null)]
[InlineData(" ", null)]
[InlineData("yes", null)]
[InlineData("no", null)]
[InlineData("Option 1", "Option 1")]
[InlineData("Option 2", "Option 2")]
[InlineData("Option 3", "Option 3")]
public void GetAnswer_GetsExpectedFormAnswer(string? input, string? expected)
{
_model.SelectedOption = input;

var answer = _model.GetAnswer();

if (expected == null)
{
answer.Should().BeNull();
}
else
{
answer.Should().NotBeNull();
answer!.OptionValue.Should().Be(expected);
}
}

[Theory]
[InlineData(null, null)]
[InlineData(" ", null)]
[InlineData("yes", null)]
[InlineData("no", null)]
[InlineData("Option 1", "Option 1")]
[InlineData("Option 2", "Option 2")]
[InlineData("Option 3", "Option 3")]
public void SetAnswer_SetsExpectedOption(string? selectedOption, string? expected)
{
var answer = new FormAnswer { OptionValue = selectedOption };

_model.SetAnswer(answer);

if (expected == null)
{
_model.SelectedOption.Should().BeNull();
}
else
{
_model.SelectedOption.Should().NotBeNull();
_model.SelectedOption.Should().Be(expected);
}
}

[Theory]
[InlineData(null, "Select an option")]
[InlineData(" ", "Select an option")]
[InlineData("yes", "Invalid option selected")]
[InlineData("no", "Invalid option selected")]
[InlineData("Option 1", null)]
[InlineData("Option 2", null)]
[InlineData("Option 3", null)]
public void Validate_ReturnsExpectedResults(string? selectedOption, string? expectedErrorMessage)
{
_model.SelectedOption = selectedOption;

var validationResults = _model.Validate(new ValidationContext(_model)).ToList();

if (expectedErrorMessage != null)
{
validationResults.Should().ContainSingle();
validationResults.First().ErrorMessage.Should().Be(expectedErrorMessage);
}
else
{
validationResults.Should().BeEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ public class FormElementSingleChoiceModel : FormElementModel, IValidatableObject

public override FormAnswer? GetAnswer()
{
return string.IsNullOrWhiteSpace(SelectedOption) ? null : new FormAnswer { OptionValue = SelectedOption };
if(
SelectedOption != null
&& Options?.Choices != null
&& Options.Choices.Contains(SelectedOption)
)
{
return new FormAnswer { OptionValue = SelectedOption };
}

return null;
}

public override void SetAnswer(FormAnswer? answer)
{
if (answer?.OptionValue != null)
if (answer?.OptionValue != null && Options?.Choices != null && Options.Choices.Contains(answer.OptionValue))
{
SelectedOption = answer.OptionValue;
}
Expand All @@ -28,7 +37,14 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex

if (string.IsNullOrWhiteSpace(SelectedOption))
{
yield return new ValidationResult("All information is required on this page", new[] { nameof(SelectedOption) });
yield return new ValidationResult("Select an option", new[] { nameof(SelectedOption) });
yield break;
}

if(Options?.Choices == null || (SelectedOption != null && !Options.Choices.Contains(SelectedOption)))
{
yield return new ValidationResult("Invalid option selected", new[] { nameof(SelectedOption) });
yield break;
}
}
}

0 comments on commit 9b03f8f

Please sign in to comment.