From 5c7e4134544c19bba6d553770f81ee716c58d6d8 Mon Sep 17 00:00:00 2001 From: Sam Carter Date: Thu, 8 Aug 2024 11:31:12 +0100 Subject: [PATCH] Added additional unit tests --- .../Controllers/AdviceControllerTests.cs | 118 ++++++++++++++++++ .../Renderers/NestedContentHelperTests.cs | 44 +++++++ 2 files changed, 162 insertions(+) diff --git a/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/AdviceControllerTests.cs b/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/AdviceControllerTests.cs index 3f27431a..66db701c 100644 --- a/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/AdviceControllerTests.cs +++ b/tests/Dfe.EarlyYearsQualification.UnitTests/Controllers/AdviceControllerTests.cs @@ -417,4 +417,122 @@ public async Task QualificationLevel7_ContentServiceReturnsAdvicePage_ReturnsAdv mockHtmlRenderer.Verify(x => x.ToHtml(It.IsAny()), Times.Once); } + + [TestMethod] + public async Task Level6QualificationPre2014_ContentServiceReturnsNoAdvicePage_RedirectsToErrorPage() + { + var mockLogger = new Mock>(); + var mockContentService = new Mock(); + var mockHtmlRenderer = new Mock(); + + 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>(); + var mockContentService = new Mock(); + var mockHtmlRenderer = new Mock(); + + 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())).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()), Times.Once); + } + + [TestMethod] + public async Task Level6QualificationPost2014_ContentServiceReturnsNoAdvicePage_RedirectsToErrorPage() + { + var mockLogger = new Mock>(); + var mockContentService = new Mock(); + var mockHtmlRenderer = new Mock(); + + 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>(); + var mockContentService = new Mock(); + var mockHtmlRenderer = new Mock(); + + 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())).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()), Times.Once); + } } \ No newline at end of file diff --git a/tests/Dfe.EarlyYearsQualification.UnitTests/Renderers/NestedContentHelperTests.cs b/tests/Dfe.EarlyYearsQualification.UnitTests/Renderers/NestedContentHelperTests.cs index 2abefd3a..e9c9f4d2 100644 --- a/tests/Dfe.EarlyYearsQualification.UnitTests/Renderers/NestedContentHelperTests.cs +++ b/tests/Dfe.EarlyYearsQualification.UnitTests/Renderers/NestedContentHelperTests.cs @@ -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; @@ -89,4 +91,46 @@ public void ContentHelper_RenderCollection() output.Should() .Be("

Text

Hyperlink ContentSome 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 { entryStructure }; + + var output = NestedContentHelper.Render(content).Result; + + output.Should().Be($"Display text"); + } } \ No newline at end of file