Skip to content

Commit

Permalink
Fix tests by supporting TryAddCascadingValue
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Aug 22, 2023
1 parent a27e4c4 commit fb52e1a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -50,4 +51,48 @@ public static IServiceCollection AddCascadingValue<TValue>(
public static IServiceCollection AddCascadingValue<TValue>(
this IServiceCollection serviceCollection, Func<IServiceProvider, CascadingValueSource<TValue>> sourceFactory)
=> serviceCollection.AddScoped<ICascadingValueSupplier>(sourceFactory);

/// <summary>
/// Adds a cascading value to the <paramref name="serviceCollection"/>, if none is already registered
/// with the value type. This is equivalent to having a fixed <see cref="CascadingValue{TValue}"/> at
/// the root of the component hierarchy.
/// </summary>
/// <typeparam name="TValue">The value type.</typeparam>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="valueFactory">A callback that supplies a fixed value within each service provider scope.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static void TryAddCascadingValue<TValue>(
this IServiceCollection serviceCollection, Func<IServiceProvider, TValue> valueFactory)
=> serviceCollection.TryAddScoped<ICascadingValueSupplier>(sp => new CascadingValueSource<TValue>(() => valueFactory(sp), isFixed: true));

/// <summary>
/// Adds a cascading value to the <paramref name="serviceCollection"/>, if none is already registered
/// with the value type, regardless of the <paramref name="name"/>. This is equivalent to having a fixed
/// <see cref="CascadingValue{TValue}"/> at the root of the component hierarchy.
/// </summary>
/// <typeparam name="TValue">The value type.</typeparam>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="name">A name for the cascading value. If set, <see cref="CascadingParameterAttribute"/> can be configured to match based on this name.</param>
/// <param name="valueFactory">A callback that supplies a fixed value within each service provider scope.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static void TryAddCascadingValue<TValue>(
this IServiceCollection serviceCollection, string name, Func<IServiceProvider, TValue> valueFactory)
=> serviceCollection.TryAddScoped<ICascadingValueSupplier>(sp => new CascadingValueSource<TValue>(name, () => valueFactory(sp), isFixed: true));

/// <summary>
/// Adds a cascading value to the <paramref name="serviceCollection"/>, if none is already registered
/// with the value type. This is equivalent to having a fixed <see cref="CascadingValue{TValue}"/> at
/// the root of the component hierarchy.
///
/// With this overload, you can supply a <see cref="CascadingValueSource{TValue}"/> which allows you
/// to notify about updates to the value later, causing recipients to re-render. This overload should
/// only be used if you plan to update the value dynamically.
/// </summary>
/// <typeparam name="TValue">The value type.</typeparam>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="sourceFactory">A callback that supplies a <see cref="CascadingValueSource{TValue}"/> within each service provider scope.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static void TryAddCascadingValue<TValue>(
this IServiceCollection serviceCollection, Func<IServiceProvider, CascadingValueSource<TValue>> sourceFactory)
=> serviceCollection.TryAddScoped<ICascadingValueSupplier>(sourceFactory);
}
3 changes: 3 additions & 0 deletions src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ static Microsoft.AspNetCore.Components.SupplyParameterFromQueryProviderServiceCo
static Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.AddCascadingValue<TValue>(this Microsoft.Extensions.DependencyInjection.IServiceCollection! serviceCollection, string! name, System.Func<System.IServiceProvider!, TValue>! valueFactory) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.AddCascadingValue<TValue>(this Microsoft.Extensions.DependencyInjection.IServiceCollection! serviceCollection, System.Func<System.IServiceProvider!, Microsoft.AspNetCore.Components.CascadingValueSource<TValue>!>! sourceFactory) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.AddCascadingValue<TValue>(this Microsoft.Extensions.DependencyInjection.IServiceCollection! serviceCollection, System.Func<System.IServiceProvider!, TValue>! valueFactory) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
static Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.TryAddCascadingValue<TValue>(this Microsoft.Extensions.DependencyInjection.IServiceCollection! serviceCollection, string! name, System.Func<System.IServiceProvider!, TValue>! valueFactory) -> void
static Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.TryAddCascadingValue<TValue>(this Microsoft.Extensions.DependencyInjection.IServiceCollection! serviceCollection, System.Func<System.IServiceProvider!, Microsoft.AspNetCore.Components.CascadingValueSource<TValue>!>! sourceFactory) -> void
static Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.TryAddCascadingValue<TValue>(this Microsoft.Extensions.DependencyInjection.IServiceCollection! serviceCollection, System.Func<System.IServiceProvider!, TValue>! valueFactory) -> void
virtual Microsoft.AspNetCore.Components.NavigationManager.Refresh(bool forceReload = false) -> void
virtual Microsoft.AspNetCore.Components.Rendering.ComponentState.DisposeAsync() -> System.Threading.Tasks.ValueTask
virtual Microsoft.AspNetCore.Components.RenderTree.Renderer.AddPendingTask(Microsoft.AspNetCore.Components.Rendering.ComponentState? componentState, System.Threading.Tasks.Task! task) -> void
Expand Down
42 changes: 42 additions & 0 deletions src/Components/Components/test/CascadingParameterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,48 @@ public void OmitsSingleDeliveryCascadingParametersWhenUpdatingDirectParameters()
});
}

[Fact]
public void CanUseTryAddPatternForCascadingValuesInServiceCollection_ValueFactory()
{
// Arrange
var services = new ServiceCollection();

// Act
services.TryAddCascadingValue(_ => new SingleDeliveryValue("first"));
services.TryAddCascadingValue(_ => new SingleDeliveryValue("second"));

// Assert
Assert.Single(services);
}

[Fact]
public void CanUseTryAddPatternForCascadingValuesInServiceCollection_NamedValueFactory()
{
// Arrange
var services = new ServiceCollection();

// Act
services.TryAddCascadingValue("Name1", _ => new SingleDeliveryValue("first"));
services.TryAddCascadingValue("Name2", _ => new SingleDeliveryValue("second"));

// Assert
Assert.Single(services);
}

[Fact]
public void CanUseTryAddPatternForCascadingValuesInServiceCollection_CascadingValueSource()
{
// Arrange
var services = new ServiceCollection();

// Act
services.TryAddCascadingValue(_ => new CascadingValueSource<SingleDeliveryValue>("Name1", new SingleDeliveryValue("first"), false));
services.TryAddCascadingValue(_ => new CascadingValueSource<SingleDeliveryValue>("Name2", new SingleDeliveryValue("second"), false));

// Assert
Assert.Single(services);
}

private class SingleDeliveryValue(string text)
{
public string Text => text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection
services.TryAddScoped<EndpointRoutingStateProvider>();
services.TryAddScoped<IRoutingStateProvider>(sp => sp.GetRequiredService<EndpointRoutingStateProvider>());
services.AddSupplyValueFromQueryProvider();
services.AddCascadingValue(sp => sp.GetRequiredService<EndpointHtmlRenderer>().HttpContext);
services.TryAddCascadingValue(sp => sp.GetRequiredService<EndpointHtmlRenderer>().HttpContext);

// Form handling
services.AddSupplyValueFromFormProvider();
Expand Down

0 comments on commit fb52e1a

Please sign in to comment.