Skip to content

Commit bffd3dd

Browse files
author
David Warwick
committed
Refactor tests to use SfDiagramComponentStub
Replaced `SfDiagramComponent` with a stub to avoid JS interop in tests, simplifying the test setup. Removed `JSRuntimeMock` as it is no longer needed. Updated `BUnitTestBase` to register the stub and adjusted `AddSyncfusionBlazor` service registration. Added a new test in `DemoTests` to verify behavior for regular users. Improved test context setup by enabling loose JS interop mode and rendering `MudPopoverProvider` separately. Cleaned up namespaces, unused directives, and redundant code for better maintainability.
1 parent 0d6c9fb commit bffd3dd

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

bUnitTests/BUnitTestBase.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
using Microsoft.AspNetCore.Components;
88
using Microsoft.AspNetCore.Components.Authorization;
99
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.DependencyInjection.Extensions;
1011
using Moq;
1112
using MudBlazor;
1213
using MudBlazor.Services;
1314
using Syncfusion.Blazor;
1415
using System;
1516
using System.Collections.Generic;
17+
using Syncfusion.Blazor.Diagram;
18+
using JwtIdentity.Client.Tests.Stubs; // adjust namespace if different
19+
1620

1721
namespace JwtIdentity.BunitTests
1822
{
@@ -32,14 +36,16 @@ public class BUnitTestBase : IDisposable
3236
protected Mock<IApiService> ApiServiceMock { get; private set; }
3337
protected Mock<IHttpClientFactory> HttpClientFactoryMock { get; private set; }
3438
protected Mock<AuthenticationStateProvider> AuthStateProviderMock { get; private set; }
35-
protected Mock<Microsoft.JSInterop.IJSRuntime> JSRuntimeMock { get; private set; }
3639
protected Mock<Microsoft.Extensions.Configuration.IConfiguration> ConfigMock { get; private set; }
3740

3841
public BUnitTestBase()
3942
{
4043
// Create test context
4144
Context = new TestContext();
4245

46+
// Substitute all SfDiagramComponent instances with our stub
47+
Context.ComponentFactories.Add<SfDiagramComponent, SfDiagramComponentStub>();
48+
4349
// Register MockNavigationManager for NavigationManager
4450
NavManager = new MockNavigationManager();
4551
Context.Services.AddSingleton<NavigationManager>(NavManager);
@@ -57,8 +63,7 @@ public BUnitTestBase()
5763
ApiServiceMock = new Mock<IApiService>();
5864
HttpClientFactoryMock = new Mock<IHttpClientFactory>();
5965
HttpClientFactoryMock.Setup(f => f.CreateClient(It.IsAny<string>())).Returns(new HttpClient());
60-
AuthStateProviderMock = new Mock<AuthenticationStateProvider>();
61-
JSRuntimeMock = new Mock<Microsoft.JSInterop.IJSRuntime>();
66+
AuthStateProviderMock = new Mock<AuthenticationStateProvider>();
6267

6368
// Register services to the test context
6469
Context.Services.AddSingleton<IAuthService>(AuthServiceMock.Object);
@@ -68,7 +73,6 @@ public BUnitTestBase()
6873
Context.Services.AddSingleton<IApiService>(ApiServiceMock.Object);
6974
Context.Services.AddSingleton<AuthenticationStateProvider>(AuthStateProviderMock.Object);
7075
Context.Services.AddSingleton<IHttpClientFactory>(HttpClientFactoryMock.Object);
71-
Context.Services.AddSingleton<Microsoft.JSInterop.IJSRuntime>(JSRuntimeMock.Object);
7276

7377
// Register a fake for CustomAuthorizationMessageHandler
7478
Context.Services.AddSingleton<JwtIdentity.Client.Services.CustomAuthorizationMessageHandler>(new FakeCustomAuthorizationMessageHandler());
@@ -78,10 +82,15 @@ public BUnitTestBase()
7882

7983
// Register all MudBlazor services (including InternalMudLocalizer) for bUnit
8084
Context.Services.AddMudServices();
81-
85+
8286
// Register Syncfusion Blazor services
83-
Context.Services.AddSyncfusionBlazor();
84-
87+
Context.Services.AddSyncfusionBlazor()
88+
.Replace(ServiceDescriptor.Transient<IComponentActivator, SfComponentActivator>());
89+
Context.Services.AddOptions();
90+
91+
Context.JSInterop.Mode = JSRuntimeMode.Loose;
92+
93+
8594
// MudPopoverProvider doesn't wrap child content, so we render it separately
8695
// This prevents "Missing <MudPopoverProvider />" errors in components that use popovers
8796
Context.RenderComponent<MudBlazor.MudPopoverProvider>();

bUnitTests/DemoTests.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
using JwtIdentity.Client.Pages.Demo;
33
using JwtIdentity.Client.Pages.Survey;
44
using JwtIdentity.Client.Services.Base;
5-
using JwtIdentity.Common.ViewModels;
65
using JwtIdentity.Common.Helpers;
6+
using JwtIdentity.Common.ViewModels;
7+
using Microsoft.AspNetCore.Components.Authorization;
8+
using Moq;
79
using NUnit.Framework;
8-
using System.Threading.Tasks;
10+
using NUnit.Framework.Legacy;
11+
using Syncfusion.Blazor.Diagram;
912
using System;
1013
using System.Collections.Generic;
1114
using System.Linq;
1215
using System.Security.Claims;
13-
using Microsoft.AspNetCore.Components.Authorization;
14-
using Moq;
16+
using System.Threading.Tasks;
1517

1618
namespace JwtIdentity.BunitTests
1719
{
@@ -196,14 +198,15 @@ public void BranchingSurveyEdit_WithDemoType_RendersSuccessfully()
196198

197199
ApiServiceMock.Setup(x => x.GetAsync<SurveyViewModel>(It.IsAny<string>()))
198200
.ReturnsAsync(survey);
201+
199202
ApiServiceMock.Setup(x => x.GetAsync<List<QuestionGroupViewModel>>(It.IsAny<string>()))
200-
.ReturnsAsync(new List<QuestionGroupViewModel>());
201-
202-
// Mock JSRuntime calls that Syncfusion Diagram component needs
203-
JSRuntimeMock.Setup(x => x.InvokeAsync<Microsoft.JSInterop.IJSObjectReference>(
204-
It.IsAny<string>(),
205-
It.IsAny<object[]>()))
206-
.ReturnsAsync(Mock.Of<Microsoft.JSInterop.IJSObjectReference>());
203+
.ReturnsAsync(new List<QuestionGroupViewModel>
204+
{
205+
new QuestionGroupViewModel
206+
{
207+
Id = 1,
208+
GroupName = "Group 1" }
209+
});
207210

208211
NavManager.NavigateTo($"http://localhost/survey/branching/{surveyId}?DemoType=branching");
209212

@@ -215,6 +218,7 @@ public void BranchingSurveyEdit_WithDemoType_RendersSuccessfully()
215218
Assert.That(cut.Markup, Does.Contain("Survey Branching"));
216219
}
217220

221+
218222
[Test]
219223
public void EditSurvey_RegularUser_DoesNotShowDemoElements()
220224
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Syncfusion.Blazor.Diagram;
2+
3+
namespace JwtIdentity.Client.Tests.Stubs
4+
{
5+
// This stub inherits from SfDiagramComponent so:
6+
// - @ref="diagram" still works (type is assignable)
7+
// - All the same parameters still exist
8+
// - But we skip the JS-heavy lifecycle
9+
public class SfDiagramComponentStub : SfDiagramComponent
10+
{
11+
// This is where the real component calls DomUtil.MeasureBounds etc.
12+
// We override it and *do not* call base to avoid JS interop.
13+
protected override Task OnAfterRenderAsync(bool firstRender)
14+
{
15+
return Task.CompletedTask;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)