-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
403f025
Add default JsRuntime
Siphonophora 653fb5a
Update src/Mocking/JSInterop/DefaultJsRuntime.cs
Siphonophora 89ad90b
Response to review. Add custom exception and code cleanup
Siphonophora 116d0e0
Merge branch 'dev' into Add-default-jsruntime
Siphonophora ca2af8a
Remove unneded method
Siphonophora f784b35
Update src/Mocking/JSInterop/MissingMockJsRuntimeException.cs
Siphonophora 2b6f92a
Update src/Mocking/JSInterop/MissingMockJsRuntimeException.cs
Siphonophora 0584252
Update src/Mocking/JSInterop/MissingMockJsRuntimeException.cs
Siphonophora 6f8d735
Update src/Mocking/JSInterop/MissingMockJsRuntimeException.cs
Siphonophora 6bd0a53
Update src/Mocking/JSInterop/MissingMockJsRuntimeException.cs
Siphonophora 4e7ea27
Update src/Mocking/JSInterop/MissingMockJsRuntimeException.cs
Siphonophora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1 +1 @@ | ||
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Egil.RazorComponents.Testing.Library.Tests")] | ||
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Egil.RazorComponents.Testing.Library.Tests")] |
This file contains hidden or 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,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"; | ||
} | ||
} | ||
} |
This file contains hidden or 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,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); | ||
} | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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,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>(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.