Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FluentCheckbox class tag always displayed #341

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,69 @@ namespace Microsoft.Fast.Components.FluentUI.Tests.Checkbox
{
public class CheckboxRenderShould : TestBase
{
[Fact]
public void RenderProperly_DefaultValues()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void RenderProperly_DefaultValues(bool currentValue)
{
// Arrange && Act
string childContent = "childContent";
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent));

.AddChildContent(childContent)
.Bind(bind => bind.Value, currentValue, newValue => currentValue = false));

// Assert
sut.MarkupMatches("<fluent-checkbox>" +
$"{childContent}" +
"</fluent-checkbox>");
if (currentValue)
{
sut.MarkupMatches("<fluent-checkbox " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
else
{
sut.MarkupMatches("<fluent-checkbox>" +
$"{childContent}" +
"</fluent-checkbox>");
}
}

[Fact]
public void RenderProperly_ReadonlyParameter()

[Theory]
[InlineData(true)]
[InlineData(false)]
public void RenderProperly_ReadonlyParameter(bool currentValue)
{
// Arrange && Act
string childContent = "childContent";
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.Add(p => p.Readonly, true));

// Assert
sut.MarkupMatches("<fluent-checkbox " +
"readonly=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
if (currentValue)
{
sut.MarkupMatches("<fluent-checkbox " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\" " +
"readonly=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
else
{
sut.MarkupMatches("<fluent-checkbox " +
"readonly=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
}

[Theory]
[InlineData(null)]
[InlineData("")]
Expand All @@ -46,44 +77,57 @@ public void RenderProperly_IdParameter(string? idParameter)
{
// Arrange && Act
string childContent = "childContent";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = true)
.Add(p => p.Id, idParameter));

// Assert
if (idParameter is null)
{
sut.MarkupMatches("<fluent-checkbox>" +
sut.MarkupMatches("<fluent-checkbox " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
else
{
sut.MarkupMatches("<fluent-checkbox " +
$"id=\"{idParameter}\">" +
$"id=\"{idParameter}\" " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
}

[Fact]
public void RenderProperly_DisabledParameter()
{
// Arrange && Act
string childContent = "childContent";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.Add(p => p.Disabled, true));

// Assert
sut.MarkupMatches("<fluent-checkbox " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\" " +
"disabled=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}

[Theory]
[InlineData(null)]
[InlineData("")]
Expand All @@ -93,127 +137,127 @@ public void RenderProperly_NameParameter(string? nameParameter)
{
// Arrange && Act
string childContent = "childContent";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.Add(p => p.Name, nameParameter));

// Assert
if (nameParameter is null)
{
sut.MarkupMatches("<fluent-checkbox>" +
sut.MarkupMatches("<fluent-checkbox " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
else
{
sut.MarkupMatches("<fluent-checkbox " +
sut.MarkupMatches("<fluent-checkbox " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\" " +
$"name=\"{nameParameter}\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
}

[Fact]
public void RenderProperly_RequiredParameter()
{
// Arrange && Act
string childContent = "childContent";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.Add(p => p.Required, true));

// Assert
sut.MarkupMatches("<fluent-checkbox " +
"required=\"\">" +
"required=\"\" " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void RenderProperly_ValueParameter(bool value)
{
// Arrange && Act
string childContent = "childContent";
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Add(p => p.Value, value));

// Assert
if (value)
{
sut.MarkupMatches("<fluent-checkbox " +
"value=\"\" current-value=\"\" current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
else
{
sut.MarkupMatches("<fluent-checkbox>" +
$"{childContent}" +
"</fluent-checkbox>");
}
}

[Fact]
public void RenderProperly_ClassParameter()
{
// Arrange && Act
string childContent = "childContent";
string cssClass = "additional-css-class";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.Add(p => p.Class, cssClass));

// Assert
sut.MarkupMatches("<fluent-checkbox " +
$"class=\"{cssClass}\">" +
$"class=\"{cssClass}\" " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}

[Fact]
public void RenderProperly_StyleParameter()
{
// Arrange && Act
string childContent = "childContent";
string style = "background-color: red;";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.Add(p => p.Style, style));

// Assert
sut.MarkupMatches("<fluent-checkbox " +
$"style=\"{style}\">" +
$"style=\"{style}\" " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}

[Fact]
public void RenderProperly_AdditionalParameter()
{
// Arrange && Act
string childContent = "childContent";
string parameterName = "parameterName";
string parameterValue = "parameterValue";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.AddUnmatched(parameterName, parameterValue));

// Assert
sut.MarkupMatches("<fluent-checkbox " +
$"{parameterName}=\"{parameterValue}\">" +
$"{parameterName}=\"{parameterValue}\" " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}

[Fact]
public void RenderProperly_AdditionalParameters()
{
Expand All @@ -223,16 +267,21 @@ public void RenderProperly_AdditionalParameters()
string parameter1Value = "parameter1Value";
string parameter2Name = "parameter2Name";
string parameter2Value = "parameter2Value";
bool currentValue = true;
IRenderedComponent<FluentCheckbox> sut = TestContext.RenderComponent<FluentCheckbox>(
parameters => parameters
.AddChildContent(childContent)
.Bind(p => p.Value, currentValue, newValue => currentValue = false)
.AddUnmatched(parameter1Name, parameter1Value)
.AddUnmatched(parameter2Name, parameter2Value));

// Assert
sut.MarkupMatches("<fluent-checkbox " +
$"{parameter1Name}=\"{parameter1Value}\" " +
$"{parameter2Name}=\"{parameter2Value}\">" +
$"{parameter2Name}=\"{parameter2Value}\" " +
"value=\"\" " +
"current-value=\"\" " +
"current-checked=\"\">" +
$"{childContent}" +
"</fluent-checkbox>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public abstract class FluentInputBase<TValue> : FluentComponentBase, IDisposable
/// </summary>
[Parameter]
public string? DisplayName { get; set; }




/// <summary>
/// Gets the associated <see cref="AspNetCore.Components.Forms.EditContext"/>.
/// This property is uninitialized if the input does not have a parent <see cref="EditForm"/>.
Expand Down Expand Up @@ -213,11 +211,16 @@ protected virtual string? ClassValue
get
{
string? fieldClass = EditContext?.FieldCssClass(FieldIdentifier);
string? cssClass = CombineClassNames(AdditionalAttributes, fieldClass) ?? string.Empty;
string? cssClass = CombineClassNames(AdditionalAttributes, fieldClass);

if (!string.IsNullOrEmpty(cssClass) || !string.IsNullOrEmpty(Class))
{
return new CssBuilder(Class)
.AddClass(cssClass)
.Build();
}

return new CssBuilder(Class)
.AddClass(cssClass)
.Build();
return null;
}
}

Expand Down