Skip to content

Commit

Permalink
store the CurrentStatus and the newly selected status, so that the co…
Browse files Browse the repository at this point in the history
…rrect radio buttons can be shown on the edit status page
  • Loading branch information
CathLass committed Feb 11, 2025
1 parent 03c1157 commit 1d42c5c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class InductionStatusRegistry

public static IReadOnlyCollection<InductionStatusInfo> All => _info.Values.ToArray();

public static IReadOnlyCollection<InductionStatusInfo> ValidStatusChangesWhenManagedByCpd =>
public static ICollection<InductionStatusInfo> ValidStatusChangesWhenManagedByCpd =>
_info
.Where(s => s.Key is InductionStatus.Exempt or InductionStatus.FailedInWales)
.Select(s => s.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class EditInductionState : IRegisterJourney

public InductionJourneyPage? JourneyStartPage { get; set; }
public InductionStatus InductionStatus { get; set; }
public InductionStatus CurrentInductionStatus { get; set; }
public DateOnly? StartDate { get; set; }
public DateOnly? CompletedDate { get; set; }
public Guid[]? ExemptionReasonIds { get; set; } = [];
Expand Down Expand Up @@ -46,11 +47,16 @@ public async Task EnsureInitializedAsync(TrsDbContext dbContext, Guid personId,

var person = await dbContext.Persons.SingleAsync(q => q.PersonId == personId);

InductionStatus = person.InductionStatus;
CurrentInductionStatus = person.InductionStatus;
JourneyStartPage = startPage;
StartDate = person.InductionStartDate;
CompletedDate = person.InductionCompletedDate;
ExemptionReasonIds = person.InductionExemptionReasonIds;
if (InductionStatus == InductionStatus.None)
{
InductionStatus = CurrentInductionStatus;
}

Initialized = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<govuk-radios-fieldset-legend data-testid="status-choices-legend" class="govuk-fieldset__legend--l" is-page-heading="true" />
@foreach (var inductionStatus in Model.StatusChoices)
{
<govuk-radios-item value="@inductionStatus.Value" checked="@(Model.InductionStatus == inductionStatus.Value && Model.FromCheckAnswers == JourneyFromCheckYourAnswersPage.CheckYourAnswers)">@inductionStatus.Title</govuk-radios-item>
<govuk-radios-item value="@inductionStatus.Value" checked="@(Model.InductionStatus == inductionStatus.Value)">@inductionStatus.Title</govuk-radios-item>
}
</govuk-radios-fieldset>
</govuk-radios>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ public class StatusModel(TrsLinkGenerator linkGenerator, TrsDbContext dbContext,
[Display(Name = "What is their induction status?")]
[NotEqual(InductionStatus.None, ErrorMessage = "Select a status")]
public InductionStatus InductionStatus { get; set; }
public InductionStatus CurrentInductionStatus { get; set; }
public string? PersonName { get; set; }
public IEnumerable<InductionStatusInfo> StatusChoices
{
get
{
return _inductionStatusManagedByCpd && (InductionStatus is not InductionStatus.FailedInWales and not InductionStatus.Exempt) ?
InductionStatusRegistry.ValidStatusChangesWhenManagedByCpd
return _inductionStatusManagedByCpd && (CurrentInductionStatus is not InductionStatus.FailedInWales and not InductionStatus.Exempt) ?
InductionStatusRegistry.ValidStatusChangesWhenManagedByCpd
.Concat(new[] { InductionStatusRegistry.All.Single(i => i.Value == CurrentInductionStatus) })
.OrderBy(i => i.Value)
.ToArray()
: InductionStatusRegistry.All.ToArray()[1..];
}
}

public string InductionIsManagedByCpdWarning => InductionStatus switch
public string InductionIsManagedByCpdWarning => CurrentInductionStatus switch
{
InductionStatus.RequiredToComplete => InductionWarnings.InductionIsManagedByCpdWarningRequiredToComplete,
InductionStatus.InProgress => InductionWarnings.InductionIsManagedByCpdWarningInProgress,
Expand All @@ -39,7 +43,7 @@ public IEnumerable<InductionStatusInfo> StatusChoices
_ => InductionWarnings.InductionIsManagedByCpdWarningOther
};

public string? StatusWarningMessage => _inductionStatusManagedByCpd && (InductionStatus is not InductionStatus.FailedInWales and not InductionStatus.Exempt) ? InductionIsManagedByCpdWarning : null;
public string? StatusWarningMessage => _inductionStatusManagedByCpd && (CurrentInductionStatus is not InductionStatus.FailedInWales and not InductionStatus.Exempt) ? InductionIsManagedByCpdWarning : null;

public InductionJourneyPage NextPage =>
InductionStatus switch
Expand Down Expand Up @@ -103,6 +107,7 @@ public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingConte

var person = await dbContext.Persons.SingleAsync(q => q.PersonId == PersonId);
_inductionStatusManagedByCpd = person.InductionStatusManagedByCpd(clock.Today);
CurrentInductionStatus = JourneyInstance!.State.CurrentInductionStatus;

await next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace TeachingRecordSystem.SupportUi.Tests.PageTests.Persons.PersonDetail.Ed
public class EditInductionStateBuilder
{
private InductionStatus InductionStatus { get; set; }
private InductionStatus CurrentInductionStatus { get; set; }
private DateOnly? StartDate { get; set; }
private DateOnly? CompletedDate { get; set; }
private Guid[]? ExemptionReasonIds { get; set; }
Expand All @@ -23,6 +24,7 @@ public EditInductionStateBuilder WithInitializedState(InductionStatus currentInd
this.Initialized = true;
JourneyStartPage = startPage;
InductionStatus = currentInductionStatus;
CurrentInductionStatus = currentInductionStatus;
return this;
}

Expand Down Expand Up @@ -84,6 +86,7 @@ public EditInductionState Build()
return new EditInductionState()
{
InductionStatus = InductionStatus,
CurrentInductionStatus = CurrentInductionStatus,
StartDate = StartDate,
CompletedDate = CompletedDate,
ExemptionReasonIds = ExemptionReasonIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ public async Task Get_InductionNotManagedByCpd_ExpectedRadioButtonsExistOnPage()
Assert.Equal(expectedChoices, statusChoices);
}

[Fact]
public async Task Get_InductionManagedByCpd_ExpectedRadioButtonsExistOnPage()
[Theory]
[InlineData(InductionStatus.Passed)]
[InlineData(InductionStatus.InProgress)]
[InlineData(InductionStatus.RequiredToComplete)]
[InlineData(InductionStatus.Failed)]
public async Task Get_InductionManagedByCpd_ExpectedRadioButtonsExistOnPage(InductionStatus currentInductionStatus)
{
// Arrange
var currentInductionStatus = InductionStatus.Passed;
InductionStatus[] expectedStatuses = { InductionStatus.Exempt, InductionStatus.FailedInWales };
InductionStatus[] expectedStatuses = new List<InductionStatus>(){ InductionStatus.Exempt, InductionStatus.FailedInWales, currentInductionStatus }.OrderBy(i => i).ToArray();
var expectedChoices = expectedStatuses.Select(s => s.ToString());
var lessThanSevenYearsAgo = Clock.Today.AddYears(-1);
var person = await TestData.CreatePersonAsync(p => p.WithQts());
Expand Down Expand Up @@ -189,36 +192,11 @@ await WithDbContext(async dbContext =>
// Assert
var doc = await AssertEx.HtmlResponseAsync(response);
var statusChoices = doc.QuerySelectorAll<IHtmlInputElement>("[type=radio]").Select(r => r.Value);
var statusChoicesLegend = doc.GetElementByTestId("status-choices-legend");
Assert.Equal("What is their induction status?", statusChoicesLegend!.TextContent);
Assert.Equal(expectedChoices, statusChoices);
}

[Fact]
public async Task Get_InductionStatus_ShowsAllRadioButtonsNotSelected()
{
// Arrange
var person = await TestData.CreatePersonAsync(p => p.WithQts());
var currentInductionStatus = InductionStatus.RequiredToComplete;
var journeyInstance = await CreateJourneyInstanceAsync(
person.PersonId,
new EditInductionStateBuilder()
.WithInitializedState(currentInductionStatus, InductionJourneyPage.Status)
.Build());

var request = new HttpRequestMessage(HttpMethod.Get, $"/persons/{person.PersonId}/edit-induction/status?{journeyInstance.GetUniqueIdQueryParameter()}");

// Act
var response = await HttpClient.SendAsync(request);

// Assert
var doc = await AssertEx.HtmlResponseAsync(response);
var selectedStatus = doc.QuerySelectorAll<IHtmlInputElement>("[type=radio]").Where(r => r.IsChecked == true);
Assert.Empty(selectedStatus);
}

[Fact]
public async Task Get_InductionStatusFromCya_ShowsSelectedRadioButton()
public async Task Get_InductionStatus_ShowsSelectedRadioButton()
{
// Arrange
var person = await TestData.CreatePersonAsync(p => p.WithQts());
Expand Down

0 comments on commit 1d42c5c

Please sign in to comment.