From 14fda005fb3eb03009ebf5ed5fbabc94b7968d1f Mon Sep 17 00:00:00 2001 From: gilaineyo Date: Wed, 4 Dec 2024 11:34:09 +0000 Subject: [PATCH] test: adds test for Unknown HeaderSize --- .../TagHelpers/HeaderTagHelperTests.cs | 215 ++++-------------- 1 file changed, 48 insertions(+), 167 deletions(-) diff --git a/tests/Dfe.PlanTech.Web.UnitTests/TagHelpers/HeaderTagHelperTests.cs b/tests/Dfe.PlanTech.Web.UnitTests/TagHelpers/HeaderTagHelperTests.cs index 0849558cf..0ee9cff7f 100644 --- a/tests/Dfe.PlanTech.Web.UnitTests/TagHelpers/HeaderTagHelperTests.cs +++ b/tests/Dfe.PlanTech.Web.UnitTests/TagHelpers/HeaderTagHelperTests.cs @@ -1,3 +1,4 @@ +using System.ComponentModel; using Dfe.PlanTech.Domain.Content.Enums; using Dfe.PlanTech.Domain.Content.Models; using Dfe.PlanTech.Web.Helpers; @@ -11,6 +12,19 @@ namespace Dfe.PlanTech.Web.UnitTests.TagHelpers; public class HeaderComponentTagHelperTests { + private ILogger _loggerSubstitute; + private HeaderComponentTagHelper? _tagHelper; + private TagHelperContext _context; + private TagHelperOutput _output; + + public HeaderComponentTagHelperTests() + { + _loggerSubstitute = Substitute.For>(); + _context = CreateTagHelperContext(); + _output = CreateTagHelperOutput(); + } + + private TagHelperContext CreateTagHelperContext() { return new TagHelperContext( @@ -33,226 +47,93 @@ private TagHelperOutput CreateTagHelperOutput() }); } - [Fact] - public void Should_RenderH1Tag_When_H1() - { - var header = new Header() - { - Text = "Header text", - Tag = HeaderTag.H1, - Size = HeaderSize.Small - }; - - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) - { - Model = header - }; - - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); - - tagHelper.Process(context, output); - - Assert.Equal("h1", output.TagName); - Assert.Contains(header.Text, output.Content.GetContent()); - } - - [Fact] - public void Should_RenderH2Tag_When_H2() + [Theory] + [InlineData(HeaderTag.H1, "h1", HeaderSize.Small)] + [InlineData(HeaderTag.H2, "h2", HeaderSize.Small)] + [InlineData(HeaderTag.H3, "h3", HeaderSize.Small)] + [InlineData(HeaderTag.H1, "h1", HeaderSize.Medium)] + [InlineData(HeaderTag.H1, "h1", HeaderSize.Large)] + [InlineData(HeaderTag.H1, "h1", HeaderSize.ExtraLarge)] + public void Should_Render_Valid_Tag_And_Class(HeaderTag headerTag, string expectedTag, HeaderSize headerSize) { var header = new Header() { Text = "Header text", - Tag = HeaderTag.H2, - Size = HeaderSize.Small + Tag = headerTag, + Size = headerSize }; - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) + _tagHelper = new HeaderComponentTagHelper(_loggerSubstitute) { Model = header }; - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); - - tagHelper.Process(context, output); - - Assert.Equal("h2", output.TagName); - Assert.Contains(header.Text, output.Content.GetContent()); - } - - [Fact] - public void Should_RenderH3Tag_When_H3() - { - var header = new Header() - { - Text = "Header text", - Tag = HeaderTag.H3, - Size = HeaderSize.Small - }; - - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) - { - Model = header - }; - - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); - - tagHelper.Process(context, output); - - Assert.Equal("h3", output.TagName); - Assert.Contains(header.Text, output.Content.GetContent()); - } - - [Fact] - public void Should_RenderRightClass_When_SmallSize() - { - var header = new Header() - { - Text = "Header text", - Tag = HeaderTag.H1, - Size = HeaderSize.Small - }; - - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) - { - Model = header - }; - - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); - - tagHelper.Process(context, output); + _tagHelper.Process(_context, _output); var expectedClass = header.GetClassForSize(); - Assert.Contains($"{expectedClass}", output.Attributes["class"].Value.ToString()); + Assert.Equal(expectedTag, _output.TagName); + Assert.Contains(header.Text, _output.Content.GetContent()); + Assert.Contains($"{expectedClass}", _output.Attributes["class"].Value.ToString()); } [Fact] - public void Should_RenderRightClass_When_MediumSize() - { - var header = new Header() - { - Text = "Header text", - Tag = HeaderTag.H1, - Size = HeaderSize.Medium - }; - - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) - { - Model = header - }; - - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); - - tagHelper.Process(context, output); - - var expectedClass = header.GetClassForSize(); - - Assert.Contains($"{expectedClass}", output.Attributes["class"].Value.ToString()); - } - - [Fact] - public void Should_RenderRightClass_When_LargeSize() + public async Task Should_LogWarning_When_Model_Is_Null() { - var header = new Header() - { - Text = "Header text", - Tag = HeaderTag.H1, - Size = HeaderSize.Large - }; - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) - { - Model = header - }; + var tagHelper = new HeaderComponentTagHelper(loggerSubstitute); var context = CreateTagHelperContext(); var output = CreateTagHelperOutput(); - tagHelper.Process(context, output); - - var expectedClass = header.GetClassForSize(); + await tagHelper.ProcessAsync(context, output); - Assert.Contains($"{expectedClass}", output.Attributes["class"].Value.ToString()); + var logMessage = loggerSubstitute.ReceivedLogMessages().FirstOrDefault(); + Assert.NotNull(logMessage?.Message); + Assert.Contains($"Missing or invalid Header {tagHelper.Model}", logMessage.Message); + Assert.Equal(LogLevel.Warning, logMessage.LogLevel); } [Fact] - public void Should_RenderRightClass_When_ExtraLarge() + public async Task Should_LogWarning_When_HeaderTag_Is_Unknown() { var header = new Header() { Text = "Header text", - Tag = HeaderTag.H1, - Size = HeaderSize.ExtraLarge + Tag = HeaderTag.Unknown }; - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) + _tagHelper = new HeaderComponentTagHelper(_loggerSubstitute) { Model = header }; - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); - - tagHelper.Process(context, output); - - var expectedClass = header.GetClassForSize(); - - Assert.Contains($"{expectedClass}", output.Attributes["class"].Value.ToString()); - } + await _tagHelper.ProcessAsync(_context, _output); - [Fact] - public async Task Should_LogWarning_When_Model_Is_Null() - { - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute); - - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); + var logMessage = _loggerSubstitute.ReceivedLogMessages().FirstOrDefault(); - await tagHelper.ProcessAsync(context, output); - - var logMessage = loggerSubstitute.ReceivedLogMessages().FirstOrDefault(); Assert.NotNull(logMessage?.Message); - Assert.Contains($"Missing or invalid Header {tagHelper.Model}", logMessage.Message); + Assert.Contains($"Missing or invalid Header {_tagHelper.Model}", logMessage.Message); Assert.Equal(LogLevel.Warning, logMessage.LogLevel); } [Fact] - public async Task Should_LogWarning_When_HeaderTag_Is_Unknown() + public async Task Should_Throw_Exception_When_HeaderSize_Is_Unknown() { var header = new Header() { Text = "Header text", - Tag = HeaderTag.Unknown + Tag = HeaderTag.H1, + Size = HeaderSize.Unknown }; - var loggerSubstitute = Substitute.For>(); - var tagHelper = new HeaderComponentTagHelper(loggerSubstitute) + _tagHelper = new HeaderComponentTagHelper(_loggerSubstitute) { Model = header }; - var context = CreateTagHelperContext(); - var output = CreateTagHelperOutput(); - - await tagHelper.ProcessAsync(context, output); - - var logMessage = loggerSubstitute.ReceivedLogMessages().FirstOrDefault(); + var exception = await Assert.ThrowsAsync(async () => await _tagHelper.ProcessAsync(_context, _output)); - Assert.NotNull(logMessage?.Message); - Assert.Contains($"Missing or invalid Header {tagHelper.Model}", logMessage.Message); - Assert.Equal(LogLevel.Warning, logMessage.LogLevel); + Assert.Equal($"Could not find header size for {header.Size}", exception.Message); } }