Skip to content

Commit

Permalink
test(Outlines): add outlines and styles tag helper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seangwright committed May 28, 2023
1 parent 2ef3e81 commit c8e67de
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,53 @@ namespace XperienceCommunity.PreviewComponentOutlines
/// </summary>
public class OutlinesStylesTagHelperComponent : TagHelperComponent
{
private string Style
private string style = "";

public string Style
{
get
{
return $$"""
<style>
[data-xpc-preview-outline]:hover {
outline: 2px {{config.OutlineColor}} dashed;
position: relative;
}
if (string.IsNullOrWhiteSpace(style))
{
style = $$"""
<style>
[data-xpc-preview-outline]:hover {
outline: 2px {{config.OutlineColor}} dashed;
position: relative;
}

[data-xpc-preview-outline]:hover::before {
display: inline;
position: absolute;
white-space: pre;
content: attr(data-xpc-preview-outline);
padding: {{config.LabelPadding}};
color: {{config.LabelFontColor}};
background-color: {{config.LabelBackgroundColor}};
border: 1px solid {{config.LabelBorderColor}};
border-radius: 5000px;
font-size: {{config.LabelFontSize}};
z-index: 1;
opacity: {{config.LabelOpacity}};
isolation: isolate;
}
[data-xpc-preview-outline]:hover::before {
display: inline;
position: absolute;
white-space: pre;
content: attr(data-xpc-preview-outline);
padding: {{config.LabelPadding}};
color: {{config.LabelFontColor}};
background-color: {{config.LabelBackgroundColor}};
border: 1px solid {{config.LabelBorderColor}};
border-radius: 5000px;
font-size: {{config.LabelFontSize}};
z-index: 1;
opacity: {{config.LabelOpacity}};
isolation: isolate;
}

[data-xpc-preview-outline$='Section']:hover::before {
top: .2rem;
left: 50%;
transform: translate(-50%, 0);
}
[data-xpc-preview-outline$='Section']:hover::before {
top: .2rem;
left: 50%;
transform: translate(-50%, 0);
}

[data-xpc-preview-outline$='Widget']:hover::before {
top: .2rem;
left: 0;
left: 0.2rem;
}
</style>
""";
[data-xpc-preview-outline$='Widget']:hover::before {
top: .2rem;
left: 0;
left: 0.2rem;
}
</style>
""";
}

return style;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Kentico.Content.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;

namespace XperienceCommunity.PreviewComponentOutlinesTests
{
public static class ContextFixtures
{
public static 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
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()
public void The_Data_Attribute_Will_Not_Be_Added_When_The_Request_Is_In_Live_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) }
{ "Kentico.Features", ContextFixtures.InitializeFeatures(isEditModeEnabled: false, isPreviewModeEnabled: false) }
};
accessor.HttpContext.Returns(httpContext);

Expand All @@ -43,19 +39,76 @@ public void The_Data_Attribute_Will_Be_Added_When_The_Request_Is_In_Preview_Mode
.Where(a =>
string.Equals(a.Name, OutlineTagHelper.TAG_HELPER_OUTPUT_ATTRIBUTE, StringComparison.OrdinalIgnoreCase)
&& string.Equals(a.Value.ToString(), componentName, StringComparison.OrdinalIgnoreCase))
.Should().HaveCount(1);
.Should().BeEmpty();
}

private IFeatureSet InitializeFeatures(bool isEditModeEnabled, bool isPreviewModeEnabled)
[Test]
public void The_Data_Attribute_Will_Not_Be_Added_When_The_Request_Is_In_Edit_Mode()
{
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;
string componentName = "My Test Widget";

var accessor = Substitute.For<IHttpContextAccessor>();
var httpContext = Substitute.For<HttpContext>();
httpContext.Items = new Dictionary<object, object?>
{
{ "Kentico.Features", ContextFixtures.InitializeFeatures(isEditModeEnabled: true, isPreviewModeEnabled: false) }
};
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().BeEmpty();
}

[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", ContextFixtures.InitializeFeatures(isEditModeEnabled: false, isPreviewModeEnabled: 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Options;
using XperienceCommunity.PreviewComponentOutlines;

namespace XperienceCommunity.PreviewComponentOutlinesTests
{
public class OutlinesStylesTagHelperComponentTests
{
[Test]
public async Task The_Styles_Element_Will_Not_Be_Added_When_The_Request_Is_In_Live_Mode()
{
var accessor = Substitute.For<IHttpContextAccessor>();
var httpContext = Substitute.For<HttpContext>();
httpContext.Items = new Dictionary<object, object?>
{
{ "Kentico.Features", ContextFixtures.InitializeFeatures(isEditModeEnabled: false, isPreviewModeEnabled: false) }
};
accessor.HttpContext.Returns(httpContext);

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

var options = Options.Create(new OutlinesConfiguration());

var sut = new OutlinesStylesTagHelperComponent(accessor, options);

await sut.ProcessAsync(tagHelperContext, output);

output.PostContent.IsEmptyOrWhiteSpace.Should().BeTrue();
}

[Test]
public async Task The_Styles_Element_Will_Not_Be_Added_When_The_Request_Is_In_Edit_Mode()
{
var accessor = Substitute.For<IHttpContextAccessor>();
var httpContext = Substitute.For<HttpContext>();
httpContext.Items = new Dictionary<object, object?>
{
{ "Kentico.Features", ContextFixtures.InitializeFeatures(isEditModeEnabled: true, isPreviewModeEnabled: false) }
};
accessor.HttpContext.Returns(httpContext);

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

var options = Options.Create(new OutlinesConfiguration());

var sut = new OutlinesStylesTagHelperComponent(accessor, options);

await sut.ProcessAsync(tagHelperContext, output);

output.PostContent.IsEmptyOrWhiteSpace.Should().BeTrue();
}

[Test]
public async Task The_Styles_Element_Will_Not_Be_Added_When_The_Request_Is_In_Preview_Mode_And_The_Configuration_Does_Not_Use_Included_Styles()
{
var accessor = Substitute.For<IHttpContextAccessor>();
var httpContext = Substitute.For<HttpContext>();
httpContext.Items = new Dictionary<object, object?>
{
{ "Kentico.Features", ContextFixtures.InitializeFeatures(isEditModeEnabled: false, isPreviewModeEnabled: true) }
};
accessor.HttpContext.Returns(httpContext);

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

var options = Options.Create(new OutlinesConfiguration
{
UseIncludedStyles = false
});

var sut = new OutlinesStylesTagHelperComponent(accessor, options);

await sut.ProcessAsync(tagHelperContext, output);

output.PostContent.IsEmptyOrWhiteSpace.Should().BeTrue();
}

[Test]
public async Task The_Styles_Element_Will_Be_Added_When_The_Request_Is_In_Preview_Mode()
{
var accessor = Substitute.For<IHttpContextAccessor>();
var httpContext = Substitute.For<HttpContext>();
httpContext.Items = new Dictionary<object, object?>
{
{ "Kentico.Features", ContextFixtures.InitializeFeatures(isEditModeEnabled: false, isPreviewModeEnabled: true) }
};
accessor.HttpContext.Returns(httpContext);

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

var config = new OutlinesConfiguration();
var options = Options.Create(config);

var sut = new OutlinesStylesTagHelperComponent(accessor, options);

await sut.ProcessAsync(tagHelperContext, output);

var content = output.PostContent.GetContent();

content.Should().BeEquivalentTo(sut.Style);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ItemGroup>
<Using Include="NUnit.Framework" />
<Using Include="NSubstitute" />
<Using Include="XperienceCommunity.PreviewComponentOutlinesTests" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit c8e67de

Please sign in to comment.