Skip to content

Commit b71b82a

Browse files
csharpfritzCopilot
andcommitted
fix: register ClientScriptShim in bUnit tests + fix PostBack demo URL
- Add ClientScriptShim to DI in all WebFormsPageBase test files - Set JSInterop.Mode = JSRuntimeMode.Loose for postback JS calls - Fix Playwright PostBackTests URL: /postback-demo -> /ControlSamples/PostBackDemo - Update BlazorWebFormsTestContext shared base with ClientScriptShim - Update IsPostBackTests with ClientScriptShim + loose JS mode - All 2,823 tests pass on net10.0 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 09d3236 commit b71b82a

9 files changed

Lines changed: 22 additions & 7 deletions

File tree

samples/AfterBlazorServerSide.Tests/Migration/PostBackTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task PostBack_Button_TriggersPostBackEvent()
2828

2929
try
3030
{
31-
await page.GotoAsync($"{_fixture.BaseUrl}/postback-demo", new PageGotoOptions
31+
await page.GotoAsync($"{_fixture.BaseUrl}/ControlSamples/PostBackDemo", new PageGotoOptions
3232
{
3333
WaitUntil = WaitUntilState.NetworkIdle,
3434
Timeout = 30000
@@ -76,7 +76,7 @@ public async Task PostBackHyperlink_Click_TriggersPostBackEvent()
7676

7777
try
7878
{
79-
await page.GotoAsync($"{_fixture.BaseUrl}/postback-demo", new PageGotoOptions
79+
await page.GotoAsync($"{_fixture.BaseUrl}/ControlSamples/PostBackDemo", new PageGotoOptions
8080
{
8181
WaitUntil = WaitUntilState.NetworkIdle,
8282
Timeout = 30000
@@ -124,7 +124,7 @@ public async Task ScriptManager_GetCurrent_RegistersStartupScript()
124124

125125
try
126126
{
127-
await page.GotoAsync($"{_fixture.BaseUrl}/postback-demo", new PageGotoOptions
127+
await page.GotoAsync($"{_fixture.BaseUrl}/ControlSamples/PostBackDemo", new PageGotoOptions
128128
{
129129
WaitUntil = WaitUntilState.NetworkIdle,
130130
Timeout = 30000

src/BlazorWebFormsComponents.Test/BlazorWebFormsTestContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ protected BlazorWebFormsTestContext() : this(null)
3030
/// <param name="outputHelper">Optional xUnit test output helper for logging. When provided, enables logging output in test results.</param>
3131
protected BlazorWebFormsTestContext(ITestOutputHelper outputHelper)
3232
{
33-
// Configure bUnit's JSInterop to handle the OnAfterRender call
34-
// This is required because the base component calls this on first render
35-
JSInterop.SetupVoid("bwfc.Page.OnAfterRender");
33+
// Configure bUnit's JSInterop to handle all JS calls from lifecycle methods
34+
JSInterop.Mode = JSRuntimeMode.Loose;
3635

3736
// Register required services for BaseWebFormsComponent and WebFormsPageBase
3837
Services.AddSingleton<LinkGenerator>(new Mock<LinkGenerator>().Object);
@@ -41,6 +40,7 @@ protected BlazorWebFormsTestContext(ITestOutputHelper outputHelper)
4140
Services.AddMemoryCache();
4241
Services.AddScoped<CacheShim>();
4342
Services.AddScoped<ServerShim>();
43+
Services.AddScoped<ClientScriptShim>();
4444

4545
// Always register logging so ILogger<T> is resolvable.
4646
// Add xUnit output sink when a test output helper is provided.

src/BlazorWebFormsComponents.Test/IsPostBackTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class IsPostBackTests : IDisposable
3434
public IsPostBackTests()
3535
{
3636
_ctx = new BunitContext();
37-
_ctx.JSInterop.SetupVoid("bwfc.Page.OnAfterRender");
37+
_ctx.JSInterop.Mode = JSRuntimeMode.Loose;
3838
_ctx.Services.AddSingleton<LinkGenerator>(new Mock<LinkGenerator>().Object);
3939
_ctx.Services.AddSingleton<IDataProtectionProvider>(new EphemeralDataProtectionProvider());
4040
_ctx.Services.AddLogging();
@@ -43,6 +43,7 @@ public IsPostBackTests()
4343
_ctx.Services.AddMemoryCache();
4444
_ctx.Services.AddScoped<CacheShim>();
4545
_ctx.Services.AddScoped<ServerShim>();
46+
_ctx.Services.AddScoped<ClientScriptShim>();
4647
}
4748

4849
private static IWebHostEnvironment CreateMockWebHostEnv()

src/BlazorWebFormsComponents.Test/WebFormsPageBase/GetRouteUrlTests.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373

7474
private (IRenderedComponent<TestPage> cut, CapturingLinkGenerator linkGen) RenderWithCapturingLinkGen()
7575
{
76+
JSInterop.Mode = JSRuntimeMode.Loose;
7677
var linkGen = new CapturingLinkGenerator();
7778
Services.AddSingleton<LinkGenerator>(linkGen);
7879
var mockHttpAccessor = new Mock<IHttpContextAccessor>();
@@ -94,6 +95,7 @@
9495
Services.AddMemoryCache();
9596
Services.AddScoped<CacheShim>();
9697
Services.AddScoped<ServerShim>();
98+
Services.AddScoped<ClientScriptShim>();
9799
}
98100

99101
[Fact]

src/BlazorWebFormsComponents.Test/WebFormsPageBase/RenderModeGuardTests.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
private IRenderedComponent<TestPage> RenderWithHttpContext()
3131
{
32+
JSInterop.Mode = JSRuntimeMode.Loose;
3233
var mockHttpAccessor = new Mock<IHttpContextAccessor>();
3334
mockHttpAccessor.Setup(x => x.HttpContext).Returns(new DefaultHttpContext());
3435
Services.AddSingleton<IHttpContextAccessor>(mockHttpAccessor.Object);
@@ -42,6 +43,7 @@
4243

4344
private IRenderedComponent<TestPage> RenderWithoutHttpContext()
4445
{
46+
JSInterop.Mode = JSRuntimeMode.Loose;
4547
var mockHttpAccessor = new Mock<IHttpContextAccessor>();
4648
mockHttpAccessor.Setup(x => x.HttpContext).Returns((HttpContext)null);
4749
Services.AddSingleton<IHttpContextAccessor>(mockHttpAccessor.Object);
@@ -62,6 +64,7 @@
6264
Services.AddMemoryCache();
6365
Services.AddScoped<CacheShim>();
6466
Services.AddScoped<ServerShim>();
67+
Services.AddScoped<ClientScriptShim>();
6568
}
6669

6770
[Fact]

src/BlazorWebFormsComponents.Test/WebFormsPageBase/RequestShimTests.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
private IRenderedComponent<TestPage> RenderWithMockNav(string uri = null)
2424
{
25+
JSInterop.Mode = JSRuntimeMode.Loose;
2526
var nav = new MockNavigationManager(initialUri: uri ?? "http://localhost/");
2627
Services.AddSingleton<Microsoft.AspNetCore.Components.NavigationManager>(nav);
2728
var mockAccessor = new Mock<IHttpContextAccessor>();
@@ -43,6 +44,7 @@
4344
Services.AddMemoryCache();
4445
Services.AddScoped<CacheShim>();
4546
Services.AddScoped<ServerShim>();
47+
Services.AddScoped<ClientScriptShim>();
4648
}
4749

4850
[Fact]
@@ -118,6 +120,7 @@
118120
[Fact]
119121
public void Cookies_WithHttpContext_ReturnsRealCookies()
120122
{
123+
JSInterop.Mode = JSRuntimeMode.Loose;
121124
// Set up a real HttpContext with cookies
122125
var httpContext = new DefaultHttpContext();
123126
httpContext.Request.Headers["Cookie"] = "session=abc123";

src/BlazorWebFormsComponents.Test/WebFormsPageBase/ResponseShimTests.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
private (IRenderedComponent<TestPage> cut, MockNavigationManager nav) RenderWithMockNav()
2323
{
24+
JSInterop.Mode = JSRuntimeMode.Loose;
2425
var mockNav = new MockNavigationManager();
2526
Services.AddSingleton<Microsoft.AspNetCore.Components.NavigationManager>(mockNav);
2627
Services.AddLogging();
@@ -33,6 +34,7 @@
3334
Services.AddMemoryCache();
3435
Services.AddScoped<CacheShim>();
3536
Services.AddScoped<ServerShim>();
37+
Services.AddScoped<ClientScriptShim>();
3638
return (Render<TestPage>(), mockNav);
3739
}
3840

src/BlazorWebFormsComponents.Test/WebFormsPageBase/ViewStateTests.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
private IRenderedComponent<TestPage> RenderTestPage()
2222
{
23+
JSInterop.Mode = JSRuntimeMode.Loose;
2324
Services.AddLogging();
2425
Services.AddScoped<SessionShim>();
2526
Services.AddScoped<IPageService, PageService>();
@@ -36,6 +37,7 @@
3637
Services.AddMemoryCache();
3738
Services.AddScoped<CacheShim>();
3839
Services.AddScoped<ServerShim>();
40+
Services.AddScoped<ClientScriptShim>();
3941
}
4042

4143
[Fact]

src/BlazorWebFormsComponents.Test/WebFormsPageBase/WebFormsPageBaseTests.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
private IRenderedComponent<TestPage> RenderTestPage()
2424
{
25+
JSInterop.Mode = JSRuntimeMode.Loose;
2526
Services.AddLogging();
2627
Services.AddScoped<SessionShim>();
2728
Services.AddScoped<IPageService, PageService>();
@@ -38,6 +39,7 @@
3839
Services.AddMemoryCache();
3940
Services.AddScoped<CacheShim>();
4041
Services.AddScoped<ServerShim>();
42+
Services.AddScoped<ClientScriptShim>();
4143
}
4244

4345
[Fact]

0 commit comments

Comments
 (0)