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 Aug 8, 2024
1 parent 74c65af commit 5c7e413
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,122 @@ public async Task QualificationLevel7_ContentServiceReturnsAdvicePage_ReturnsAdv

mockHtmlRenderer.Verify(x => x.ToHtml(It.IsAny<Document>()), Times.Once);
}

[TestMethod]
public async Task Level6QualificationPre2014_ContentServiceReturnsNoAdvicePage_RedirectsToErrorPage()
{
var mockLogger = new Mock<ILogger<AdviceController>>();
var mockContentService = new Mock<IContentService>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();

var controller = new AdviceController(mockLogger.Object, mockContentService.Object, mockHtmlRenderer.Object);

mockContentService.Setup(x => x.GetAdvicePage(AdvicePages.Level6QualificationPre2014))
.ReturnsAsync((AdvicePage?)default).Verifiable();

var result = await controller.Level6QualificationPre2014();

result.Should().NotBeNull();

var resultType = result as RedirectToActionResult;

resultType.Should().NotBeNull();

resultType!.ActionName.Should().Be("Index");
resultType.ControllerName.Should().Be("Error");

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

[TestMethod]
public async Task Level6QualificationPre2014_ContentServiceReturnsAdvicePage_ReturnsAdvicePageModel()
{
var mockLogger = new Mock<ILogger<AdviceController>>();
var mockContentService = new Mock<IContentService>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();

var controller = new AdviceController(mockLogger.Object, mockContentService.Object, mockHtmlRenderer.Object);

var renderedHtmlBody = "Test html body (level 6 pre 2014)";

var advicePage = new AdvicePage { Heading = "Heading (level 6 pre 2014)", Body = ContentfulContentHelper.Text("Anything") };
mockContentService.Setup(x => x.GetAdvicePage(AdvicePages.Level6QualificationPre2014))
.ReturnsAsync(advicePage);

mockHtmlRenderer.Setup(x => x.ToHtml(It.IsAny<Document>())).ReturnsAsync(renderedHtmlBody);

var result = await controller.Level6QualificationPre2014();

result.Should().NotBeNull();

var resultType = result as ViewResult;
resultType.Should().NotBeNull();

var model = resultType!.Model as AdvicePageModel;
model.Should().NotBeNull();

model!.Heading.Should().Be(advicePage.Heading);
model.BodyContent.Should().Be(renderedHtmlBody);

mockHtmlRenderer.Verify(x => x.ToHtml(It.IsAny<Document>()), Times.Once);
}

[TestMethod]
public async Task Level6QualificationPost2014_ContentServiceReturnsNoAdvicePage_RedirectsToErrorPage()
{
var mockLogger = new Mock<ILogger<AdviceController>>();
var mockContentService = new Mock<IContentService>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();

var controller = new AdviceController(mockLogger.Object, mockContentService.Object, mockHtmlRenderer.Object);

mockContentService.Setup(x => x.GetAdvicePage(AdvicePages.Level6QualificationPost2014))
.ReturnsAsync((AdvicePage?)default).Verifiable();

var result = await controller.Level6QualificationPost2014();

result.Should().NotBeNull();

var resultType = result as RedirectToActionResult;

resultType.Should().NotBeNull();

resultType!.ActionName.Should().Be("Index");
resultType.ControllerName.Should().Be("Error");

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

[TestMethod]
public async Task Level6QualificationPost2014_ContentServiceReturnsAdvicePage_ReturnsAdvicePageModel()
{
var mockLogger = new Mock<ILogger<AdviceController>>();
var mockContentService = new Mock<IContentService>();
var mockHtmlRenderer = new Mock<IHtmlRenderer>();

var controller = new AdviceController(mockLogger.Object, mockContentService.Object, mockHtmlRenderer.Object);

var renderedHtmlBody = "Test html body (level 6 post 2014)";

var advicePage = new AdvicePage { Heading = "Heading (level 6 post 2014)", Body = ContentfulContentHelper.Text("Anything") };
mockContentService.Setup(x => x.GetAdvicePage(AdvicePages.Level6QualificationPost2014))
.ReturnsAsync(advicePage);

mockHtmlRenderer.Setup(x => x.ToHtml(It.IsAny<Document>())).ReturnsAsync(renderedHtmlBody);

var result = await controller.Level6QualificationPost2014();

result.Should().NotBeNull();

var resultType = result as ViewResult;
resultType.Should().NotBeNull();

var model = resultType!.Model as AdvicePageModel;
model.Should().NotBeNull();

model!.Heading.Should().Be(advicePage.Heading);
model.BodyContent.Should().Be(renderedHtmlBody);

mockHtmlRenderer.Verify(x => x.ToHtml(It.IsAny<Document>()), Times.Once);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Contentful.Core.Models;
using Dfe.EarlyYearsQualification.Content.Entities;
using Dfe.EarlyYearsQualification.Content.Renderers.Helpers;
using FluentAssertions;
using Newtonsoft.Json.Linq;

namespace Dfe.EarlyYearsQualification.UnitTests.Renderers;

Expand Down Expand Up @@ -89,4 +91,46 @@ public void ContentHelper_RenderCollection()
output.Should()
.Be("<p class=\"govuk-body\">Text</p><a href='https://my.uri' class='govuk-link'>Hyperlink Content</a>Some text");
}

[TestMethod]
public void ContentHelper_RenderExternalNavigationLink()
{
var navigationLink = new NavigationLink
{
Sys =
{
ContentType = new ContentType
{
SystemProperties = new SystemProperties
{
Id = "externalNavigationLink"
}
}
},
Href = "/",
DisplayText = "Display text",
OpenInNewTab = true
};

var jObject = JObject.FromObject(navigationLink);

var customNode = new CustomNode
{
JObject = jObject
};

var entryStructure = new EntryStructure
{
Data = new EntryStructureData
{
Target = customNode
}
};

var content = new List<IContent> { entryStructure };

var output = NestedContentHelper.Render(content).Result;

output.Should().Be($"<a href='/' target='_blank' class='govuk-link'>Display text</a>");
}
}

0 comments on commit 5c7e413

Please sign in to comment.