2020-09-11 14:37:38 +08:00
|
|
|
|
using System;
|
|
|
|
|
using AntDesign.JsInterop;
|
|
|
|
|
using Bunit;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Rendering;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.JSInterop;
|
|
|
|
|
using Moq;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign.Tests.tabs
|
|
|
|
|
{
|
|
|
|
|
public class TabsTests : AntDesignTestBase
|
|
|
|
|
{
|
|
|
|
|
private IRenderedComponent<Tabs> CreateTabs(Action<RenderTreeBuilder> childContent)
|
|
|
|
|
{
|
|
|
|
|
var jsRuntime = new Mock<IJSRuntime>();
|
|
|
|
|
jsRuntime.Setup(u => u.InvokeAsync<Element>(JSInteropConstants.GetDomInfo, It.IsAny<object[]>()))
|
|
|
|
|
.ReturnsAsync(new Element());
|
|
|
|
|
|
|
|
|
|
Context.Services.AddScoped(_ => jsRuntime.Object);
|
|
|
|
|
|
|
|
|
|
var cut = Context.RenderComponent<Tabs>(tabs => tabs
|
|
|
|
|
.Add(x => x.DefaultActiveKey, "2")
|
|
|
|
|
.Add(b => b.ChildContent, b => childContent(b))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return cut;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 20:02:35 +08:00
|
|
|
|
private static RenderFragment CreateTabPanel(string key, Action<ComponentParameterCollectionBuilder<TabPane>> configure = null)
|
2020-09-11 14:37:38 +08:00
|
|
|
|
{
|
2021-01-20 20:02:35 +08:00
|
|
|
|
var tabPane1Builder = new ComponentParameterCollectionBuilder<TabPane>()
|
2020-09-11 14:37:38 +08:00
|
|
|
|
.Add(x => x.Key, key)
|
2020-11-24 18:47:52 +08:00
|
|
|
|
.Add(x => x.Tab, $"Tab {key}".ToRenderFragment())
|
|
|
|
|
.Add(x => x.ChildContent, $"Content {key}".ToRenderFragment());
|
2020-09-11 14:37:38 +08:00
|
|
|
|
|
|
|
|
|
configure?.Invoke(tabPane1Builder);
|
|
|
|
|
|
2021-01-20 20:02:35 +08:00
|
|
|
|
return tabPane1Builder.Build().ToRenderFragment<TabPane>();
|
|
|
|
|
|
2020-09-11 14:37:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Should_only_render_the_active_pane_when_ForceRender_is_false()
|
|
|
|
|
{
|
|
|
|
|
var cut = CreateTabs(p =>
|
|
|
|
|
{
|
|
|
|
|
var tabPane1 = CreateTabPanel("1");
|
|
|
|
|
var tabPane2 = CreateTabPanel("2");
|
|
|
|
|
|
|
|
|
|
tabPane1(p);
|
|
|
|
|
tabPane2(p);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var renderedPanes = cut.FindAll(".ant-tabs-tabpane");
|
|
|
|
|
|
|
|
|
|
Assert.Equal(1, renderedPanes.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Should_render_the_all_panes_when_ForceRender_is_true()
|
|
|
|
|
{
|
|
|
|
|
var cut = CreateTabs(p =>
|
|
|
|
|
{
|
|
|
|
|
var tabPane1 = CreateTabPanel("1", x => x.Add(y => y.ForceRender, true));
|
|
|
|
|
var tabPane2 = CreateTabPanel("2", x => x.Add(y => y.ForceRender, true));
|
|
|
|
|
|
|
|
|
|
tabPane1(p);
|
|
|
|
|
tabPane2(p);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var renderedPanes = cut.FindAll(".ant-tabs-tabpane");
|
|
|
|
|
|
|
|
|
|
Assert.Equal(2, renderedPanes.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Should_preserve_tab_panels_once_they_have_been_rendered()
|
|
|
|
|
{
|
|
|
|
|
var cut = CreateTabs(p =>
|
|
|
|
|
{
|
|
|
|
|
var tabPane1 = CreateTabPanel("1");
|
|
|
|
|
var tabPane2 = CreateTabPanel("2");
|
|
|
|
|
|
|
|
|
|
tabPane1(p);
|
|
|
|
|
tabPane2(p);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var renderedPanes = cut.FindAll(".ant-tabs-tabpane", true);
|
|
|
|
|
Assert.Equal(1, renderedPanes.Count);
|
|
|
|
|
|
|
|
|
|
cut.SetParametersAndRender(p => p.Add(x => x.ActiveKey, "1"));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(2, renderedPanes.Count);
|
|
|
|
|
}
|
2020-11-24 18:47:52 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Clicking_on_an_inactive_tab_should_make_it_active()
|
|
|
|
|
{
|
|
|
|
|
var cut = CreateTabs(p =>
|
|
|
|
|
{
|
|
|
|
|
var tabPane1 = CreateTabPanel("1");
|
|
|
|
|
var tabPane2 = CreateTabPanel("2");
|
|
|
|
|
|
|
|
|
|
tabPane1(p);
|
|
|
|
|
tabPane2(p);
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-21 17:20:10 +08:00
|
|
|
|
Assert.Equal("Content 2", cut.Find(".ant-tabs-tabpane").TextContent.Trim());
|
2020-11-24 18:47:52 +08:00
|
|
|
|
|
2020-11-30 10:53:41 +08:00
|
|
|
|
cut.Find("div.ant-tabs-tab").Click();
|
2020-11-24 18:47:52 +08:00
|
|
|
|
|
2021-01-21 17:20:10 +08:00
|
|
|
|
Assert.Equal("Content 1", cut.Find(".ant-tabs-tabpane").TextContent.Trim());
|
2020-11-24 18:47:52 +08:00
|
|
|
|
}
|
2020-09-11 14:37:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|