Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Components/Web.JS/src/Rendering/Events/EventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export function registerCustomEventType(eventName: string, options: EventTypeOpt
throw new Error(`The event '${eventName}' is already registered.`);
}

// When aliasing a browser event, the custom event name must be different from the browser event name
// to avoid double-triggering (once for the browser event, once for the custom event wrapper)
if (options.browserEventName && eventName === options.browserEventName) {
throw new Error(`The custom event '${eventName}' cannot have the same name as its browserEventName '${options.browserEventName}'. Choose a different name for the custom event.`);
}

// If applicable, register this as an alias of the given browserEventName
if (options.browserEventName) {
const aliasGroup = browserEventNamesToAliases.get(options.browserEventName);
Expand Down
11 changes: 11 additions & 0 deletions src/Components/test/E2ETest/Tests/EventCustomArgsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ public void CanRegisterCustomEventAndSupplyComplexParams()
line => Assert.Equal("Event with IJSObjectReference received: Hello!", line));
}

[Fact]
public void ThrowsErrorWhenEventNameMatchesBrowserEventName()
{
// Attempt to register a custom event with the same name as the browser event
Browser.Exists(By.Id("register-invalid-same-name-event")).Click();

// Verify that an error is thrown
var errorMessage = Browser.Exists(By.Id("same-name-event-error")).Text;
Assert.Contains("cannot have the same name as its browserEventName", errorMessage);
}

void SendKeysSequentially(IWebElement target, string text)
{
foreach (var c in text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
onclick="Blazor.registerCustomEventType('custommouseover', { browserEventName: 'mouseover' })">
Register custom mouseover event (which has no corresponding native listener)
</button>

<button id="register-invalid-same-name-event"
onclick="try { Blazor.registerCustomEventType('scrolltop', { browserEventName: 'scrolltop' }); document.getElementById('same-name-event-error').textContent = 'No error thrown'; } catch (error) { document.getElementById('same-name-event-error').textContent = error.message; }">
Try to register event with same name as browserEventName (should throw error)
</button>
</p>

<p>
<span id="same-name-event-error"></span>
</p>

<p>
Expand Down
12 changes: 11 additions & 1 deletion src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.Mvc.Testing;

Expand Down Expand Up @@ -588,7 +589,16 @@ private static void EnsureDepsFile()
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> from the bootstrapped application.</param>
/// <returns></returns>
protected virtual TestServer CreateServer(IServiceProvider serviceProvider) => new(serviceProvider);
protected virtual TestServer CreateServer(IServiceProvider serviceProvider)
{
var options = serviceProvider.GetService<IOptions<TestServerOptions>>();
if (options is not null)
{
return new(serviceProvider, options);
}

return new(serviceProvider);
}

/// <summary>
/// Creates the <see cref="IHost"/> with the bootstrapped application in <paramref name="builder"/>.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;

namespace Microsoft.AspNetCore.Mvc.FunctionalTests;

public class CustomWebApplicationFactory : WebApplicationFactory<SimpleWebSite.Startup>
{
public Action<TestServerOptions> ConfigureOptions { get; set; }

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseTestServer(ConfigureOptions);
}
}

public class TestServerWithCustomConfigurationIntegrationTests : IClassFixture<CustomWebApplicationFactory>
{
public CustomWebApplicationFactory Factory { get; }

public TestServerWithCustomConfigurationIntegrationTests(CustomWebApplicationFactory factory)
{
Factory = factory;
}

[Fact]
public async Task ServerConfigured()
{
// Arrange
var optionsConfiguredCounter = 0;
Factory.ConfigureOptions = options =>
{
options.AllowSynchronousIO = true;
options.PreserveExecutionContext = true;
optionsConfiguredCounter++;
};

// Act
Assert.Equal(0, optionsConfiguredCounter);
Factory.StartServer();

using var client = Factory.CreateClient();

using var response = await client.GetAsync("/");

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, optionsConfiguredCounter);
Assert.True(Factory.Server.AllowSynchronousIO);
Assert.True(Factory.Server.PreserveExecutionContext);
}
}
Loading