From fe30357d55ce9edc82dbd0b973799f2f6c2d54b1 Mon Sep 17 00:00:00 2001 From: Nine <948825551@qq.com> Date: Mon, 17 Jan 2022 02:27:05 +0000 Subject: [PATCH] =?UTF-8?q?!2324=20test(#I4R1L6):=20add=20alert=20componen?= =?UTF-8?q?t=20unit=20tests=20*=20test:=20=E6=B7=BB=E5=8A=A0=20Toggle=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/ToggleTest.cs | 118 +++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 test/UnitTest/Components/ToggleTest.cs diff --git a/test/UnitTest/Components/ToggleTest.cs b/test/UnitTest/Components/ToggleTest.cs new file mode 100644 index 000000000..e5c9f6d66 --- /dev/null +++ b/test/UnitTest/Components/ToggleTest.cs @@ -0,0 +1,118 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using BootstrapBlazor.Components; +using Bunit; +using Microsoft.AspNetCore.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnitTest.Core; +using Xunit; + +namespace UnitTest.Components; + +public class ToggleTest : BootstrapBlazorTestBase +{ + [Fact] + public void Color_Ok() + { + var cut = Context.RenderComponent(builder => + { + builder.Add(s => s.Color, Color.Success); + builder.Add(s => s.Value, true); + }); + + Assert.Contains("bg-success", cut.Markup); + } + + [Fact] + public void Width_Ok() + { + var cut = Context.RenderComponent(builder => + { + builder.Add(s => s.Width, 100); + builder.Add(s => s.Value, true); + }); + + Assert.Equal(100, cut.Instance.Width); + } + + [Fact] + public void OnText_Ok() + { + var cut = Context.RenderComponent(builder => + { + builder.Add(s => s.OnText, "On"); + builder.Add(s => s.Value, true); + }); + + Assert.Equal("On", cut.Instance.OnText); + } + + [Fact] + public void OffText_Ok() + { + var cut = Context.RenderComponent(builder => + { + builder.Add(s => s.OffText, "Off"); + builder.Add(s => s.Value, true); + }); + + Assert.Equal("Off", cut.Instance.OffText); + } + + [Fact] + public void OnValueChanged_Ok() + { + var value = false; + var cut = Context.RenderComponent(builder => + { + builder.Add(s => s.OnValueChanged, (e) => + { + value = e; + return Task.CompletedTask; + }); + builder.Add(s => s.Value, false); + }); + + cut.Find(".btn-toggle").Click(); + + Assert.True(value); + } + + [Fact] + public void ValueChanged_Ok() + { + var value = false; + var cut = Context.RenderComponent(builder => + { + builder.Add(s => s.ValueChanged, EventCallback.Factory.Create(this, (e) => + { + value = e; + })); + builder.Add(s => s.Value, false); + }); + + cut.Find(".btn-toggle").Click(); + + Assert.True(value); + } + + [Fact] + public void DisplayText_Ok() + { + var cut = Context.RenderComponent(builder => + { + builder.Add(s => s.ShowLabel, true); + builder.Add(s => s.DisplayText, "Toggle"); + }); + + var value = cut.Find(".form-label").TextContent; + + Assert.NotNull(value); + } +}