Skip to content

Add default JsRuntime #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 22, 2020
2 changes: 1 addition & 1 deletion src/Assembly.cs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Egil.RazorComponents.Testing.Library.Tests")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Egil.RazorComponents.Testing.Library.Tests")]
41 changes: 41 additions & 0 deletions src/Mocking/JSInterop/MissingMockJsRuntimeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;

namespace Egil.RazorComponents.Testing
{
/// <summary>
/// Exception use to indicate that a MockJsRuntime is required by a test
/// but was not provided.
/// </summary>
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "<Pending>")]
public class MissingMockJsRuntimeException : Exception
{
/// <summary>
/// Identifer string used in the JSInvoke method.
/// </summary>
public string Identifier { get; }

/// <summary>
/// Arguments passed to the JSInvoke method.
/// </summary>
public IReadOnlyList<object> Arguments { get; }

/// <summary>
/// Creates a new instance of the <see cref="MissingMockJsRuntimeException"/>
/// with the arguments used in the invocation.
/// </summary>
/// <param name="identifier">The identifer used in the invocation.</param>
/// <param name="arguments">The args used in the invocation, if any</param>
public MissingMockJsRuntimeException(string identifier, object[] arguments)
: base($"This test requires a IJsRuntime to be supplied, because the component under test invokes the IJsRuntime during the test. The invoked method is '{identifier}' and the invocation arguments are stored in the {nameof(Arguments)} property of this exception. Guidance on mocking the IJsRuntime is available in the testing library's Wiki.")
{
Identifier = identifier;
Arguments = arguments;
HelpLink = "https://github.com/egil/razor-components-testing-library/wiki/Mocking-JsRuntime";
}
}
}
26 changes: 26 additions & 0 deletions src/Mocking/JSInterop/PlaceholderJsRuntime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.JSInterop;

namespace Egil.RazorComponents.Testing.Mocking.JSInterop
{
/// <summary>
/// This JsRuntime is used to provide users with helpful exceptions if they fail to provide a mock when required.
/// </summary>
internal class PlaceholderJsRuntime : IJSRuntime
{
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args)
{
throw new MissingMockJsRuntimeException(identifier, args);
}

public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args)
{
throw new MissingMockJsRuntimeException(identifier, args);
}
}
}
9 changes: 8 additions & 1 deletion src/TestServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Egil.RazorComponents.Testing.Mocking.JSInterop;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using System;
using System.Diagnostics.CodeAnalysis;

Expand All @@ -13,6 +15,11 @@ public sealed class TestServiceProvider : IServiceProvider, IDisposable
private readonly ServiceCollection _serviceCollection = new ServiceCollection();
private ServiceProvider? _serviceProvider;

public TestServiceProvider()
{
_serviceCollection.AddSingleton<IJSRuntime, PlaceholderJsRuntime>();
}

/// <summary>
/// Gets whether this <see cref="TestServiceProvider"/> has been initialized, and
/// no longer will accept calls to the <c>AddService</c>'s methods.
Expand Down
33 changes: 33 additions & 0 deletions tests/TestServiceProviderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Egil.RazorComponents.Testing.EventDispatchExtensions;
using Egil.RazorComponents.Testing.Extensions;
using Egil.RazorComponents.Testing.Mocking.JSInterop;
using Egil.RazorComponents.Testing.SampleComponents;
using Egil.RazorComponents.Testing.SampleComponents.Data;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace Egil.RazorComponents.Testing
{
public class TestServiceProviderTest : ComponentTestFixture
{
[Fact(DisplayName = "The test service provider should register a placeholder IJSRuntime " +
"which throws exceptions")]
public void Test001()
{
var ex = Assert.Throws<AggregateException>(() => RenderComponent<SimpleWithJsRuntimeDep>());
Assert.True(ex?.InnerException is MissingMockJsRuntimeException);
}

[Fact(DisplayName = "The placeholder IJSRuntime is overriden by a supplied mock and does not throw")]
public void Test002()
{
Services.AddMockJsRuntime();

RenderComponent<SimpleWithJsRuntimeDep>();
}
}
}