Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSP-662 Employer AAN Web Check Your Answers #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using SFA.DAS.Employer.Aan.Web.Controllers.Onboarding;
using SFA.DAS.Employer.Aan.Web.Infrastructure;

namespace SFA.DAS.Employer.Aan.Web.UnitTests.Controllers.Onboarding.CheckYourAnswersControllerTests;

[TestFixture]
public class CheckYourAnswersControllerAttributeTests
{
[Test]
public void Controller_HasCorrectRouteAttribute()
{
typeof(CheckYourAnswersController).Should().BeDecoratedWith<RouteAttribute>();
typeof(CheckYourAnswersController).Should().BeDecoratedWith<RouteAttribute>().Subject.Template.Should().Be("onboarding/check-your-answers");
typeof(CheckYourAnswersController).Should().BeDecoratedWith<RouteAttribute>().Subject.Name.Should().Be(RouteNames.Onboarding.CheckYourAnswers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Moq;
using SFA.DAS.Employer.Aan.Domain.Interfaces;
using SFA.DAS.Employer.Aan.Web.Controllers.Onboarding;
using SFA.DAS.Employer.Aan.Web.Models.Onboarding;
using SFA.DAS.Employer.Aan.Web.UnitTests.TestHelpers;

namespace SFA.DAS.Employer.Aan.Web.UnitTests.Controllers.Onboarding.CheckYourAnswersControllerTests.WhenGetIsInvoked;

public class AndSessionModelIsNotPopulated
{
ViewResult getResult;
CheckYourAnswersViewModel viewModel;
CheckYourAnswersController sut;
OnboardingSessionModel sessionModel;

[SetUp]
public void Init()
{
sessionModel = new();
Mock<ISessionService> sessionServiceMock = new();
sessionServiceMock.Setup(s => s.Get<OnboardingSessionModel>()).Returns(sessionModel);
sut = new(sessionServiceMock.Object);

sut.AddUrlHelperMock();

sessionModel.Regions = new();

getResult = sut.Get().As<ViewResult>();
viewModel = getResult.Model.As<CheckYourAnswersViewModel>();
}

[Test]
public void ThenReturnsViewResults()
{
getResult.Should().NotBeNull();
getResult.ViewName.Should().Be(CheckYourAnswersController.ViewPath);
}

[Test]
public void ThenSetsRegionToNullInViewModel()
{
viewModel.Region.Should().BeEmpty();
}

[TearDown]
public void Dispose()
{
sut = null!;
getResult = null!;
viewModel = null!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Moq;
using SFA.DAS.Employer.Aan.Domain.Interfaces;
using SFA.DAS.Employer.Aan.Web.Controllers.Onboarding;
using SFA.DAS.Employer.Aan.Web.Infrastructure;
using SFA.DAS.Employer.Aan.Web.Models;
using SFA.DAS.Employer.Aan.Web.Models.Onboarding;
using SFA.DAS.Employer.Aan.Web.UnitTests.TestHelpers;

namespace SFA.DAS.Employer.Aan.Web.UnitTests.Controllers.Onboarding.CheckYourAnswersControllerTests.WhenGetIsInvoked;

public class AndSessionModelIsPopulated
{
OnboardingSessionModel sessionModel;
CheckYourAnswersController sut;
ViewResult getResult;
CheckYourAnswersViewModel viewModel;

static readonly string RegionUrl = Guid.NewGuid().ToString();
static readonly string LocallyPreferredRegion = Guid.NewGuid().ToString();
static readonly List<RegionModel> MultipleRegionsSelected = new()
{
new RegionModel() { Area = LocallyPreferredRegion, IsSelected = true, IsConfirmed = true},
new RegionModel() { Area = Guid.NewGuid().ToString(), IsSelected = true, IsConfirmed = false},
new RegionModel() { Area = Guid.NewGuid().ToString(), IsSelected = true, IsConfirmed = false},
new RegionModel() { Area = Guid.NewGuid().ToString(), IsSelected = true, IsConfirmed = false}
};

static readonly List<RegionModel> SingleRegionSelected = new()
{
new RegionModel() { Area = Guid.NewGuid().ToString(), IsSelected = true, IsConfirmed = false},
new RegionModel() { Area = Guid.NewGuid().ToString(), IsSelected = true, IsConfirmed = false},
new RegionModel() { Area = Guid.NewGuid().ToString(), IsSelected = true, IsConfirmed = false},
new RegionModel() { Area = Guid.NewGuid().ToString(), IsSelected = true, IsConfirmed = false}
};

[SetUp]
public void Init()
{
sessionModel = new();
Mock<ISessionService> sessionServiceMock = new();
sessionServiceMock.Setup(s => s.Get<OnboardingSessionModel>()).Returns(sessionModel);
sut = new(sessionServiceMock.Object);

sut.AddUrlHelperMock()
.AddUrlForRoute(RouteNames.Onboarding.Regions, RegionUrl);

sessionModel.Regions = MultipleRegionsSelected;
InvokeControllerGet();
}

private void InvokeControllerGet()
{
getResult = sut.Get().As<ViewResult>();
viewModel = getResult.Model.As<CheckYourAnswersViewModel>();
}

[Test]
public void ThenReturnsViewResults()
{
InvokeControllerGet();
getResult.Should().NotBeNull();
getResult.ViewName.Should().Be(CheckYourAnswersController.ViewPath);
}

[Test]
public void ThenSetsRegionChangeLinkInViewModel()
{
InvokeControllerGet();
viewModel.RegionChangeLink.Should().Be(RegionUrl);
}

[Test]
public void ThenSetsSingleRegionInViewModel()
{
sessionModel.Regions = SingleRegionSelected;
InvokeControllerGet();
viewModel.Region.Should().Equal(SingleRegionSelected.Where(x => x.IsSelected).Select(x => x.Area).ToList());
}

[Test]
public void ThenSetsLocallyPreferredRegionInViewModel()
{
sessionModel.Regions = MultipleRegionsSelected;
InvokeControllerGet();
var result = MultipleRegionsSelected.Where(x => x.IsSelected).Select(x => x.Area).ToList();
result.Add($"Locally prefers {LocallyPreferredRegion}");

viewModel.Region.Should().Equal(result);
}

[TearDown]
public void Dispose()
{
sessionModel = null!;
sut = null!;
getResult = null!;
viewModel = null!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ public async Task Get_ViewResult_HasCorrectViewPath(
}

[Test, MoqAutoData]
public async Task Get_ViewModelHasBackLinkToTermsAndConditions(
public async Task Get_ViewModelHasSeenPreviewIsFalse_BackLinkSetsToTermsAndConditions(
[Frozen] Mock<ISessionService> sessionServiceMock,
[Greedy] RegionsController sut,
OnboardingSessionModel sessionModel,
string termsAndConditionsUrl,
CancellationToken cancellationToken)
{
sut.AddUrlHelperMock().AddUrlForRoute(RouteNames.Onboarding.TermsAndConditions, termsAndConditionsUrl);
sessionModel.HasSeenPreview = false;
sessionServiceMock.Setup(s => s.Get<OnboardingSessionModel>()).Returns(sessionModel);

var result = await sut.Get(cancellationToken);
Expand All @@ -70,4 +71,21 @@ public async Task Get_ViewModel_HasSessionData(
var result = await sut.Get(cancellationToken);
result.As<ViewResult>().Model.As<RegionsViewModel>().Regions.Should().Equal(sessionModel.Regions);
}

[MoqAutoData]
public async Task Get_ViewModelHasSeenPreviewIsTrue_BackLinkSetsToCheckYourAnswers(
[Frozen] Mock<ISessionService> sessionServiceMock,
[Greedy] RegionsController sut,
OnboardingSessionModel sessionModel,
string checkYourAnswersUrl,
CancellationToken cancellationToken)
{
sut.AddUrlHelperMock().AddUrlForRoute(RouteNames.Onboarding.CheckYourAnswers, checkYourAnswersUrl);
sessionModel.HasSeenPreview = true;
sessionServiceMock.Setup(s => s.Get<OnboardingSessionModel>()).Returns(sessionModel);

var result = await sut.Get(cancellationToken);

result.As<ViewResult>().Model.As<RegionsViewModel>().BackLink.Should().Be(checkYourAnswersUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Mvc;
using SFA.DAS.Employer.Aan.Domain.Interfaces;
using SFA.DAS.Employer.Aan.Web.Infrastructure;
using SFA.DAS.Employer.Aan.Web.Models.Onboarding;

namespace SFA.DAS.Employer.Aan.Web.Controllers.Onboarding;

[Route("onboarding/check-your-answers", Name = RouteNames.Onboarding.CheckYourAnswers)]
public class CheckYourAnswersController : Controller
{
public const string ViewPath = "~/Views/Onboarding/CheckYourAnswers.cshtml";
private readonly ISessionService _sessionService;

public CheckYourAnswersController(ISessionService sessionService)
{
_sessionService = sessionService;
}

[HttpGet]
public IActionResult Get()
{
var sessionModel = _sessionService.Get<OnboardingSessionModel>();
sessionModel.HasSeenPreview = true;
_sessionService.Set(sessionModel);

CheckYourAnswersViewModel model = new(Url, sessionModel);
return View(ViewPath, model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace SFA.DAS.Employer.Aan.Web.Controllers.Onboarding;

[Route("onboarding/previous-engagement", Name = RouteNames.Onboarding.PreviousEngagement)]
[Route("onboarding/checkyouranswers", Name = RouteNames.Onboarding.CheckYourAnswers)]
public class PreviousEngagementController : Controller
{
public const string ViewPath = "~/Views/Onboarding/PreviousEngagement.cshtml";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private async Task<RegionsViewModel> GetViewModel(CancellationToken cancellation

return new RegionsViewModel
{
BackLink = Url.RouteUrl(@RouteNames.Onboarding.TermsAndConditions)!,
BackLink = sessionModel.HasSeenPreview ? Url.RouteUrl(@RouteNames.Onboarding.CheckYourAnswers)! : Url.RouteUrl(@RouteNames.Onboarding.TermsAndConditions)!,
Regions = sessionModel.Regions
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Mvc;
using SFA.DAS.Employer.Aan.Web.Infrastructure;

namespace SFA.DAS.Employer.Aan.Web.Models.Onboarding;

public class CheckYourAnswersViewModel
{
public string RegionChangeLink { get; }
public List<string>? Region { get; }

public CheckYourAnswersViewModel(IUrlHelper url, OnboardingSessionModel sessionModel)
{
RegionChangeLink = url.RouteUrl(@RouteNames.Onboarding.Regions)!;
Region = GetRegions(sessionModel);
}

private static List<string>? GetRegions(OnboardingSessionModel sessionModel)
{
var locallyPreferredRegion = sessionModel.Regions.Find(x => x.IsConfirmed);
var regions = sessionModel.Regions.Where(x => x.IsSelected).Select(x => x.Area).ToList()!;

if (locallyPreferredRegion != null && sessionModel.Regions.Count(x => x.IsSelected) > 1)
{
regions.Add($"Locally prefers {locallyPreferredRegion.Area}");
}
return regions!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class OnboardingSessionModel
{
public bool HasSeenPreview { get; set; }
public List<ProfileModel> ProfileData { get; set; } = new List<ProfileModel>();
public bool HasAcceptedTerms { get; set; } = false;
public bool IsValid => HasAcceptedTerms && ProfileData.Count > 0;
Expand Down
Loading