generated from DFE-Digital/govuk-dotnet-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added support for Areas of practice.
Merge pull request #264 from DFE-Digital/Feature/AreaOfPractic
- Loading branch information
Showing
13 changed files
with
474 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...drens-Social-Care-CPD-Tests/Contentful/Renderers/InlineAreaOfPracticeListRendererTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using Childrens_Social_Care_CPD.Contentful.Models; | ||
using Childrens_Social_Care_CPD.Contentful.Renderers; | ||
using Contentful.Core.Models; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.Extensions.WebEncoders.Testing; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Childrens_Social_Care_CPD_Tests.Contentful.Renderers; | ||
|
||
public class InlineAreaOfPracticeListRendererTests | ||
{ | ||
private IRenderer<ContentLink> _contentLinkRenderer; | ||
private InlineAreaOfPracticeListRenderer _sut; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_contentLinkRenderer = Substitute.For<IRenderer<ContentLink>>(); | ||
_sut = new InlineAreaOfPracticeListRenderer(_contentLinkRenderer); | ||
} | ||
|
||
[Test] | ||
public void AreaOfPractice_Renders_When_No_AreasOfPractice() | ||
{ | ||
// arrange | ||
var stringWriter = new StringWriter(); | ||
var areaOfPracticeList = new AreaOfPracticeList() | ||
{ | ||
Title = "A Title", | ||
Areas = new List<Content>() | ||
}; | ||
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA")); | ||
|
||
// act | ||
var htmlContent = _sut.Render(areaOfPracticeList); | ||
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder()); | ||
var actual = stringWriter.ToString(); | ||
|
||
// assert | ||
actual.Should().Be("<span>HtmlEncode[[No Areas Of practice Available]]</span>"); | ||
} | ||
|
||
[Test] | ||
public void AreaOfPracticeList_Renders_Empty_When_AreaOfPractice_Is_Null() | ||
{ | ||
// arrange | ||
var stringWriter = new StringWriter(); | ||
var areaOfPracticeList = new AreaOfPracticeList() | ||
{ | ||
Title = "A Title", | ||
Areas = new List<Content> | ||
{ | ||
new Content | ||
{ | ||
Id = "id", | ||
Title = "title", | ||
Items = new List<IContent>() | ||
} | ||
} | ||
}; | ||
|
||
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA")); | ||
|
||
// act | ||
var htmlContent = _sut.Render(areaOfPracticeList); | ||
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder()); | ||
var actual = stringWriter.ToString(); | ||
|
||
// assert | ||
actual.Should().Be(string.Empty); | ||
} | ||
|
||
[Test] | ||
public void AreaOfPracticeList_Renders_Title() | ||
{ | ||
// arrange | ||
var stringWriter = new StringWriter(); | ||
var areaOfPracticeList = new AreaOfPracticeList | ||
{ | ||
Title = "A Title", | ||
Areas = new List<Content> | ||
{ | ||
new Content | ||
{ | ||
Id = "id", | ||
Items = new List<IContent>() | ||
{ | ||
new AreaOfPractice() | ||
{ | ||
Title = "title" | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA")); | ||
var expected = $"^{Regex.Escape("<div class=\"HtmlEncode[[govuk-heading-s govuk-!-margin-bottom-1]]\"><h2>AAA</h2></div>")}.*"; | ||
|
||
// act | ||
var htmlContent = _sut.Render(areaOfPracticeList); | ||
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder()); | ||
var actual = stringWriter.ToString(); | ||
|
||
// assert | ||
actual.Should().MatchRegex(expected); | ||
} | ||
|
||
[Test] | ||
public void AreaOfPracticeList_Renders_Summary() | ||
{ | ||
// arrange | ||
var stringWriter = new StringWriter(); | ||
var areaOfPractice = new AreaOfPractice() | ||
{ | ||
Title = "title", | ||
AreaOfPracticeListSummary = "summary" | ||
}; | ||
var areaOfPracticeList = new AreaOfPracticeList() | ||
{ | ||
Title = "A Title", | ||
Areas = new List<Content> | ||
{ | ||
new Content | ||
{ | ||
Id = "id", | ||
Items = new List<IContent>() | ||
{ | ||
areaOfPractice | ||
} | ||
} | ||
} | ||
}; | ||
|
||
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("AAA")); | ||
var expected = $".*?{Regex.Escape("<p class=\"HtmlEncode[[govuk-body]]\">HtmlEncode[[summary]]</p>")}.*"; | ||
|
||
// act | ||
var htmlContent = _sut.Render(areaOfPracticeList); | ||
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder()); | ||
var actual = stringWriter.ToString(); | ||
|
||
// assert | ||
actual.Should().MatchRegex(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Childrens-Social-Care-CPD/Contentful/Models/AreaOfPractice.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Contentful.Core.Models; | ||
|
||
namespace Childrens_Social_Care_CPD.Contentful.Models; | ||
|
||
public class AreaOfPractice : IContent | ||
{ | ||
public string Title { get; set; } | ||
public string Summary { get; set; } | ||
public string AreaOfPracticeListSummary { get; set; } | ||
public string OtherNames { get; set; } | ||
public Document WhatYoullDo { get; set; } | ||
public Document SkillsAndKnowledge { get; set; } | ||
public Document WhoYouWillWorkWith { get; set; } | ||
public Document HowYouWillWork { get; set; } | ||
public Document CurrentOpportunities { get; set; } | ||
} |
9 changes: 9 additions & 0 deletions
9
Childrens-Social-Care-CPD/Contentful/Models/AreaOfPracticeList.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Contentful.Core.Models; | ||
|
||
namespace Childrens_Social_Care_CPD.Contentful.Models; | ||
|
||
public class AreaOfPracticeList : IContent | ||
{ | ||
public string Title { get; set; } | ||
public List<Content> Areas { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
Childrens-Social-Care-CPD/Contentful/Renderers/InlineAreaOfPracticeListRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Childrens_Social_Care_CPD.Contentful.Models; | ||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
|
||
namespace Childrens_Social_Care_CPD.Contentful.Renderers; | ||
|
||
internal class InlineAreaOfPracticeListRenderer : IRenderer<AreaOfPracticeList> | ||
{ | ||
private readonly IRenderer<ContentLink> _contentLinkRenderer; | ||
|
||
public InlineAreaOfPracticeListRenderer(IRenderer<ContentLink> contentLinkRenderer) | ||
{ | ||
_contentLinkRenderer = contentLinkRenderer; | ||
} | ||
|
||
public IHtmlContent Render(AreaOfPracticeList item) | ||
{ | ||
if (item.Areas.Count == 0) | ||
{ | ||
return NoAreasOfpractice(); | ||
} | ||
|
||
var htmlContentBuilder = new HtmlContentBuilder(); | ||
|
||
foreach (var contentItem in item.Areas) | ||
{ | ||
var areaOfpractice = contentItem.Items.OfType<AreaOfPractice>().FirstOrDefault(); | ||
if (areaOfpractice == null) continue; | ||
|
||
htmlContentBuilder.AppendHtml(AreaOfPracticeTitle(contentItem.Id, areaOfpractice)); | ||
htmlContentBuilder.AppendHtml(AreaOfPracticeSummary(areaOfpractice)); | ||
} | ||
|
||
return htmlContentBuilder; | ||
} | ||
|
||
private static IHtmlContent NoAreasOfpractice() | ||
{ | ||
var span = new TagBuilder("span"); | ||
span.InnerHtml.Append("No Areas Of practice Available"); | ||
return span; | ||
} | ||
|
||
private IHtmlContent AreaOfPracticeTitle(string id, AreaOfPractice areaOfPractice) | ||
{ | ||
var div = new TagBuilder("div"); | ||
div.AddCssClass("govuk-heading-s govuk-!-margin-bottom-1"); | ||
var heading2 = new TagBuilder("h2"); | ||
|
||
var contentLink = new ContentLink() | ||
{ | ||
Name = areaOfPractice.Title, | ||
Uri = id | ||
}; | ||
|
||
heading2.InnerHtml.AppendHtml(_contentLinkRenderer.Render(contentLink)); | ||
|
||
div.InnerHtml.AppendHtml(heading2); | ||
return div; | ||
} | ||
|
||
private static IHtmlContent AreaOfPracticeSummary(AreaOfPractice areaOfPractice) | ||
{ | ||
var htmlContentBuilder = new HtmlContentBuilder(); | ||
var p = new TagBuilder("p"); | ||
p.AddCssClass("govuk-body"); | ||
p.InnerHtml.Append(areaOfPractice.AreaOfPracticeListSummary); | ||
htmlContentBuilder.AppendHtml(p); | ||
return htmlContentBuilder; | ||
} | ||
} |
Oops, something went wrong.