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

Added additional unit tests #300

Merged
merged 1 commit into from
Aug 8, 2024
Merged
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
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>");
}
}
Loading