Skip to content

Commit ba61010

Browse files
author
Warren Buckley
authored
Merge pull request #20 from prjseal/feature/add-unit-tests
Feature/add unit tests
2 parents da27cec + 7a25e1b commit ba61010

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using Our.Umbraco.TagHelpers;
6+
using System.Threading.Tasks;
7+
8+
namespace Our.Umbraco.TagHelpers.Tests
9+
{
10+
public class IncludeIfTagHelperTests
11+
{
12+
[TestCase(true, "original-child-content", "original-child-content")]
13+
[TestCase(false, "original-child-content", "")]
14+
public async Task Given_Predicate_Return_Contents_Or_Empty(bool predicate, string childContent, string expected)
15+
{
16+
// Arrange
17+
var id = "unique-id";
18+
var tagHelperContext = GetTagHelperContext(id);
19+
var tagHelperOutput = GetTagHelperOutput(
20+
attributes: new TagHelperAttributeList(),
21+
childContent: childContent);
22+
tagHelperOutput.Content.SetContent(childContent);
23+
24+
var tagHelper = new IncludeIfTagHelper { Predicate = predicate };
25+
26+
// Act
27+
await tagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);
28+
29+
var content = tagHelperOutput.Content.GetContent();
30+
31+
// Assert
32+
Assert.AreEqual(expected, content);
33+
}
34+
35+
private static TagHelperContext GetTagHelperContext(string id = "testid")
36+
{
37+
return new TagHelperContext(
38+
tagName: "p",
39+
allAttributes: new TagHelperAttributeList(),
40+
items: new Dictionary<object, object>(),
41+
uniqueId: id);
42+
}
43+
44+
private static TagHelperOutput GetTagHelperOutput(
45+
string tagName = "p",
46+
TagHelperAttributeList attributes = null,
47+
string childContent = "some child content")
48+
{
49+
attributes = attributes ?? new TagHelperAttributeList { { "attr", "value" } };
50+
51+
return new TagHelperOutput(
52+
tagName,
53+
attributes,
54+
getChildContentAsync: (useCachedResult, encoder) =>
55+
{
56+
var tagHelperContent = new DefaultTagHelperContent();
57+
var content = tagHelperContent.SetHtmlContent(childContent);
58+
return Task.FromResult<TagHelperContent>(content);
59+
});
60+
}
61+
}
62+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
11+
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.2.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
13+
<PackageReference Include="NUnit" Version="3.13.1" />
14+
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
15+
<PackageReference Include="coverlet.collector" Version="3.0.2" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\Our.Umbraco.TagHelpers\Our.Umbraco.TagHelpers.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)