Skip to content

Commit

Permalink
feat(blazorui): improve BitLabel tests #7824 (#7866)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrus-Sushiant committed Jun 25, 2024
1 parent 69986bc commit db49214
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<BitLabel data-val-test="bit">I'm a label</BitLabel>
6 changes: 0 additions & 6 deletions src/BlazorUI/Bit.BlazorUI.Tests/Labels/BitLabelTest.razor

This file was deleted.

198 changes: 193 additions & 5 deletions src/BlazorUI/Bit.BlazorUI.Tests/Labels/BitLabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,199 @@ namespace Bit.BlazorUI.Tests.Labels;
[TestClass]
public class BitLabelTests : BunitTestContext
{
[DataTestMethod, DataRow(true, true), DataRow(false, false)]
public void BitLabelShouldRespectIsRequired(bool isRequired, bool expectedResult)
[DataTestMethod]
public void BitLabelShouldRenderExpectedElement()
{
var component = RenderComponent<BitLabelTest>(parameters => parameters.Add(p => p.IsRequired, isRequired));
var bitLabel = component.Find(".bit-lbl");
Assert.AreEqual(expectedResult, bitLabel.ClassList.Contains("bit-lbl-req"));
var component = RenderComponent<BitLabel>();

component.MarkupMatches(@"<label id:regex="".+"" class=""bit-lbl""></label>");
}

[DataTestMethod,
DataRow(true),
DataRow(false)
]
public void BitLabelShouldRespectIsEnabled(bool isEnabled)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.IsEnabled, isEnabled);
});

var cssClass = isEnabled ? "bit-lbl" : "bit-lbl bit-dis";

component.MarkupMatches(@$"<label class=""{cssClass}"" id:ignore></label>");
}

[DataTestMethod,
DataRow(true),
DataRow(false)
]
public void BitLabelShouldRespectRequired(bool required)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.Required, required);
});

var cssClass = required ? "bit-lbl bit-lbl-req" : "bit-lbl";

component.MarkupMatches(@$"<label class=""{cssClass}"" id:ignore></label>");
}

[DataTestMethod,
DataRow("font-size: 14px; color: red;"),
DataRow("padding: 1rem;"),
DataRow(null)
]
public void BitLabelShouldRespectStyle(string style)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.Style, style);
});

if (style.HasNoValue())
{
component.MarkupMatches(@"<label id:ignore class:ignore></label>");
}
else
{
component.MarkupMatches(@$"<label style=""{style}"" id:ignore class:ignore></label>");
}
}

[DataTestMethod,
DataRow("test-class"),
DataRow(null)
]
public void BitLabelShouldRespectClass(string @class)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.Class, @class);
});

var cssClass = @class.HasValue() ? $"bit-lbl {@class}" : "bit-lbl";

component.MarkupMatches(@$"<label class=""{cssClass}"" id:ignore></label>");
}

[DataTestMethod,
DataRow("test-id"),
DataRow(null)
]
public void BitLabelShouldRespectId(string id)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.Id, id);
});


if (id.HasNoValue())
{
component.MarkupMatches(@$"<label id=""{component.Instance.UniqueId}"" class:ignore></label>");
}
else
{
component.MarkupMatches(@$"<label id=""{id}"" class:ignore></label>");
}
}

[DataTestMethod,
DataRow("test-for"),
DataRow(null)
]
public void BitLabelShouldRespectFor(string @for)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.For, @for);
});


if (@for.HasNoValue())
{
component.MarkupMatches(@"<label id:ignore class:ignore></label>");
}
else
{
component.MarkupMatches(@$"<label for=""{@for}"" id:ignore class:ignore></label>");
}
}

[DataTestMethod,
DataRow(BitDir.Rtl),
DataRow(BitDir.Ltr),
DataRow(BitDir.Auto),
DataRow(null)
]
public void BitLabelShouldRespectDir(BitDir? dir)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.Dir, dir);
});

if (dir.HasValue)
{
var cssClass = dir is BitDir.Rtl ? "bit-lbl bit-rtl" : "bit-lbl";
component.MarkupMatches(@$"<label class=""{cssClass}"" dir=""{dir.Value.ToString().ToLower()}"" id:ignore></label>");
}
else
{
component.MarkupMatches(@"<label class=""bit-lbl"" id:ignore></label>");
}
}

[DataTestMethod,
DataRow(BitVisibility.Visible),
DataRow(BitVisibility.Collapsed),
DataRow(BitVisibility.Hidden)
]
public void BitLabelShouldRespectVisibility(BitVisibility visibility)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.Add(p => p.Visibility, visibility);
});


switch (visibility)
{
case BitVisibility.Visible:
component.MarkupMatches(@"<label id:ignore class:ignore></label>");
break;
case BitVisibility.Hidden:
component.MarkupMatches(@"<label style=""visibility: hidden;"" id:ignore class:ignore></label>");
break;
case BitVisibility.Collapsed:
component.MarkupMatches(@"<label style=""display: none;"" id:ignore class:ignore></label>");
break;
}
}

[DataTestMethod,
DataRow("Bit Blazor UI"),
DataRow("<span>Bit Blazor UI</span>"),
DataRow(null)
]
public void BitLabelShouldRespectChildContent(string childContent)
{
var component = RenderComponent<BitLabel>(parameters =>
{
parameters.AddChildContent(childContent);
});

component.MarkupMatches(@$"<label id:ignore class:ignore>{childContent}</label>");
}

[DataTestMethod]
public void BitLabelShouldRespectHtmlAttributes()
{
var component = RenderComponent<BitLabelHtmlAttributesTest>();

component.MarkupMatches(@"<label data-val-test=""bit"" id:ignore class:ignore>I'm a label</label>");
}
}

0 comments on commit db49214

Please sign in to comment.