// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Globalization; using AntDesign.JsInterop; using Bunit; using FluentAssertions; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Xunit; namespace AntDesign.Tests.Avatar { public class AvatarTests : AntDesignTestBase { [Theory] [InlineData(AntSizeLDSType.Small, "ant-avatar-sm", "")] [InlineData(AntSizeLDSType.Large, "ant-avatar-lg", "")] [InlineData("64.5", "", "width:64.5px;height:64.5px;line-height:64.5px;font-size:calc(64.5px / 2);")] public void ItShouldSetSizeProperly(string size, string expectedClass, string expectedStyle) { var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.Size, size) .Add(x => x.Icon, "user")); systemUnderTest.MarkupMatches(@$" "); } [Theory] [InlineData(AvatarShape.Square, "ant-avatar-square")] [InlineData(AvatarShape.Circle, "ant-avatar-circle")] [InlineData(null, "")] public void ItShouldProperlyStyleShapes(string shape, string expectedClass) { var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.Shape, shape) .Add(x => x.Icon, "user")); systemUnderTest.MarkupMatches(@$" "); } [Theory] [InlineData(200, 64, 0.28)] [InlineData(56, 64, 1)] [InlineData(10, 64, 1)] [InlineData(57, 64, 0.9824561403508771929824561404)] public void ItShouldRenderAndScaleTextProperly(int textWidth, decimal avatarWidth, double expectedScale) { JSInterop .Setup("AntDesign.interop.domInfoHelper.getInfo", _ => true) .SetResult(new HtmlElement { OffsetWidth = textWidth }); JSInterop .Setup("AntDesign.interop.domInfoHelper.getBoundingClientRect", _ => true) .SetResult(new DomRect { Width = avatarWidth }); var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.Text, "KR")); systemUnderTest.MarkupMatches(@$" KR "); } [Fact] public void ItShouldRenderAndScaleTextProperlyWhenUnableToGetJsInfo() { JSInterop .Setup("AntDesign.interop.domInfoHelper.getInfo", _ => true) .SetResult(null); JSInterop .Setup("AntDesign.interop.domInfoHelper.getBoundingClientRect", _ => true) .SetResult(null); var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.Text, "KR")); systemUnderTest.MarkupMatches(@$" KR "); } [Theory] [InlineData(200, 64, 0.28)] [InlineData(56, 64, 1)] [InlineData(10, 64, 1)] [InlineData(57, 64, 0.9824561403508771929824561404)] public void ItShouldRenderAndScaleChildContentProperly(int textWidth, decimal avatarWidth, double expectedScale) { JSInterop .Setup("AntDesign.interop.domInfoHelper.getInfo", _ => true) .SetResult(new HtmlElement { OffsetWidth = textWidth }); JSInterop .Setup("AntDesign.interop.domInfoHelper.getBoundingClientRect", _ => true) .SetResult(new DomRect { Width = avatarWidth }); RenderFragment fragment = builder => { builder.OpenElement(0, "span"); builder.AddContent(0, "Text"); builder.CloseElement(); }; var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.ChildContent, fragment)); systemUnderTest.MarkupMatches(@$" Text "); } [Fact] public void ItShouldCallOnErrorWhenImageLoadErrors() { var calledOnError = false; var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.Size, AntSizeLDSType.Default) .Add(x => x.Src, "InvalidImage") .Add(x => x.OnError, () => calledOnError = true) .Add(x => x.Icon, "user")); systemUnderTest.Find("img").TriggerEvent("onerror", new ErrorEventArgs()); systemUnderTest.WaitForAssertion(() => calledOnError.Should().BeTrue()); } [Fact] public void ItShouldRenderIconWhenImageErrorsAndGivenIcon() { var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.Size, AntSizeLDSType.Default) .Add(x => x.Src, "InvalidImage") .Add(x => x.Icon, "user") .Add(x => x.Text, "Won't be used")); systemUnderTest.Find("img").TriggerEvent("onerror", new ErrorEventArgs()); systemUnderTest.WaitForAssertion(() => systemUnderTest.MarkupMatches(@$" ")); } [Fact] public void ItShouldRenderTextWhenImageErrorsAndGivenTextButNotIcon() { JSInterop .Setup("AntDesign.interop.domInfoHelper.getInfo", _ => true) .SetResult(new HtmlElement()); JSInterop .Setup("AntDesign.interop.domInfoHelper.getBoundingClientRect", _ => true) .SetResult(new DomRect()); var systemUnderTest = RenderComponent(parameters => parameters .Add(x => x.Size, AntSizeLDSType.Default) .Add(x => x.Src, "InvalidImage") .Add(x => x.Text, "USER")); systemUnderTest.Find("img").TriggerEvent("onerror", new ErrorEventArgs()); systemUnderTest.WaitForAssertion(() => systemUnderTest.Find(".ant-avatar-string") .MarkupMatches(@$" USER ")); } } }