@inherits GridTestBase
<SfFixture @ref="fixture" Test="GridBasicRendering" Description="Flat grid with data source and column">
<ComponentUnderTest>
<SfGrid DataSource="GenerateOrderData()" AllowPaging="true">
<GridColumns>
<GridColumn Field="@nameof(Order.OrderID)" Width="150"></GridColumn>
<GridColumn Field="@nameof(Order.CustomerID)" Width="150"></GridColumn>
<GridColumn Field="@nameof(Order.Freight)" Width="150" Format="c2"></GridColumn>
</GridColumns>
<GridEvents TValue="Order" DataBound="DataBound"></GridEvents>
</SfGrid>
</ComponentUnderTest>
</SfFixture>
@code
{
SfFixture fixture { get; set; }
bool _canAssert = false;
void GridBasicRendering(Fixture fixture)
{
var cut = fixture.GetComponentUnderTest<SfGrid<Order>>();
Assert.True(12 == cut.FindAll(".e-row").Count, "Number of rows generated properly");
Assert.True(3 == cut.Find(".e-row").Children.Count(), "Number of cells generated properly");
Assert.True(12 == cut.Instance.CurrentViewData.Count(), "CurrentViewData count is proper");
_canAssert = true;
cut.FindComponent<SfPager>().FindAll(".e-link").ElementAt(1).Click();
}
void DataBound()
{
if (_canAssert) {
_canAssert = false;
var cut = fixture.GetComponentUnderTest<SfGrid<Order>>();
Assert.True(8 == cut.FindAll(".e-row").Count, "On paging rows refreshed properly");
Assert.True(8 == cut.Instance.CurrentViewData.Count(), "On paging current view data changed properly");
}
}
}
|
List callResult = new()
{
new Friend { Id = 1.ToGuid(), Name = "Andrea Bioli", City = "Rome", Kind = Friend.RelationshipKind.Good, Pets = new List() },
new Friend { Id = 2.ToGuid(), Name = "Andrea Bioli", City = "Rome", Kind = Friend.RelationshipKind.Good, Pets = new List() }
};
HttpResponseMessage msg = new(HttpStatusCode.OK)
{
Content = JsonContent.Create(JsonSerializer.Serialize(new { result = callResult, count = callResult.Count }))
};
mock.When("/api/friends/friends-for-grid").Respond(req => msg); |
My SF tab components were not rendered while testing
@using Syncfusion.Blazor.Navigations
@using Syncfusion.Blazor.Inputs <SfTab OverflowMode="@OverflowMode.Popup" HeaderPlacement="@HeaderPosition.Top" LoadOn="ContentLoad.Init"> <TabItems> <TabItem> <ChildContent> <TabHeader Text="Informations" IconCss="e-tabheader-wizard-step1"></TabHeader> </ChildContent> <ContentTemplate> <div id="informations"> <SfTextBox /> <div>Hey, I should be generated</div> </div> </ContentTemplate> </TabItem> </TabItems> </SfTab> |
using System.Threading.Tasks;
using BlazorApp1.Pages; using Bunit; using NUnit.Framework; namespace TestProject { public class UnitTest1 : MyTestContext { [Test] public async Task TestMethod1() { var cut = RenderComponent<Index>(); await Task.Delay(1000); Assert.IsTrue(cut.Markup.Contains("Hey, I should be generated")); } } } |
The above solution didn't work
Hi,
Please find the attached zip file of the project for sftab unit testing.
using Bunit;
using System; using Xunit; using Xunit; using SFTabsDemoTest; using SFTabsDemo.Pages; using Syncfusion.Blazor; using System.Threading.Tasks; namespace SFTabsDemoTest { public class UnitTest1 : TestContext { public void Setup() { Services.AddSyncfusionBlazor(options => { options.IgnoreScriptIsolation = true; }); } [Fact] public async Task IsPopulate() { Setup(); var renderedComponent= RenderComponent<SFTabsDemo.Pages.Index>(); await Task.Delay(1000); //renderedComponent.MarkupMatches("Tab1"); Assert.Equal("Tab1", renderedComponent.Find("." + "e-toolbar-item").TextContent); Assert.Equal("Tab2", renderedComponent.FindAll("." + "e-toolbar-item")[1].TextContent); Assert.Contains("Hey, I should be generated", renderedComponent.Markup); //line 27 shows an error that "text is missing" because tab1 and tab2 is not rendered } } } |
Hi,
The solution has rendered the data from the first tab correctly, but for the second tab or the third tab, the data is not rendered. Also the unit tests for buttons do not work for onclick event.
<div class="col-lg-12 control-section">
<div class="e-sample-resize-container"> <SfTab @ref="Tab" CssClass="BlazorTab" LoadOn="ContentLoad.Init"> ................................................... </SfTab> </div> </div> |
using Bunit;
using System; using Xunit; using Xunit; using SFTabsDemoTest; using SFTabsDemo.Pages; using Syncfusion.Blazor; using System.Threading.Tasks; namespace SFTabsDemoTest { public class UnitTest1 : TestContext { public void Setup() { Services.AddSyncfusionBlazor(options => { options.IgnoreScriptIsolation = true; }); } [Fact] public async Task IsPopulate() { Setup(); var renderedComponent= RenderComponent<SFTabsDemo.Pages.Index>(); await Task.Delay(1000); //renderedComponent.MarkupMatches("Tab1"); Assert.Equal("Tab1", renderedComponent.Find("." + "e-toolbar-item").TextContent); Assert.Equal("Tab2", renderedComponent.FindAll("." + "e-toolbar-item")[1].TextContent); Assert.Equal("Tab3", renderedComponent.FindAll("." + "e-toolbar-item")[2].TextContent); Assert.Contains("Hey, I should be generated", renderedComponent.Markup); Assert.Contains("Weather forecast", renderedComponent.Markup); Assert.Contains("Survey Prompt", renderedComponent.Markup); //line 27 shows an error that "text is missing" because tab1 and tab2 is not rendered } } } |