-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(components): add tests for the switch component
- Loading branch information
1 parent
a061ca7
commit 86f38ed
Showing
1 changed file
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) LumexUI 2024 | ||
// LumexUI licenses this file to you under the MIT license | ||
// See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
||
using LumexUI.Common; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using TailwindMerge; | ||
|
||
namespace LumexUI.Tests.Components; | ||
|
||
public class SwitchTests : TestContext | ||
{ | ||
public SwitchTests() | ||
{ | ||
Services.AddSingleton<TwMerge>(); | ||
} | ||
|
||
[Fact] | ||
public void Switch_ShouldRenderCorrectly() | ||
{ | ||
var action = () => RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.ValueExpression, () => true ) | ||
); | ||
|
||
action.Should().NotThrow(); | ||
} | ||
|
||
[Fact] | ||
public void Switch_WithChildContent_ShouldRenderCorrectly() | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.ValueExpression, () => true ) | ||
.AddChildContent( "switch" ) | ||
); | ||
|
||
cut.Markup.Should().Contain( "switch" ); | ||
} | ||
|
||
[Fact] | ||
public void Switch_WithThumbIcon_ShouldRenderCustomIcon() | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.ValueExpression, () => true ) | ||
.Add( p => p.ThumbIcon, Icons.Rounded.Headphones ) | ||
); | ||
|
||
cut.FindComponent<LumexIcon>().Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Switch_WithStartIcon_ShouldRenderCustomIcon() | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.ValueExpression, () => true ) | ||
.Add( p => p.StartIcon, Icons.Rounded.Headphones ) | ||
); | ||
|
||
cut.FindComponent<LumexIcon>().Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Switch_WithEndIcon_ShouldRenderCustomIcon() | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.ValueExpression, () => true ) | ||
.Add( p => p.EndIcon, Icons.Rounded.Headphones ) | ||
); | ||
|
||
cut.FindComponent<LumexIcon>().Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Switch_WithStartAndEndIcon_ShouldRenderCustomIcons() | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.ValueExpression, () => true ) | ||
.Add( p => p.StartIcon, Icons.Rounded.HeadphonesBattery ) | ||
.Add( p => p.EndIcon, Icons.Rounded.Headphones ) | ||
); | ||
|
||
cut.FindComponents<LumexIcon>().Should().NotBeNull(); | ||
cut.FindComponents<LumexIcon>().Should().HaveCount( 2 ); | ||
} | ||
|
||
[Fact] | ||
public void Switch_OnChange_ShouldChangeValue() | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.Value, false ) | ||
.Add( p => p.ValueExpression, () => true ) | ||
); | ||
|
||
cut.Instance.Value.Should().BeFalse(); | ||
|
||
var @switch = cut.Find( "input" ); | ||
@switch.Change( true ); | ||
|
||
cut.Instance.Value.Should().BeTrue(); | ||
} | ||
|
||
[Theory] | ||
[InlineData( true, false )] | ||
[InlineData( false, true )] | ||
public void Switch_DisabledOrReadOnly_ShouldNotTriggerChange( bool disabled, bool readOnly ) | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.Value, true ) | ||
.Add( p => p.ValueExpression, () => true ) | ||
.Add( p => p.Disabled, disabled ) | ||
.Add( p => p.ReadOnly, readOnly ) | ||
); | ||
|
||
cut.Instance.Value.Should().BeTrue(); | ||
|
||
var @switch = cut.Find( "input" ); | ||
@switch.Change( false ); | ||
|
||
cut.Instance.Value.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task Switch_SetCurrentValueAsString_ShouldThrowNotSupported() | ||
{ | ||
var cut = RenderComponent<LumexSwitch>( p => p | ||
.Add( p => p.Value, false ) | ||
.Add( p => p.ValueExpression, () => true ) | ||
); | ||
|
||
var action = async () => await cut.Instance.SetCurrentValueAsStringAsync( "true" ); | ||
|
||
await action.Should().ThrowAsync<NotSupportedException>(); | ||
} | ||
} |