-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes when multiple nuget packages or project references contain Blazored.LocalStorage and call AddBlazoredLocalStorage(). No error occurs if it is added more than once, but the services are only needed once.
- Loading branch information
Showing
2 changed files
with
106 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...s/Blazored.LocalStorage.Tests/LocalStorageServiceTests/ServiceCollectionExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Linq; | ||
using Blazored.LocalStorage.Serialization; | ||
using Blazored.LocalStorage.StorageOptions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Xunit; | ||
|
||
namespace Blazored.LocalStorage.Tests.LocalStorageServiceTests; | ||
|
||
public class ServiceCollectionExtensionsTest | ||
{ | ||
[Fact] | ||
public void Scoped() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
services.AddBlazoredLocalStorage(); | ||
services.AddBlazoredLocalStorage(); | ||
services.AddBlazoredLocalStorage(); | ||
services.AddBlazoredLocalStorage(); | ||
|
||
// Assert | ||
AssertEqual(services, typeof(IJsonSerializer), typeof(SystemTextJsonSerializer), ServiceLifetime.Scoped); | ||
AssertEqual(services, typeof(IStorageProvider), typeof(BrowserStorageProvider), ServiceLifetime.Scoped); | ||
AssertEqual(services, typeof(ILocalStorageService), typeof(LocalStorageService), ServiceLifetime.Scoped); | ||
AssertEqual(services, typeof(ISyncLocalStorageService), typeof(LocalStorageService), ServiceLifetime.Scoped); | ||
AssertEqualConfigureOptions(services, typeof(IConfigureOptions<LocalStorageOptions>), ServiceLifetime.Singleton); | ||
} | ||
|
||
[Fact] | ||
public void Singleton() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
services.AddBlazoredLocalStorageAsSingleton(); | ||
services.AddBlazoredLocalStorageAsSingleton(); | ||
services.AddBlazoredLocalStorageAsSingleton(); | ||
services.AddBlazoredLocalStorageAsSingleton(); | ||
|
||
// Assert | ||
AssertEqual(services, typeof(IJsonSerializer), typeof(SystemTextJsonSerializer), ServiceLifetime.Singleton); | ||
AssertEqual(services, typeof(IStorageProvider), typeof(BrowserStorageProvider), ServiceLifetime.Singleton); | ||
AssertEqual(services, typeof(ILocalStorageService), typeof(LocalStorageService), ServiceLifetime.Singleton); | ||
AssertEqual(services, typeof(ISyncLocalStorageService), typeof(LocalStorageService), ServiceLifetime.Singleton); | ||
AssertEqualConfigureOptions(services, typeof(IConfigureOptions<LocalStorageOptions>), ServiceLifetime.Singleton); | ||
} | ||
|
||
|
||
static void AssertEqual( | ||
IServiceCollection services, | ||
Type serviceType, | ||
Type implementationType, | ||
ServiceLifetime serviceLifetime | ||
) | ||
{ | ||
var descriptors = services.Where(serviceDescriptor => serviceDescriptor.ServiceType == serviceType).ToArray(); | ||
Assert.Single(descriptors); | ||
var descriptor = descriptors.Single(); | ||
Assert.Equal(serviceType, descriptor.ServiceType); | ||
Assert.Equal(implementationType, descriptor.ImplementationType); | ||
Assert.Equal(serviceLifetime, descriptor.Lifetime); | ||
} | ||
|
||
static void AssertEqualConfigureOptions( | ||
IServiceCollection services, | ||
Type serviceType, | ||
ServiceLifetime serviceLifetime | ||
) | ||
{ | ||
var descriptors = services.Where(serviceDescriptor => serviceDescriptor.ServiceType == serviceType).ToArray(); | ||
Assert.Single(descriptors); | ||
var descriptor = descriptors.Single(); | ||
Assert.Equal(serviceType, descriptor.ServiceType); | ||
Assert.Equal(serviceLifetime, descriptor.Lifetime); | ||
} | ||
} |