Skip to content

Commit

Permalink
test(Outlines): initial passing test
Browse files Browse the repository at this point in the history
  • Loading branch information
seangwright committed May 8, 2023
1 parent 7277a2c commit 2ef3e81
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace XperienceCommunity.PreviewComponentOutlines;
public class OutlineTagHelper : TagHelper
{
public const string TAG_HELPER_ATTRIBUTE = "xpc-preview-outline";
public const string TAG_HELPER_OUTPUT_ATTRIBUTE = "data-xpc-preview-outline";

private readonly IHttpContextAccessor accessor;

Expand All @@ -38,6 +39,6 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
return;
}

output.Attributes.Add("data-xpc-preview-outline", ComponentName);
output.Attributes.Add(TAG_HELPER_OUTPUT_ATTRIBUTE, ComponentName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using FluentAssertions;
using Kentico.Content.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Razor.TagHelpers;
using NSubstitute;

namespace XperienceCommunity.PreviewComponentOutlines;

public class OutlineTagHelperTests
{

[Test]
public void The_Data_Attribute_Will_Be_Added_When_The_Request_Is_In_Preview_Mode()
{
string componentName = "My Test Widget";

var accessor = Substitute.For<IHttpContextAccessor>();
var httpContext = Substitute.For<HttpContext>();
httpContext.Items = new Dictionary<object, object?>
{
{ "Kentico.Features", InitializeFeatures(false, true) }
};
accessor.HttpContext.Returns(httpContext);

var tagHelperContext = new TagHelperContext("section", new(), new Dictionary<object, object>(), Guid.NewGuid().ToString());
var output = new TagHelperOutput("section", new(), (val, encoder) =>
{
return Task.FromResult<TagHelperContent>(new DefaultTagHelperContent());
});

var sut = new OutlineTagHelper(accessor);

sut.ComponentName = componentName;
sut.Process(tagHelperContext, output);

output.Attributes
.Where(a => string.Equals(a.Name, OutlineTagHelper.TAG_HELPER_ATTRIBUTE, StringComparison.OrdinalIgnoreCase))
.Should().BeEmpty();

output.Attributes
.Where(a =>
string.Equals(a.Name, OutlineTagHelper.TAG_HELPER_OUTPUT_ATTRIBUTE, StringComparison.OrdinalIgnoreCase)
&& string.Equals(a.Value.ToString(), componentName, StringComparison.OrdinalIgnoreCase))
.Should().HaveCount(1);
}

private IFeatureSet InitializeFeatures(bool isEditModeEnabled, bool isPreviewModeEnabled)
{
var previewFeature = Substitute.For<IPreviewFeature>();
previewFeature.Enabled.Returns(isPreviewModeEnabled);
var features = Substitute.For<IFeatureSet>();
features.GetFeature<IPreviewFeature>().Returns(previewFeature);
var pageBuilderFeature = Substitute.For<IPageBuilderFeature>();
pageBuilderFeature.EditMode.Returns(isEditModeEnabled);
features.GetFeature<IPageBuilderFeature>().Returns(pageBuilderFeature);

return features;
}
}
15 changes: 0 additions & 15 deletions test/XperienceCommunity.PreviewComponentOutlinesTests/UnitTest1.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Kentico.Xperience.Core.Tests" Version="25.0.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
<Using Include="NSubstitute" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
"resolved": "3.2.0",
"contentHash": "xjY8xBigSeWIYs4I7DgUHqSNoGqnHi7Fv7/7RZD02rvZyG3hlsjnQKiVKVWKgr9kRKgmV+dEfu8KScvysiC0Wg=="
},
"FluentAssertions": {
"type": "Direct",
"requested": "[6.11.0, )",
"resolved": "6.11.0",
"contentHash": "aBaagwdNtVKkug1F3imGXUlmoBd8ZUZX8oQ5niThaJhF79SpESe1Gzq7OFuZkQdKD5Pa4Mone+jrbas873AT4g==",
"dependencies": {
"System.Configuration.ConfigurationManager": "4.4.0"
}
},
"Kentico.Xperience.Core.Tests": {
"type": "Direct",
"requested": "[25.0.0, )",
Expand All @@ -29,6 +38,15 @@
"Microsoft.TestPlatform.TestHost": "17.5.0"
}
},
"NSubstitute": {
"type": "Direct",
"requested": "[5.0.0, )",
"resolved": "5.0.0",
"contentHash": "CWGy4oXvcJcZzL7m5Rr/eMSejFXMDq1RRMlsbL6eltS3AuN/d8GH3ZzUcuStQcCSlcx+hpsgTxdnb3lhozWG6w==",
"dependencies": {
"Castle.Core": "5.0.0"
}
},
"NUnit": {
"type": "Direct",
"requested": "[3.13.3, )",
Expand Down Expand Up @@ -95,6 +113,14 @@
"System.Threading.Tasks.Extensions": "4.5.4"
}
},
"Castle.Core": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "edc8jjyXqzzy8jFdhs36FZdwmlDDTgqPb2Zy1Q5F/f2uAc88bu/VS/0Tpvgupmpl9zJOvOo5ZizVANb0ltN1NQ==",
"dependencies": {
"System.Diagnostics.EventLog": "6.0.0"
}
},
"CommandLineParser": {
"type": "Transitive",
"resolved": "2.9.1",
Expand Down Expand Up @@ -738,6 +764,11 @@
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
}
},
"System.Diagnostics.EventLog": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "lcyUiXTsETK2ALsZrX+nWuHSIQeazhqPphLfaRxzdGaG93+0kELqpgEHtwWOlQe7+jSFnKwaCAgL4kjeZCQJnw=="
},
"System.Diagnostics.Tracing": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down

0 comments on commit 2ef3e81

Please sign in to comment.