Skip to content

Commit

Permalink
Added additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-c-dfe committed Jul 9, 2024
1 parent d8c22b1 commit 93d2f64
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<IActionResult> Get()
[HttpPost]
public IActionResult Refine(string refineSearch)
{
userJourneyCookieService.SetNameSearchCriteria(refineSearch);
userJourneyCookieService.SetQualificationNameSearchCriteria(refineSearch);

return RedirectToAction("Get");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IUserJourneyCookieService
public void SetLevelOfQualification(string level);
public void SetAwardingOrganisation(string awardingOrganisation);

public void SetNameSearchCriteria(string searchCriteria);
public void SetQualificationNameSearchCriteria(string searchCriteria);
public UserJourneyModel GetUserJourneyModelFromCookie();
public void ResetUserJourneyCookie();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void SetAwardingOrganisation(string awardingOrganisation)
SetJourneyCookie(model);
}

public void SetNameSearchCriteria(string searchCriteria)
public void SetQualificationNameSearchCriteria(string searchCriteria)
{
var model = GetUserJourneyModelFromCookie();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,34 @@ public async Task Get_NoContent_LogsAndRedirectsToError()

mockLogger.VerifyError("No content for the qualification list page");
}

[TestMethod]
public void Refine_SaveQualificationName_RedirectsToGet()
{
var mockLogger = new Mock<ILogger<QualificationDetailsController>>();
var mockContentService = new Mock<IContentService>();
var mockContentFilterService = new Mock<IContentFilterService>();
var mockInsetTextRenderer = new Mock<IGovUkInsetTextRenderer>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();
var mockUserJourneyCookieService = new Mock<IUserJourneyCookieService>();

var controller =
new QualificationDetailsController(mockLogger.Object, mockContentService.Object, mockContentFilterService.Object, mockInsetTextRenderer.Object,
mockHtmlRenderer.Object, mockUserJourneyCookieService.Object)
{
ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext()
}
};

var result = controller.Refine("Test");

result.Should().BeOfType<RedirectToActionResult>();

var actionResult = (RedirectToActionResult)result;

actionResult.ActionName.Should().Be("Get");
mockUserJourneyCookieService.Verify(x => x.SetQualificationNameSearchCriteria("Test"), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void GetWhenWasQualificationAwarded_CookieValueIsEmpty_ReturnsNull()

var service = new UserJourneyCookieService(mockHttpContextAccessor.Object, mockLogger.Object);

(int? startMonth, int? startYear) = service.GetWhenWasQualificationAwarded();
(int? startMonth, int? startYear) = service.GetWhenWasQualificationAwarded();

startMonth.Should().BeNull();
startYear.Should().BeNull();
Expand All @@ -311,7 +311,7 @@ public void GetWhenWasQualificationAwarded_CookieHasInvalidValue_ReturnsNull()

var service = new UserJourneyCookieService(mockHttpContextAccessor.Object, mockLogger.Object);

(int? startMonth, int? startYear) = service.GetWhenWasQualificationAwarded();
(int? startMonth, int? startYear) = service.GetWhenWasQualificationAwarded();

startMonth.Should().BeNull();
startYear.Should().BeNull();
Expand All @@ -329,11 +329,65 @@ public void GetWhenWasQualificationAwarded_CookieHasValidValue_ReturnsValue()

var service = new UserJourneyCookieService(mockHttpContextAccessor.Object, mockLogger.Object);

(int? startMonth, int? startYear) = service.GetWhenWasQualificationAwarded();
(int? startMonth, int? startYear) = service.GetWhenWasQualificationAwarded();

startMonth.Should().Be(4);
startYear.Should().Be(2015);
}

[TestMethod]
public void SetNameSearchCriteria_StringProvided_SetsCookieCorrectly()
{
var modelInCookie = new UserJourneyModel();
var mockHttpContextAccessor = SetHttpContextWithExistingCookie(modelInCookie);
var mockLogger = new Mock<ILogger<UserJourneyCookieService>>();

var service = new UserJourneyCookieService(mockHttpContextAccessor.Object, mockLogger.Object);

var searchCriteria = "This is a test";
service.SetQualificationNameSearchCriteria(searchCriteria);

var serialisedModelToCheck = JsonSerializer.Serialize(new UserJourneyModel
{
SearchCriteria = searchCriteria
});

CheckSerializedModelWasSet(mockHttpContextAccessor, serialisedModelToCheck);
}

[TestMethod]
public void GetSearchCriteria_CookieHasInvalidValue_ReturnsNull()
{
var existingModel = new UserJourneyModel
{
SearchCriteria = ""
};
var mockHttpContextAccessor = SetHttpContextWithExistingCookie(existingModel);
var mockLogger = new Mock<ILogger<UserJourneyCookieService>>();

var service = new UserJourneyCookieService(mockHttpContextAccessor.Object, mockLogger.Object);

var searchCriteria = service.GetSearchCriteria();

searchCriteria.Should().BeNull();
}

[TestMethod]
public void GetSearchCriteria_CookieHasValidValue_ReturnsValue()
{
var existingModel = new UserJourneyModel
{
SearchCriteria = "Test"
};
var mockHttpContextAccessor = SetHttpContextWithExistingCookie(existingModel);
var mockLogger = new Mock<ILogger<UserJourneyCookieService>>();

var service = new UserJourneyCookieService(mockHttpContextAccessor.Object, mockLogger.Object);

var searchCriteria = service.GetSearchCriteria();

searchCriteria.Should().Be("Test");
}

private static Mock<IHttpContextAccessor> SetHttpContextWithExistingCookie(object? model)
{
Expand Down

0 comments on commit 93d2f64

Please sign in to comment.