-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from IowaComputerGurus/feature/card-header-but…
…tons Fixes #20 by adding back the helper
- Loading branch information
Showing
4 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/AspNetCore.Utilities.Bootstrap5TagHelpers.Tests/Card/CardHeaderActionsTagHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Card; | ||
using ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Contexts; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
|
||
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Tests.Card; | ||
public class CardHeaderActionsTagHelperTests : AbstractTagHelperTest | ||
{ | ||
[Fact] | ||
public async Task Should_ThrowException_WhenMissingContext() | ||
{ | ||
//Arrange | ||
var context = MakeTagHelperContext(); | ||
var output = MakeTagHelperOutput(" "); | ||
|
||
//Act | ||
var helper = new CardHeaderActionsTagHelper(); | ||
var exceptionResult = await Record.ExceptionAsync(() => helper.ProcessAsync(context, output)); | ||
|
||
Assert.NotNull(exceptionResult); | ||
Assert.IsType<KeyNotFoundException>(exceptionResult); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_ThrowException_WhenContextIsNull() | ||
{ | ||
//Arrange | ||
var context = MakeTagHelperContext(); | ||
context.Items.Add(typeof(CardContext), null); | ||
var output = MakeTagHelperOutput(" "); | ||
|
||
//Act | ||
var helper = new CardHeaderActionsTagHelper(); | ||
var exceptionResult = await Record.ExceptionAsync(() => helper.ProcessAsync(context, output)); | ||
|
||
Assert.NotNull(exceptionResult); | ||
Assert.IsType<ArgumentException>(exceptionResult); | ||
} | ||
|
||
[Theory] | ||
[InlineData("d-flex")] | ||
[InlineData("flex-nowrap")] | ||
[InlineData("mt-2")] | ||
[InlineData("mt-sm-0")] | ||
public async Task Should_Render_With_ClassAdded(string expectedClass) | ||
{ | ||
//Arrange | ||
var context = MakeTagHelperContext(); | ||
context.Items.Add(typeof(CardContext), new CardContext()); | ||
var output = MakeTagHelperOutput(" "); | ||
|
||
//Act | ||
var helper = new CardHeaderActionsTagHelper(); | ||
await helper.ProcessAsync(context, output); | ||
|
||
//Assert | ||
var classValue = output.Attributes["class"].Value; | ||
Assert.NotNull(classValue); | ||
var classString = classValue.ToString(); | ||
Assert.True(classString?.Contains(expectedClass)); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_Render_With_ClassAdded_PreservingCustomClasses() | ||
{ | ||
//Arrange | ||
var customClass = "testing-out"; | ||
var expectedClass = $"{customClass} d-flex flex-nowrap mt-2 mt-sm-0"; | ||
var existingAttributes = new TagHelperAttributeList(new List<TagHelperAttribute> | ||
{new("class", customClass)}); | ||
var context = MakeTagHelperContext(); | ||
context.Items.Add(typeof(CardContext), new CardContext()); | ||
var output = MakeTagHelperOutput(" ", existingAttributes); | ||
|
||
//Act | ||
var helper = new CardHeaderActionsTagHelper(); | ||
await helper.ProcessAsync(context, output); | ||
|
||
//Assert | ||
Assert.Equal(expectedClass, output.Attributes["class"].Value.ToString()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/AspNetCore.Utilities.Bootstrap5TagHelpers/Card/CardHeaderActionsTagHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Contexts; | ||
using Microsoft.AspNetCore.Mvc.TagHelpers; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using System; | ||
|
||
namespace ICG.AspNetCore.Utilities.Bootstrap5TagHelpers.Card; | ||
|
||
/// <summary> | ||
/// Helper for rendering actions within the header of a card | ||
/// </summary> | ||
[RestrictChildren("button", "a")] | ||
public class CardHeaderActionsTagHelper : TagHelper | ||
{ | ||
/// <summary> | ||
/// Renders the control | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="output"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentException"></exception> | ||
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) | ||
{ | ||
//Get the context information | ||
var cardContext = context.Items[typeof(CardContext)] as CardContext; | ||
if (cardContext == null) | ||
throw new ArgumentException("CardContext is not specified in context parameter"); | ||
|
||
return ProcessAsyncInternal(output); | ||
} | ||
|
||
/// <summary> | ||
/// Internal implementation | ||
/// </summary> | ||
/// <param name="output"></param> | ||
/// <returns></returns> | ||
private static async Task ProcessAsyncInternal(TagHelperOutput output) | ||
{ | ||
output.TagName = "div"; | ||
output.AddClass("d-flex", HtmlEncoder.Default); | ||
output.AddClass("flex-nowrap", HtmlEncoder.Default); | ||
output.AddClass("mt-2", HtmlEncoder.Default); | ||
output.AddClass("mt-sm-0", HtmlEncoder.Default); | ||
|
||
var content = (await output.GetChildContentAsync()).GetContent(); | ||
|
||
output.Content.AppendHtml(content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters