Skip to content

Commit

Permalink
Merge pull request #246 from DFE-Digital/Feature/support-inline-conte…
Browse files Browse the repository at this point in the history
…nt-links

feat: add support for inline ContentLink rendering in Rich Text Blocks
  • Loading branch information
nnagepat authored Sep 4, 2023
2 parents 16ba4eb + be1ad9a commit 18282ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using NSubstitute;
using NUnit.Framework;
using System.IO;
using System;
using ParagraphRenderer = Childrens_Social_Care_CPD.Contentful.Renderers.ParagraphRenderer;
using FluentAssertions;
using System.Collections.Generic;
Expand All @@ -18,6 +17,7 @@ public class ParagraphRendererTests
private IRenderer<Text> _textRenderer;
private IRenderer<RoleList> _roleListRenderer;
private IRenderer<Hyperlink> _hyperlinkRenderer;
private IRenderer<ContentLink> _contentLinkRenderer;

private ParagraphRenderer _sut;

Expand All @@ -27,8 +27,9 @@ public void Setup()
_textRenderer = Substitute.For<IRenderer<Text>>();
_roleListRenderer = Substitute.For<IRenderer<RoleList>>();
_hyperlinkRenderer = Substitute.For<IRenderer<Hyperlink>>();
_contentLinkRenderer = Substitute.For<IRenderer<ContentLink>>();

_sut = new ParagraphRenderer(_textRenderer, _roleListRenderer, _hyperlinkRenderer);
_sut = new ParagraphRenderer(_textRenderer, _roleListRenderer, _hyperlinkRenderer, _contentLinkRenderer);
}

[Test]
Expand All @@ -47,6 +48,7 @@ public void Paragraph_Renders_With_Text()
_textRenderer.Render(Arg.Any<Text>()).Returns(new HtmlString("AAA"));
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));

// act
var htmlContent = _sut.Render(paragraph);
Expand Down Expand Up @@ -79,6 +81,7 @@ public void Paragraph_Renders_With_RoleList()
_textRenderer.Render(Arg.Any<Text>()).Returns(new HtmlString("AAA"));
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));

// act
var htmlContent = _sut.Render(paragraph);
Expand All @@ -105,6 +108,7 @@ public void Paragraph_Renders_With_Hyperlink()
_textRenderer.Render(Arg.Any<Text>()).Returns(new HtmlString("AAA"));
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));

// act
var htmlContent = _sut.Render(paragraph);
Expand All @@ -114,4 +118,37 @@ public void Paragraph_Renders_With_Hyperlink()
// assert
actual.Should().Be($"<p class=\"HtmlEncode[[govuk-body-m]]\">CCC</p>");
}

[Test]
public void Paragraph_Renders_With_ContentLink()
{
// arrange
var stringWriter = new StringWriter();
var paragraph = new Paragraph()
{
Content = new List<IContent>
{
new EntryStructure
{
Data = new EntryStructureData
{
Target = new ContentLink()
}
}
}
};

_textRenderer.Render(Arg.Any<Text>()).Returns(new HtmlString("AAA"));
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));

// act
var htmlContent = _sut.Render(paragraph);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().Be($"<p class=\"HtmlEncode[[govuk-body-m]]\">DDD</p>");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ internal class ParagraphRenderer : IRenderer<Paragraph>
private readonly IRenderer<Text> _textRenderer;
private readonly IRenderer<RoleList> _roleListRenderer;
private readonly IRenderer<Hyperlink> _hyperlinkRenderer;
private readonly IRenderer<ContentLink> _contentLinkRenderer;


public ParagraphRenderer(IRenderer<Text> textRenderer, IRenderer<RoleList> roleListRenderer, IRenderer<Hyperlink> hyperlinkRenderer)
public ParagraphRenderer(IRenderer<Text> textRenderer, IRenderer<RoleList> roleListRenderer, IRenderer<Hyperlink> hyperlinkRenderer, IRenderer<ContentLink> contentLinkRenderer)
{
_textRenderer = textRenderer;
_roleListRenderer = roleListRenderer;
_hyperlinkRenderer = hyperlinkRenderer;
_contentLinkRenderer = contentLinkRenderer;
}

public IHtmlContent Render(Paragraph item)
Expand All @@ -32,6 +35,7 @@ public IHtmlContent Render(Paragraph item)
{
switch (entryStructure.Data.Target)
{
case ContentLink contentLink: p.InnerHtml.AppendHtml(_contentLinkRenderer.Render(contentLink)); break;
case RoleList roleList: p.InnerHtml.AppendHtml(_roleListRenderer.Render(roleList)); break;
}
break;
Expand Down

0 comments on commit 18282ec

Please sign in to comment.