Skip to content

Commit

Permalink
Merge pull request #226 from DFE-Digital/chore/renderer-test
Browse files Browse the repository at this point in the history
New unit test for InsetTextRenderer
  • Loading branch information
RobertGHippo authored Jul 3, 2024
2 parents cdb0510 + 80b3014 commit 35b88f9
Showing 1 changed file with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Contentful.Core;
using Contentful.Core.Configuration;
using Contentful.Core.Models;
using Dfe.EarlyYearsQualification.Content.Entities;
using Dfe.EarlyYearsQualification.Content.Renderers.GovUk;
using FluentAssertions;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Dfe.EarlyYearsQualification.UnitTests.Renderers;
Expand Down Expand Up @@ -58,7 +60,7 @@ public void InsetTextRenderer_DataTargetIsEmptyCustomNode_NotSupported()
}

[TestMethod]
public void InsetTextRenderer_DataTargetIsCustomNodeWithJObject_OtherId_Supported()
public void InsetTextRenderer_DataTargetIsCustomNodeWithJObject_OtherId_NotSupported()
{
var contentClientMock = new Mock<IContentfulClient>();

Expand Down Expand Up @@ -95,7 +97,7 @@ public void InsetTextRenderer_DataTargetIsCustomNodeWithJObject_OtherId_Supporte
}

[TestMethod]
public void InsetTextRenderer_DataTargetIsCustomNodeWithJObject_Supported()
public void InsetTextRenderer_DataTargetIsCustomNodeWithJObject_IsSupported()
{
var contentClientMock = new Mock<IContentfulClient>();

Expand Down Expand Up @@ -150,4 +152,70 @@ public void InsetTextRenderer_DoesNotSupportHyperlink()

new InsetTextRenderer(contentClientMock.Object).SupportsContent(list).Should().BeFalse();
}

[TestMethod]
public async Task InsetTextRenderer_RendersDivWithId()
{
var innerDocument = new Document { Content = [] };
// ...we haven't worked out yet how to test this _with_ inner content,
// there is some Contentful SDK inner magic involved that is hard to replicate.

var govUkInsetTextModel = new GovUkInsetTextModel
{
Content = JObject.FromObject(innerDocument),
Sys =
{
ContentType = new ContentType
{
SystemProperties = new SystemProperties
{
Id = "govUkInsetText"
}
}
}
};

var jObject = JObject.FromObject(govUkInsetTextModel);

var customNode = new CustomNode
{
JObject = jObject
};

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

var renderer = new InsetTextRenderer(CreateMockContentfulContentClient());

var result = await renderer.RenderAsync(content);

result.Should().Be("<div class=\"govuk-inset-text\"></div>");
}

private static IContentfulClient CreateMockContentfulContentClient()
{
var contentClientMock = new Mock<IContentfulClient>();

var serializerSettings = new JsonSerializerSettings
{
Converters =
[
new AssetJsonConverter(),
new ContentJsonConverter()
],
TypeNameHandling = TypeNameHandling.All
}; // This is what a real ContentfulClient would have

contentClientMock.SetupGet(c => c.SerializerSettings)
.Returns(serializerSettings);

contentClientMock.SetupGet(c => c.ResolveEntriesSelectively).Returns(true);

return contentClientMock.Object;
}
}

0 comments on commit 35b88f9

Please sign in to comment.