-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #683 from cabinetoffice/feature/dp-543-exclusions-…
…single-choice-question-type Feature/dp 543 exclusions single choice question type
- Loading branch information
Showing
14 changed files
with
2,243 additions
and
34 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
106 changes: 106 additions & 0 deletions
106
...tend/CO.CDP.OrganisationApp.Tests/Pages/Forms/FormElementGroupedSingleChoiceModelTests.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,106 @@ | ||
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 FormElementGroupedSingleChoiceModelTests | ||
{ | ||
private readonly FormElementGroupedSingleChoiceModel _model; | ||
|
||
public FormElementGroupedSingleChoiceModelTests() | ||
{ | ||
_model = new FormElementGroupedSingleChoiceModel | ||
{ | ||
Options = new FormQuestionOptions | ||
{ | ||
Groups = new List<FormQuestionGroup> | ||
{ | ||
new FormQuestionGroup | ||
{ | ||
Name = "Group 1", | ||
Hint = "Group 1 Hint", | ||
Caption = "Group 1 Caption", | ||
Choices = new List<FormQuestionGroupChoice> | ||
{ | ||
new FormQuestionGroupChoice { Title = "Group Choice 1", Value = "group_choice_1" }, | ||
new FormQuestionGroupChoice { Title = "Group Choice 2", Value = "group_choice_2" } | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void GetAnswer_ShouldReturnNull_WhenNoOptionIsSelected() | ||
{ | ||
_model.SelectedOption = null; | ||
var answer = _model.GetAnswer(); | ||
answer.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void GetAnswer_ShouldReturnFormAnswer_WhenValidOptionIsSelected() | ||
{ | ||
_model.SelectedOption = "group_choice_1"; | ||
var answer = _model.GetAnswer(); | ||
|
||
answer.Should().NotBeNull(); | ||
answer!.OptionValue.Should().Be("group_choice_1"); | ||
} | ||
|
||
[Fact] | ||
public void GetAnswer_ShouldReturnNull_WhenInvalidOptionIsSelected() | ||
{ | ||
_model.SelectedOption = "invalid_choice"; | ||
|
||
var answer = _model.GetAnswer(); | ||
answer.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void SetAnswer_ShouldSetSelectedOption_WhenValidOptionIsProvided() | ||
{ | ||
var answer = new FormAnswer { OptionValue = "group_choice_2" }; | ||
|
||
_model.SetAnswer(answer); | ||
_model.SelectedOption.Should().Be("group_choice_2"); | ||
} | ||
|
||
[Fact] | ||
public void SetAnswer_ShouldNotSetSelectedOption_WhenInvalidOptionIsProvided() | ||
{ | ||
var answer = new FormAnswer { OptionValue = "invalid_choice" }; | ||
|
||
_model.SetAnswer(answer); | ||
_model.SelectedOption.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnValidationError_WhenNoOptionIsSelected() | ||
{ | ||
_model.SelectedOption = null; | ||
|
||
var validationResults = _model.Validate(new ValidationContext(_model)); | ||
validationResults.Should().ContainSingle(v => v.ErrorMessage == "Select an option"); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnValidationError_WhenInvalidOptionIsSelected() | ||
{ | ||
_model.SelectedOption = "invalid_choice"; | ||
|
||
var validationResults = _model.Validate(new ValidationContext(_model)); | ||
validationResults.Should().ContainSingle(v => v.ErrorMessage == "Invalid option selected"); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnNoError_WhenValidOptionIsSelected() | ||
{ | ||
_model.SelectedOption = "group_choice_1"; | ||
|
||
var validationResults = _model.Validate(new ValidationContext(_model)); | ||
validationResults.Should().BeEmpty(); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
Frontend/CO.CDP.OrganisationApp/Pages/Forms/FormElementGroupedSingleChoiceModel.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,53 @@ | ||
using CO.CDP.OrganisationApp.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace CO.CDP.OrganisationApp.Pages.Forms; | ||
|
||
public class FormElementGroupedSingleChoiceModel : FormElementModel, IValidatableObject | ||
{ | ||
[BindProperty] | ||
public string? SelectedOption { get; set; } | ||
|
||
public override FormAnswer? GetAnswer() | ||
{ | ||
if (SelectedOption != null && Options?.Groups != null) | ||
{ | ||
var isValidOption = Options.Groups.Any(group => group.Choices != null && group.Choices.Any(choice => choice.Value == SelectedOption)); | ||
|
||
if (isValidOption) | ||
{ | ||
return new FormAnswer { OptionValue = SelectedOption }; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public override void SetAnswer(FormAnswer? answer) | ||
{ | ||
if (answer?.OptionValue != null && Options?.Groups != null) | ||
{ | ||
var isValidOption = Options.Groups.Any(group => group.Choices != null && group.Choices.Any(choice => choice.Value == answer.OptionValue)); | ||
|
||
if (isValidOption) | ||
{ | ||
SelectedOption = answer.OptionValue; | ||
} | ||
} | ||
} | ||
|
||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
if (string.IsNullOrWhiteSpace(SelectedOption)) | ||
{ | ||
yield return new ValidationResult("Select an option", new[] { nameof(SelectedOption) }); | ||
yield break; | ||
} | ||
|
||
if (Options?.Groups == null || !Options.Groups.Any(group => group.Choices != null && group.Choices.Any(choice => choice.Value == SelectedOption))) | ||
{ | ||
yield return new ValidationResult("Invalid option selected", new[] { nameof(SelectedOption) }); | ||
} | ||
} | ||
} |
Oops, something went wrong.