using CTA.WebForms.Helpers.TagConversion; using CTA.WebForms.Services; using CTA.WebForms.TagConverters; using CTA.WebForms.TagConverters.TagTemplateConditions; using CTA.WebForms.TagConverters.TagTemplateInvokables; using HtmlAgilityPack; using NUnit.Framework; using System.Collections.Generic; using System.Threading.Tasks; namespace CTA.WebForms.Tests.TagConverters { public class TemplateTagConverterTests { [Test] public void Validate_Does_Not_Throw_Exception_When_Configuration_Is_Valid() { var templateConverter = new TemplateTagConverter() { TagName = "TestTag", CodeBehindHandler = "Default", Conditions = new[] { new HasAttributeTemplateCondition() { AttributeName = "Attr0" } }, Invocations = new[] { new AddUsingDirectiveTemplateInvokable() { NamespaceName = "Namespace0" } }, Templates = new Dictionary { { "Default", "

#Attr0#

" } } }; Assert.DoesNotThrow(() => templateConverter.Validate()); } [Test] public void Validate_Does_Not_Throw_Exception_When_Optional_Properties_Not_Set() { var templateConverter = new TemplateTagConverter() { TagName = "TestTag", Templates = new Dictionary { { "Default", "

#Attr0#

" } } }; Assert.DoesNotThrow(() => templateConverter.Validate()); } [Test] public void Validate_Throws_Exception_When_TagName_Not_Set() { var templateConverter = new TemplateTagConverter() { Templates = new Dictionary { { "Default", "

#Attr0#

" } } }; Assert.Throws(typeof(ConfigValidationException), () => templateConverter.Validate()); } [Test] public void Validate_Throws_Exception_When_TagName_Is_Empty() { var templateConverter = new TemplateTagConverter() { TagName = string.Empty, Templates = new Dictionary { { "Default", "

#Attr0#

" } } }; Assert.Throws(typeof(ConfigValidationException), () => templateConverter.Validate()); } [Test] public void Validate_Throws_Exception_When_CodeBehindHandler_Not_Valid() { var templateConverter = new TemplateTagConverter() { TagName = "TestTag", CodeBehindHandler = "NonExistentType", }; Assert.Throws(typeof(ConfigValidationException), () => templateConverter.Validate()); } [Test] public void Validate_Throws_Exception_When_Some_Conditions_Not_Valid() { var templateConverter = new TemplateTagConverter() { TagName = "TestTag", CodeBehindHandler = "Default", Conditions = new[] { new HasAttributeTemplateCondition() }, Invocations = new[] { new AddUsingDirectiveTemplateInvokable() { NamespaceName = "Namespace0" } }, Templates = new Dictionary { { "Default", "

#Attr0#

" } } }; Assert.Throws(typeof(ConfigValidationException), () => templateConverter.Validate()); } [Test] public void Validate_Throws_Exception_When_Some_Invocations_Not_Valid() { var templateConverter = new TemplateTagConverter() { TagName = "TestTag", CodeBehindHandler = "Default", Conditions = new[] { new HasAttributeTemplateCondition() { AttributeName = "Attr0" } }, Invocations = new[] { new AddUsingDirectiveTemplateInvokable() }, Templates = new Dictionary { { "Default", "

#Attr0#

" } } }; Assert.Throws(typeof(ConfigValidationException), () => templateConverter.Validate()); } [Test] public async Task MigrateTag_Replaces_Node_Using_Correct_Template() { var expectedInnerHtml = "

https://aws.amazon.com

"; var parent = HtmlNode.CreateNode("
"); var node = HtmlNode.CreateNode(" { { "Other0", "

#Attr0#

" }, { "Other1", "

#href#

" }, { "Default", "This is a placeholder for your link..." } } }; templateConverter.Initialize(new TaskManagerService(), new CodeBehindReferenceLinkerService(), new ViewImportService()); await templateConverter.MigrateTagAsync(node, "testpath", null, 0); Assert.AreEqual(expectedInnerHtml, parent.InnerHtml); } [Test] public async Task MigrateTag_Does_Nothing_When_No_Suitable_Template_Is_Found() { var expectedInnerHtml = ""; var parent = HtmlNode.CreateNode("
"); var node = HtmlNode.CreateNode(expectedInnerHtml); parent.AppendChild(node); var templateConverter = new TemplateTagConverter() { TagName = "TestTag", Conditions = new[] { new HasAttributeTemplateCondition() { AttributeName = "Attr0", ForTemplates = new[] { "Default" } } }, Templates = new Dictionary { { "Default", "

#Attr0#

" } } }; templateConverter.Initialize(new TaskManagerService(), new CodeBehindReferenceLinkerService(), new ViewImportService()); await templateConverter.MigrateTagAsync(node, "testpath", null, 0); Assert.AreEqual(expectedInnerHtml, parent.InnerHtml); } } }