Skip to content

Commit

Permalink
Added Blazor wrappers for chat components
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose A. Hernandez committed Feb 14, 2025
1 parent a188625 commit 1c39f7e
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/blazor-workspace/SprightBlazor/ChatMessageType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SprightBlazor;

public enum ChatMessageType
{
System,
Outbound,
Inbound
}

internal static class ChatMessageTypeExtensions
{
private static readonly Dictionary<ChatMessageType, string> _enumValues = AttributeHelpers.GetEnumNamesAsKebabCaseValues<ChatMessageType>();

public static string? ToAttributeValue(this ChatMessageType? value) => value == null ? null : _enumValues[value.Value];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@namespace SprightBlazor
<spright-chat-conversation
@attributes="AdditionalAttributes">
@ChildContent
</spright-chat-conversation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Components;

namespace SprightBlazor;

public partial class SprightChatConversation : ComponentBase
{
/// <summary>
/// The child content of the element.
/// </summary>
[Parameter]
public RenderFragment? ChildContent { get; set; }

/// <summary>
/// Any additional attributes that did not match known properties.
/// </summary>
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object>? AdditionalAttributes { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@namespace SprightBlazor
<spright-chat-message
message-type="@MessageType.ToAttributeValue()"
@attributes="AdditionalAttributes">
@ChildContent
</spright-chat-message>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Components;

namespace SprightBlazor;

public partial class SprightChatMessage : ComponentBase
{
/// <summary>
/// The message type of the message
/// </summary>
[Parameter]
public ChatMessageType? MessageType { get; set; }

/// <summary>
/// The child content of the element.
/// </summary>
[Parameter]
public RenderFragment? ChildContent { get; set; }

/// <summary>
/// Any additional attributes that did not match known properties.
/// </summary>
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object>? AdditionalAttributes { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Bunit;
using Xunit;

namespace SprightBlazor.Tests.Unit.Components;

/// <summary>
/// Test for <see cref="SprightChatConversation"/>.
/// </summary>
public class SprightChatConversationTests
{
[Fact]
public void SprightChatConversation_Render_HasChatConversationMarkup()
{
var context = new TestContext();
context.JSInterop.Mode = JSRuntimeMode.Loose;
var expectedMarkup = "spright-chat-conversation";

var component = context.RenderComponent<SprightChatConversation>();

Assert.Contains(expectedMarkup, component.Markup);
}

[Fact]
public void SprightChatConversation_SupportsAdditionalAttributes()
{
var context = new TestContext();
context.JSInterop.Mode = JSRuntimeMode.Loose;
var exception = Record.Exception(() => context.RenderComponent<SprightChatConversation>(ComponentParameter.CreateParameter("class", "foo")));
Assert.Null(exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Linq.Expressions;
using Bunit;
using Xunit;

namespace SprightBlazor.Tests.Unit.Components;

/// <summary>
/// Test for <see cref="SprightChatMessage"/>.
/// </summary>
public class SprightChatMessageTests
{
[Fact]
public void SprightChatMessage_Render_HasChatConversationMarkup()
{
var context = new TestContext();
context.JSInterop.Mode = JSRuntimeMode.Loose;
var expectedMarkup = "spright-chat-message";

var component = context.RenderComponent<SprightChatMessage>();

Assert.Contains(expectedMarkup, component.Markup);
}

[Fact]
public void SprightChatMessage_SupportsAdditionalAttributes()
{
var context = new TestContext();
context.JSInterop.Mode = JSRuntimeMode.Loose;
var exception = Record.Exception(() => context.RenderComponent<SprightChatMessage>(ComponentParameter.CreateParameter("class", "foo")));
Assert.Null(exception);
}

[Theory]
[InlineData(ChatMessageType.Outbound, "message-type=\"outbound\"")]
[InlineData(ChatMessageType.Inbound, "message-type=\"inbound\"")]
[InlineData(ChatMessageType.System, "message-type=\"system\"")]
public void SprightChatMessageTypeVariant_AttributeIsSet(ChatMessageType value, string expectedAttribute)
{
var button = RenderWithPropertySet(x => x.MessageType, value);

Assert.Contains(expectedAttribute, button.Markup);
}

private IRenderedComponent<SprightChatMessage> RenderWithPropertySet<TProperty>(Expression<Func<SprightChatMessage, TProperty>> propertyGetter, TProperty propertyValue)
{
var context = new TestContext();
context.JSInterop.Mode = JSRuntimeMode.Loose;
return context.RenderComponent<SprightChatMessage>(p => p.Add(propertyGetter, propertyValue));
}
}

0 comments on commit 1c39f7e

Please sign in to comment.