diff --git a/CHANGELOG.md b/CHANGELOG.md index 6857dc96d..9fe85854e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ List of new features. List of changes in existing functionality. - `TestContextBase.Dispose` made virtual to allow inheritor's to override it. By [@SimonCropp](https://github.com/SimonCropp) in [#137](https://github.com/egil/bunit/pull/137). +- Changed naming convention for JSMock feature. All classes and methods containing `Js` (meaning JavaScript) renamed to `JS`. By [yourilima](https://github.com/yourilima) in [#150](https://github.com/egil/bUnit/pull/150) ### Deprecated List of soon-to-be removed features. diff --git a/docs/site/docs/csharp-test-examples.md b/docs/site/docs/csharp-test-examples.md index 545695cab..d90f2fb34 100644 --- a/docs/site/docs/csharp-test-examples.md +++ b/docs/site/docs/csharp-test-examples.md @@ -396,11 +396,11 @@ public class ThemedButtonTest : ComponentTestFixture - `Test002` above uses the `CascadingValue(object value)` helper method to pass an **unnamed** cascading value to the CUT. - `Test003` above demonstrates how multiple (named) cascading values can be passed to a component under test. -## Testing components that use on `IJsRuntime` +## Testing components that use on `IJSRuntime` It is not uncommon to have components use Blazor's JSInterop functionality to call JavaScript, e.g. after first render. -To make it easy to mock calls to JavaScript, the library comes with a `IJsRuntime` mocking helper, that allows you to specify return how JSInterop calls should be handled, and to verify that they have happened. +To make it easy to mock calls to JavaScript, the library comes with a `IJSRuntime` mocking helper, that allows you to specify return how JSInterop calls should be handled, and to verify that they have happened. If you have more complex mocking needs, you could look to frameworks like [Moq](https://github.com/Moq) or [JustMock Lite](https://github.com/telerik/JustMockLite), which both work nicely with bUnit. @@ -441,10 +441,10 @@ public class WikiSearchTest : ComponentTestFixture public void Test001() { // Arrange - // Registered the MockJsRuntime in "Loose" mode with the service provider used when rendering components. - // JsRuntimeMockMode.Loose is the default. It configures the mock to just return the default + // Registered the MockJSRuntime in "Loose" mode with the service provider used when rendering components. + // JSRuntimeMockMode.Loose is the default. It configures the mock to just return the default // value for whatever is requested in a InvokeAsync call if no call has explicitly been set up. - var jsMock = Services.AddMockJsRuntime(); + var jsMock = Services.AddMockJSRuntime(); // Act - render the WikiSearch component var cut = RenderComponent(); @@ -460,10 +460,10 @@ public class WikiSearchTest : ComponentTestFixture public void Test002() { // Arrange - // Registered the MockJsRuntime in "strict" mode with the service provider used when rendering components. - // JsRuntimeMockMode.Strict mode configures the mock to throw an error if it receives an InvokeAsync call + // Registered the MockJSRuntime in "strict" mode with the service provider used when rendering components. + // JSRuntimeMockMode.Strict mode configures the mock to throw an error if it receives an InvokeAsync call // it has not been set up to handle. - var jsMock = Services.AddMockJsRuntime(JsRuntimeMockMode.Strict); + var jsMock = Services.AddMockJSRuntime(JSRuntimeMockMode.Strict); // Set up the mock to handle the expected call var expectedSearchResult = "SEARCH RESULT"; @@ -486,7 +486,7 @@ public class WikiSearchTest : ComponentTestFixture } ``` -- `Test001` just injects the mock in "Loose" mode. It means it will only returns a `default(TValue)` for calls to `InvokeAsync(...)` it receives. This allows us to test components that expects a `IJsRuntime` to be injected, but where the test we want to perform isn't dependent on it providing any specific return value. +- `Test001` just injects the mock in "Loose" mode. It means it will only returns a `default(TValue)` for calls to `InvokeAsync(...)` it receives. This allows us to test components that expects a `IJSRuntime` to be injected, but where the test we want to perform isn't dependent on it providing any specific return value. In "Loose" mode it is still possible to call `VerifyInvoke(identifier)` and assert against the expected invocation. @@ -497,7 +497,7 @@ public class WikiSearchTest : ComponentTestFixture ### Verifying element references passed to InvokeAsync -If you want to verify that a element reference (`ElementReference`) passed to a IJsRuntime.InvokeAsync call is references the expected DOM element, you can do so with the `ShouldBeElementReferenceTo()` assert helper. +If you want to verify that a element reference (`ElementReference`) passed to a IJSRuntime.InvokeAsync call is references the expected DOM element, you can do so with the `ShouldBeElementReferenceTo()` assert helper. For example, consider the [FocussingInput.razor](https://github.com/egil/razor-components-testing-library/tree/main/sample/src/Components/FocussingInput.razor) component, which looks like this: @@ -530,8 +530,8 @@ public class FocussingInputTest : ComponentTestFixture [Fact(DisplayName = "After first render, the new input field has focus")] public void Test001() { - // Arrange - add the IJsRuntime mock - var jsRtMock = Services.AddMockJsRuntime(); + // Arrange - add the IJSRuntime mock + var jsRtMock = Services.AddMockJSRuntime(); // Act - render the FocussingInput component, causing // the OnAfterRender(firstRender: true) to be called diff --git a/sample/tests/RazorTestComponents/Components/AlertRazorTest.razor b/sample/tests/RazorTestComponents/Components/AlertRazorTest.razor index 4ca31f87d..c12c71a3b 100644 --- a/sample/tests/RazorTestComponents/Components/AlertRazorTest.razor +++ b/sample/tests/RazorTestComponents/Components/AlertRazorTest.razor @@ -1,11 +1,11 @@ @inherits TestComponentBase @code { - MockJsRuntimeInvokeHandler MockJsRuntime { get; set; } = default!; + MockJSRuntimeInvokeHandler MockJSRuntime { get; set; } = default!; void Setup(Fixture fixture) { - MockJsRuntime = fixture.Services.AddMockJsRuntime(); + MockJSRuntime = fixture.Services.AddMockJSRuntime(); } } @@ -190,7 +190,7 @@ void Test007(Fixture fixture) { // arrange - var plannedInvocation = MockJsRuntime.Setup("window.transitionFinished"); + var plannedInvocation = MockJSRuntime.Setup("window.transitionFinished"); var cut = fixture.GetComponentUnderTest(); // Act diff --git a/sample/tests/RazorTestComponents/Components/TodoListTest.razor b/sample/tests/RazorTestComponents/Components/TodoListTest.razor index c4b5156b5..6aa0a9965 100644 --- a/sample/tests/RazorTestComponents/Components/TodoListTest.razor +++ b/sample/tests/RazorTestComponents/Components/TodoListTest.razor @@ -1,6 +1,6 @@ @inherits TestComponentBase - @@ -36,7 +36,7 @@ } - @@ -65,7 +65,7 @@ } - @@ -101,14 +101,14 @@ @code { - MockJsRuntimeInvokeHandler jsRtMock = default!; + MockJSRuntimeInvokeHandler jsRtMock = default!; Todo? createdTodo; void OnAddingTodoHandler(Todo todo) => createdTodo = todo; void Setup(Fixture fixture) { - jsRtMock = fixture.Services.AddMockJsRuntime(); + jsRtMock = fixture.Services.AddMockJSRuntime(); } } diff --git a/sample/tests/SampleApp.Tests.csproj b/sample/tests/SampleApp.Tests.csproj index 7b8248d53..14806245e 100644 --- a/sample/tests/SampleApp.Tests.csproj +++ b/sample/tests/SampleApp.Tests.csproj @@ -11,7 +11,7 @@ - + diff --git a/sample/tests/SnapshotTests/AlertSnapshotTest.razor b/sample/tests/SnapshotTests/AlertSnapshotTest.razor index 19665fe04..7e692430e 100644 --- a/sample/tests/SnapshotTests/AlertSnapshotTest.razor +++ b/sample/tests/SnapshotTests/AlertSnapshotTest.razor @@ -1,6 +1,6 @@ @inherits TestComponentBase - diff --git a/sample/tests/SnapshotTests/TodoListTest.razor b/sample/tests/SnapshotTests/TodoListTest.razor index 4d4e7ddd8..cb7a61560 100644 --- a/sample/tests/SnapshotTests/TodoListTest.razor +++ b/sample/tests/SnapshotTests/TodoListTest.razor @@ -1,7 +1,7 @@ @inherits TestComponentBase + Setup="(test) => test.Services.AddMockJSRuntime()"> diff --git a/sample/tests/Tests/Components/AlertTest.cs b/sample/tests/Tests/Components/AlertTest.cs index 335ba8ee0..0a4de2d19 100644 --- a/sample/tests/Tests/Components/AlertTest.cs +++ b/sample/tests/Tests/Components/AlertTest.cs @@ -1,6 +1,6 @@ using System; using System.Threading.Tasks; -using Bunit.Mocking.JSInterop; +using Bunit.TestDoubles.JSInterop; using SampleApp.Components; using SampleApp.Data; using Microsoft.AspNetCore.Authentication; @@ -13,11 +13,11 @@ namespace SampleApp.Tests.Components { public class AlertTest2 : TestContext { - MockJsRuntimeInvokeHandler MockJsRuntime { get; } + MockJSRuntimeInvokeHandler MockJSRuntime { get; } public AlertTest2() { - MockJsRuntime = Services.AddMockJsRuntime(); + MockJSRuntime = Services.AddMockJSRuntime(); } [Fact(DisplayName = "Given no parameters, " + @@ -156,8 +156,8 @@ public void Test006() public void Test() { // Arrange - var mockJsRuntime = Services.AddMockJsRuntime(); - var plannedInvocation = mockJsRuntime.Setup("window.transitionFinished"); + var mockJSRuntime = Services.AddMockJSRuntime(); + var plannedInvocation = mockJSRuntime.Setup("window.transitionFinished"); DismissingEventArgs? dismissingEvent = default; Alert? dismissedAlert = default; @@ -191,7 +191,7 @@ public void Test() public void Test007() { // Arrange - var plannedInvocation = MockJsRuntime.Setup("window.transitionFinished"); + var plannedInvocation = MockJSRuntime.Setup("window.transitionFinished"); var cut = RenderComponent(); // Act - click the button diff --git a/sample/tests/Tests/Components/FocussingInputTest.cs b/sample/tests/Tests/Components/FocussingInputTest.cs index 8d2f186e6..2b379a914 100644 --- a/sample/tests/Tests/Components/FocussingInputTest.cs +++ b/sample/tests/Tests/Components/FocussingInputTest.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using Bunit.Mocking.JSInterop; +using Bunit.TestDoubles.JSInterop; using SampleApp.Components; using Xunit; using Bunit; @@ -16,8 +16,8 @@ public class FocussingInputTest : TestContext [Fact(DisplayName = "After first render, the new input field has focus")] public void Test001() { - // Arrange - add the IJsRuntime mock - var jsRtMock = Services.AddMockJsRuntime(); + // Arrange - add the IJSRuntime mock + var jsRtMock = Services.AddMockJSRuntime(); // Act - render the FocussingInput component, causing // the OnAfterRender(firstRender: true) to be called diff --git a/sample/tests/Tests/Components/TodoListTest.cs b/sample/tests/Tests/Components/TodoListTest.cs index c0878f252..4c8114e59 100644 --- a/sample/tests/Tests/Components/TodoListTest.cs +++ b/sample/tests/Tests/Components/TodoListTest.cs @@ -1,6 +1,6 @@ using Shouldly; using AngleSharp.Dom; -using Bunit.Mocking.JSInterop; +using Bunit.TestDoubles.JSInterop; using SampleApp.Components; using SampleApp.Data; using Microsoft.AspNetCore.Components; @@ -34,7 +34,7 @@ private string GetExpectedHtml(string label = "Task description", string itemsHt public void Test001() { // arrange - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); // act var cut = RenderComponent(); @@ -47,7 +47,7 @@ public void Test001() public void Test002() { // arrange - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); var label = "hello world"; // act @@ -61,7 +61,7 @@ public void Test002() public void Test003() { // arrange - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); RenderFragment itemTemplate = todo => builder => builder.AddMarkupContent(0, $"
  • {todo.Id}
  • "); var items = new[] { new Todo { Id = 42 }, new Todo { Id = 1337 } }; @@ -80,7 +80,7 @@ public void Test003() public void Test004() { // arrange - var jsRtMock = Services.AddMockJsRuntime(); + var jsRtMock = Services.AddMockJSRuntime(); // act var cut = RenderComponent(); @@ -95,7 +95,7 @@ public void Test004() public void Test0041() { // arrange - var jsRtMock = Services.AddMockJsRuntime(); + var jsRtMock = Services.AddMockJSRuntime(); // act var cut = RenderComponent(); // first render @@ -110,7 +110,7 @@ public void Test0041() "the OnAddingTodo is raised with a new Todo containing the entered text")] public void Test005() { - var jsRtMock = Services.AddMockJsRuntime(); + var jsRtMock = Services.AddMockJSRuntime(); var taskValue = "HELLO WORLD TASK"; var createdTask = default(Todo); var cut = RenderComponent( @@ -127,7 +127,7 @@ public void Test005() [Fact(DisplayName = "When add task form is submitted with no text OnAddingTodo is not called")] public void Test006() { - var jsRtMock = Services.AddMockJsRuntime(); + var jsRtMock = Services.AddMockJSRuntime(); var createdTask = default(Todo); var cut = RenderComponent( EventCallback(nameof(TodoList.OnAddingTodo), task => createdTask = task) diff --git a/sample/tests/Tests/Components/WikiSearchTest.cs b/sample/tests/Tests/Components/WikiSearchTest.cs index 42288de4e..40d52c86a 100644 --- a/sample/tests/Tests/Components/WikiSearchTest.cs +++ b/sample/tests/Tests/Components/WikiSearchTest.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; using SampleApp.Components; -using Bunit.Mocking.JSInterop; +using Bunit.TestDoubles.JSInterop; using Shouldly; using Xunit; using Bunit; @@ -18,10 +18,10 @@ public class WikiSearchTest : TestContext public void Test001() { // Arrange - // Registered the MockJsRuntime in "Loose" mode with the service provider used when rendering components. - // JsRuntimeMockMode.Loose is the default. It configures the mock to just return the default + // Registered the MockJSRuntime in "Loose" mode with the service provider used when rendering components. + // JSRuntimeMockMode.Loose is the default. It configures the mock to just return the default // value for whatever is requested in a InvokeAsync call if no call has explicitly been set up. - var jsMock = Services.AddMockJsRuntime(); + var jsMock = Services.AddMockJSRuntime(); // Act - render the WikiSearch component var cut = RenderComponent(); @@ -37,10 +37,10 @@ public void Test001() public void Test002() { // Arrange - // Registered the MockJsRuntime in "strict" mode with the service provider used when rendering components. - // JsRuntimeMockMode.Strict mode configures the mock to throw an error if it receives an InvokeAsync call + // Registered the MockJSRuntime in "strict" mode with the service provider used when rendering components. + // JSRuntimeMockMode.Strict mode configures the mock to throw an error if it receives an InvokeAsync call // it has not been set up to handle. - var jsMock = Services.AddMockJsRuntime(JsRuntimeMockMode.Strict); + var jsMock = Services.AddMockJSRuntime(JSRuntimeMockMode.Strict); // Set up the mock to handle the expected call var expectedSearchResult = "SEARCH RESULT"; diff --git a/sample/tests/Tests/Pages/TodosTest.cs b/sample/tests/Tests/Pages/TodosTest.cs index e7bf08c22..798feb12c 100644 --- a/sample/tests/Tests/Pages/TodosTest.cs +++ b/sample/tests/Tests/Pages/TodosTest.cs @@ -9,7 +9,7 @@ using SampleApp.Data; using SampleApp.Pages; using Microsoft.Extensions.DependencyInjection; -using Bunit.Mocking.JSInterop; +using Bunit.TestDoubles.JSInterop; using Bunit; namespace SampleApp.CodeOnlyTests.Pages @@ -18,7 +18,7 @@ public class TodosTest : TestContext { public TodosTest() { - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); } [Fact(DisplayName = "Renders Todos provided by todo service")] diff --git a/sample/tests/_Imports.razor b/sample/tests/_Imports.razor index 2dbc4caa8..4436c8eb3 100644 --- a/sample/tests/_Imports.razor +++ b/sample/tests/_Imports.razor @@ -2,7 +2,7 @@ @using Microsoft.Extensions.DependencyInjection @using Bunit -@using Bunit.Mocking.JSInterop +@using Bunit.TestDoubles.JSInterop @using SampleApp @using SampleApp.Data @using SampleApp.Components diff --git a/src/bunit.core.tests/ComponentParameterFactoryTest.cs b/src/bunit.core.tests/ComponentParameterFactoryTest.cs index dc980c405..c57dc1682 100644 --- a/src/bunit.core.tests/ComponentParameterFactoryTest.cs +++ b/src/bunit.core.tests/ComponentParameterFactoryTest.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; -using Bunit.Mocking.JSInterop; using Bunit.Rendering; using Bunit.TestAssets.SampleComponents; +using Bunit.TestDoubles.JSInterop; using Shouldly; using Xunit; @@ -16,7 +16,7 @@ public class ComponentParameterFactoryTest : TestContext [Fact(DisplayName = "All types of parameters are correctly assigned to component on render")] public void Test005() { - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); var cut = RenderComponent>( ("some-unmatched-attribute", "unmatched value"), @@ -48,7 +48,7 @@ public void Test005() public void Test002() { // arrange - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); var cut = RenderComponent>(); // assert that no parameters have been set initially diff --git a/src/bunit.sln b/src/bunit.sln index 16508929f..ecb2b9882 100644 --- a/src/bunit.sln +++ b/src/bunit.sln @@ -46,7 +46,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{785D EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.template", "bunit.template\bunit.template.csproj", "{58FB9C26-7170-426C-ACE8-A98237710CDB}" -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.template", "bunit.template\bunit.template.csproj", "{56F7777C-38B1-4183-98A1-3D8D207A19BB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -92,8 +91,6 @@ Global {58FB9C26-7170-426C-ACE8-A98237710CDB}.Debug|Any CPU.Build.0 = Debug|Any CPU {58FB9C26-7170-426C-ACE8-A98237710CDB}.Release|Any CPU.ActiveCfg = Release|Any CPU {58FB9C26-7170-426C-ACE8-A98237710CDB}.Release|Any CPU.Build.0 = Release|Any CPU - {56F7777C-38B1-4183-98A1-3D8D207A19BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {56F7777C-38B1-4183-98A1-3D8D207A19BB}.Release|Any CPU.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/bunit.template/bunit.template.csproj b/src/bunit.template/bunit.template.csproj index f8ea5da04..107b52190 100644 --- a/src/bunit.template/bunit.template.csproj +++ b/src/bunit.template/bunit.template.csproj @@ -12,7 +12,7 @@ A project template for a testing Blazor/Razor components using the bUnit library. - bUnit is a testing library for Blazor Components. You can easily define components under test in C# or Razor syntax and verify outcome using semantic HTML diffing/comparison logic. You can easily interact with and inspect components, trigger event handlers, provide cascading values, inject services, mock IJsRuntime, and perform snapshot testing. + bUnit is a testing library for Blazor Components. You can easily define components under test in C# or Razor syntax and verify outcome using semantic HTML diffing/comparison logic. You can easily interact with and inspect components, trigger event handlers, provide cascading values, inject services, mock IJSRuntime, and perform snapshot testing. The library's goal is to make it easy to write comprehensive, stable unit tests for Blazor Components/Razor Components. Go to bunit.egilhansen.com to learn more. diff --git a/src/bunit.template/template/CounterCSharpTest.cs b/src/bunit.template/template/CounterCSharpTest.cs index 096841bf2..17ae787f2 100644 --- a/src/bunit.template/template/CounterCSharpTest.cs +++ b/src/bunit.template/template/CounterCSharpTest.cs @@ -1,7 +1,7 @@ using System; using Xunit; using Bunit; -using Bunit.Mocking.JSInterop; +using Bunit.TestDoubles.JSInterop; using static Bunit.ComponentParameterFactory; namespace Company.BlazorTests1 @@ -35,4 +35,4 @@ public void ClickingButtonIncrementsCounter() cut.Find("p").MarkupMatches("

    Current count: 1

    "); } } -} \ No newline at end of file +} diff --git a/src/bunit.template/template/_Imports.razor b/src/bunit.template/template/_Imports.razor index d4b250fb7..237a205d6 100644 --- a/src/bunit.template/template/_Imports.razor +++ b/src/bunit.template/template/_Imports.razor @@ -1,8 +1,8 @@ -@using Microsoft.AspNetCore.Components.Forms +@using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web @using Microsoft.JSInterop @using Microsoft.Extensions.DependencyInjection @using Bunit -@using Bunit.Mocking.JSInterop -@using Xunit \ No newline at end of file +@using Bunit.TestDoubles.JSInterop +@using Xunit diff --git a/src/bunit.testassets/SampleComponents/AllTypesOfParams.cs b/src/bunit.testassets/SampleComponents/AllTypesOfParams.cs index 3fd5d31a1..d2017c178 100644 --- a/src/bunit.testassets/SampleComponents/AllTypesOfParams.cs +++ b/src/bunit.testassets/SampleComponents/AllTypesOfParams.cs @@ -9,7 +9,7 @@ namespace Bunit.TestAssets.SampleComponents public class AllTypesOfParams : ComponentBase { [Inject] - public IJSRuntime? JsRuntime { get; set; } + public IJSRuntime? JSRuntime { get; set; } [Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary Attributes { get; set; } = default!; diff --git a/src/bunit.testassets/SampleComponents/SimpleWithJsRuntimeDep.razor b/src/bunit.testassets/SampleComponents/SimpleWithJSRuntimeDep.razor similarity index 89% rename from src/bunit.testassets/SampleComponents/SimpleWithJsRuntimeDep.razor rename to src/bunit.testassets/SampleComponents/SimpleWithJSRuntimeDep.razor index 4c8f6d1d0..91347b5d8 100644 --- a/src/bunit.testassets/SampleComponents/SimpleWithJsRuntimeDep.razor +++ b/src/bunit.testassets/SampleComponents/SimpleWithJSRuntimeDep.razor @@ -1,4 +1,4 @@ -@inject IJSRuntime jsRuntime +@inject IJSRuntime jsRuntime

    @name

    @code{ string name = string.Empty; @@ -11,4 +11,4 @@ StateHasChanged(); } } -} \ No newline at end of file +} diff --git a/src/bunit.web.tests/BlazorE2E/ComponentRenderingTest.cs b/src/bunit.web.tests/BlazorE2E/ComponentRenderingTest.cs index e9ce63b5a..c1f901d04 100644 --- a/src/bunit.web.tests/BlazorE2E/ComponentRenderingTest.cs +++ b/src/bunit.web.tests/BlazorE2E/ComponentRenderingTest.cs @@ -2,11 +2,9 @@ using System.Configuration.Assemblies; using System.Linq; using System.Numerics; - -using Bunit.Mocking.JSInterop; using Bunit.TestAssets.BlazorE2E; using Bunit.TestAssets.BlazorE2E.HierarchicalImportsTest.Subdir; - +using Bunit.TestDoubles.JSInterop; using Microsoft.AspNetCore.Components; using Shouldly; @@ -367,9 +365,9 @@ public void LogicalElementInsertionWorksHierarchically() } [Fact] - public void CanUseJsInteropToReferenceElements() + public void CanUseJSInteropToReferenceElements() { - // NOTE: This test required JS to modify the DOM. Test rewritten to use MockJsRuntime + // NOTE: This test required JS to modify the DOM. Test rewritten to use MockJSRuntime // The original test code is here: // var cut = RenderComponent(); // var inputElement = cut.Find("#capturedElement"); @@ -382,14 +380,14 @@ public void CanUseJsInteropToReferenceElements() // buttonElement.Click(); // Assert.Equal("Clicks: 2", inputElement.GetAttribute("value")); - var mockJs = Services.AddMockJsRuntime(); + var mockJS = Services.AddMockJSRuntime(); var cut = RenderComponent(); var inputElement = cut.Find("#capturedElement"); var refId = inputElement.GetAttribute(Htmlizer.ELEMENT_REFERENCE_ATTR_NAME); var buttonElement = cut.Find("button"); buttonElement.Click(); - mockJs.VerifyInvoke("setElementValue") + mockJS.VerifyInvoke("setElementValue") .Arguments[0] .ShouldBeOfType() .Id.ShouldBe(refId); @@ -398,7 +396,7 @@ public void CanUseJsInteropToReferenceElements() [Fact] public void CanCaptureReferencesToDynamicallyAddedElements() { - // NOTE: This test required JS to modify the DOM. Test rewritten to use MockJsRuntime + // NOTE: This test required JS to modify the DOM. Test rewritten to use MockJSRuntime // The original test code is here: //var cut = RenderComponent(); //var buttonElement = cut.Find("button"); @@ -421,7 +419,7 @@ public void CanCaptureReferencesToDynamicallyAddedElements() //buttonElement.Click(); //Assert.Equal("Clicks: 1", () => inputElement.GetAttribute("value")); - var mockJs = Services.AddMockJsRuntime(); + var mockJS = Services.AddMockJSRuntime(); var cut = RenderComponent(); var buttonElement = cut.Find("button"); @@ -443,7 +441,7 @@ public void CanCaptureReferencesToDynamicallyAddedElements() // See that the capture variable was automatically updated to reference the new instance buttonElement.Click(); - mockJs.VerifyInvoke("setElementValue") + mockJS.VerifyInvoke("setElementValue") .Arguments[0] .ShouldBeOfType() .Id.ShouldBe(refId); @@ -481,7 +479,7 @@ public void CanCaptureReferencesToDynamicallyAddedComponents() // Test depends on javascript changing the DOM, thus doesnt make sense in this context. //[Fact] - //public void CanUseJsInteropForRefElementsDuringOnAfterRender() + //public void CanUseJSInteropForRefElementsDuringOnAfterRender() //{ // var cut = RenderComponent(); // Assert.Equal("Value set after render", () => Browser.Find("input").GetAttribute("value")); diff --git a/src/bunit.web.tests/Rendering/RenderedComponentTest.cs b/src/bunit.web.tests/Rendering/RenderedComponentTest.cs index 0950b65cd..e9533c313 100644 --- a/src/bunit.web.tests/Rendering/RenderedComponentTest.cs +++ b/src/bunit.web.tests/Rendering/RenderedComponentTest.cs @@ -1,7 +1,6 @@ using System; -using Bunit.Mocking.JSInterop; using Bunit.TestAssets.SampleComponents; - +using Bunit.TestDoubles.JSInterop; using Shouldly; using Xunit; @@ -47,7 +46,7 @@ public void Test0041() public void Test003() { // arrange - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); var cut = RenderComponent>(); // assert diff --git a/src/bunit.web.tests/TestContextTest.cs b/src/bunit.web.tests/TestContextTest.cs index 4d14cef64..33ba7d293 100644 --- a/src/bunit.web.tests/TestContextTest.cs +++ b/src/bunit.web.tests/TestContextTest.cs @@ -1,8 +1,6 @@ using System; - -using Bunit.Mocking.JSInterop; using Bunit.TestAssets.SampleComponents; - +using Bunit.TestDoubles.JSInterop; using Shouldly; using Xunit; @@ -14,16 +12,16 @@ public class TestContextTest : TestContext [Fact(DisplayName = "The test service provider should register a placeholder IJSRuntime which throws exceptions")] public void Test021() { - var ex = Should.Throw(() => RenderComponent()); - ex.InnerException.ShouldBeOfType(); + var ex = Should.Throw(() => RenderComponent()); + ex.InnerException.ShouldBeOfType(); } [Fact(DisplayName = "The placeholder IJSRuntime is overridden by a supplied mock and does not throw")] public void Test022() { - Services.AddMockJsRuntime(); + Services.AddMockJSRuntime(); - RenderComponent(); + RenderComponent(); } } } diff --git a/src/bunit.web.tests/Mocking/JSInterop/JsRuntimeAssertExtensionsTest.cs b/src/bunit.web.tests/TestDoubles/JSInterop/JSRuntimeAssertExtensionsTest.cs similarity index 66% rename from src/bunit.web.tests/Mocking/JSInterop/JsRuntimeAssertExtensionsTest.cs rename to src/bunit.web.tests/TestDoubles/JSInterop/JSRuntimeAssertExtensionsTest.cs index ab1aa7f27..1f4273f92 100644 --- a/src/bunit.web.tests/Mocking/JSInterop/JsRuntimeAssertExtensionsTest.cs +++ b/src/bunit.web.tests/TestDoubles/JSInterop/JSRuntimeAssertExtensionsTest.cs @@ -6,7 +6,6 @@ using Bunit.Asserting; using Bunit.Diffing; - using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; @@ -16,45 +15,45 @@ using Xunit; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { - public class JsRuntimeAssertExtensionsTest + public class JSRuntimeAssertExtensionsTest { [Fact(DisplayName = "VerifyNotInvoke throws if handler is null")] public void Test001() { - MockJsRuntimeInvokeHandler? handler = null; - Should.Throw(() => JsRuntimeAssertExtensions.VerifyNotInvoke(handler!, "")); + MockJSRuntimeInvokeHandler? handler = null; + Should.Throw(() => (handler!).VerifyNotInvoke("")); } - [Fact(DisplayName = "VerifyNotInvoke throws JsInvokeCountExpectedException if identifier " + + [Fact(DisplayName = "VerifyNotInvoke throws JSInvokeCountExpectedException if identifier " + "has been invoked one or more times")] public async Task Test002() { var identifier = "test"; - var handler = new MockJsRuntimeInvokeHandler(); - await handler.ToJsRuntime().InvokeVoidAsync(identifier); + var handler = new MockJSRuntimeInvokeHandler(); + await handler.ToJSRuntime().InvokeVoidAsync(identifier); - Should.Throw(() => handler.VerifyNotInvoke(identifier)); + Should.Throw(() => handler.VerifyNotInvoke(identifier)); } - [Fact(DisplayName = "VerifyNotInvoke throws JsInvokeCountExpectedException if identifier " + + [Fact(DisplayName = "VerifyNotInvoke throws JSInvokeCountExpectedException if identifier " + "has been invoked one or more times, with custom error message")] public async Task Test003() { var identifier = "test"; var errMsg = "HELLO WORLD"; - var handler = new MockJsRuntimeInvokeHandler(); - await handler.ToJsRuntime().InvokeVoidAsync(identifier); + var handler = new MockJSRuntimeInvokeHandler(); + await handler.ToJSRuntime().InvokeVoidAsync(identifier); - Should.Throw(() => handler.VerifyNotInvoke(identifier, errMsg)) + Should.Throw(() => handler.VerifyNotInvoke(identifier, errMsg)) .Message.ShouldContain(errMsg); } [Fact(DisplayName = "VerifyNotInvoke does not throw if identifier has not been invoked")] public void Test004() { - var handler = new MockJsRuntimeInvokeHandler(); + var handler = new MockJSRuntimeInvokeHandler(); handler.VerifyNotInvoke("FOOBAR"); } @@ -62,28 +61,28 @@ public void Test004() [Fact(DisplayName = "VerifyInvoke throws if handler is null")] public void Test100() { - MockJsRuntimeInvokeHandler? handler = null; - Should.Throw(() => JsRuntimeAssertExtensions.VerifyInvoke(handler!, "")); - Should.Throw(() => JsRuntimeAssertExtensions.VerifyInvoke(handler!, "", 42)); + MockJSRuntimeInvokeHandler? handler = null; + Should.Throw(() => (handler!).VerifyInvoke("")); + Should.Throw(() => (handler!).VerifyInvoke("", 42)); } [Fact(DisplayName = "VerifyInvoke throws invokeCount is less than 1")] public void Test101() { - var handler = new MockJsRuntimeInvokeHandler(); + var handler = new MockJSRuntimeInvokeHandler(); Should.Throw(() => handler.VerifyInvoke("", 0)); } - [Fact(DisplayName = "VerifyInvoke throws JsInvokeCountExpectedException when " + + [Fact(DisplayName = "VerifyInvoke throws JSInvokeCountExpectedException when " + "invocation count doesn't match the expected")] public async Task Test103() { var identifier = "test"; - var handler = new MockJsRuntimeInvokeHandler(); - await handler.ToJsRuntime().InvokeVoidAsync(identifier); + var handler = new MockJSRuntimeInvokeHandler(); + await handler.ToJSRuntime().InvokeVoidAsync(identifier); - var actual = Should.Throw(() => handler.VerifyInvoke(identifier, 2)); + var actual = Should.Throw(() => handler.VerifyInvoke(identifier, 2)); actual.ExpectedInvocationCount.ShouldBe(2); actual.ActualInvocationCount.ShouldBe(1); actual.Identifier.ShouldBe(identifier); @@ -93,8 +92,8 @@ public async Task Test103() public async Task Test104() { var identifier = "test"; - var handler = new MockJsRuntimeInvokeHandler(); - await handler.ToJsRuntime().InvokeVoidAsync(identifier); + var handler = new MockJSRuntimeInvokeHandler(); + await handler.ToJSRuntime().InvokeVoidAsync(identifier); var invocations = handler.VerifyInvoke(identifier, 1); invocations.ShouldBeSameAs(handler.Invocations[identifier]); @@ -106,9 +105,9 @@ public async Task Test104() [Fact(DisplayName = "ShouldBeElementReferenceTo throws if actualArgument or targeted element is null")] public void Test200() { - Should.Throw(() => JsRuntimeAssertExtensions.ShouldBeElementReferenceTo(null!, null!)) + Should.Throw(() => JSRuntimeAssertExtensions.ShouldBeElementReferenceTo(null!, null!)) .ParamName.ShouldBe("actualArgument"); - Should.Throw(() => JsRuntimeAssertExtensions.ShouldBeElementReferenceTo(string.Empty, null!)) + Should.Throw(() => string.Empty.ShouldBeElementReferenceTo(null!)) .ParamName.ShouldBe("expectedTargetElement"); } diff --git a/src/bunit.web.tests/Mocking/JSInterop/JsRuntimeInvocationTest.cs b/src/bunit.web.tests/TestDoubles/JSInterop/JSRuntimeInvocationTest.cs similarity index 66% rename from src/bunit.web.tests/Mocking/JSInterop/JsRuntimeInvocationTest.cs rename to src/bunit.web.tests/TestDoubles/JSInterop/JSRuntimeInvocationTest.cs index 0926becf9..7bf55b6be 100644 --- a/src/bunit.web.tests/Mocking/JSInterop/JsRuntimeInvocationTest.cs +++ b/src/bunit.web.tests/TestDoubles/JSInterop/JSRuntimeInvocationTest.cs @@ -1,26 +1,25 @@ -using System; +using System; using System.Collections.Generic; using System.Threading; - using Shouldly; using Xunit; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { - public class JsRuntimeInvocationTest + public class JSRuntimeInvocationTest { public static IEnumerable GetEqualsTestData() { var token = new CancellationToken(true); var args = new object[] { 1, "baz" }; - var i1 = new JsRuntimeInvocation("foo", token, args); - var i2 = new JsRuntimeInvocation("foo", token, args); - var i3 = new JsRuntimeInvocation("bar", token, args); - var i4 = new JsRuntimeInvocation("foo", CancellationToken.None, args); - var i5 = new JsRuntimeInvocation("foo", token, Array.Empty()); - var i6 = new JsRuntimeInvocation("foo", token, new object[] { 2, "woop" }); + var i1 = new JSRuntimeInvocation("foo", token, args); + var i2 = new JSRuntimeInvocation("foo", token, args); + var i3 = new JSRuntimeInvocation("bar", token, args); + var i4 = new JSRuntimeInvocation("foo", CancellationToken.None, args); + var i5 = new JSRuntimeInvocation("foo", token, Array.Empty()); + var i6 = new JSRuntimeInvocation("foo", token, new object[] { 2, "woop" }); yield return new object[] { i1, i1, true }; yield return new object[] { i1, i2, true }; @@ -32,7 +31,7 @@ public static IEnumerable GetEqualsTestData() [Theory(DisplayName = "Equals operator works as expected")] [MemberData(nameof(GetEqualsTestData))] - public void Test002(JsRuntimeInvocation left, JsRuntimeInvocation right, bool expectedResult) + public void Test002(JSRuntimeInvocation left, JSRuntimeInvocation right, bool expectedResult) { left.Equals(right).ShouldBe(expectedResult); right.Equals(left).ShouldBe(expectedResult); @@ -45,12 +44,12 @@ public void Test002(JsRuntimeInvocation left, JsRuntimeInvocation right, bool ex [Fact(DisplayName = "Equals operator works as expected with non compatible types")] public void Test003() { - new JsRuntimeInvocation().Equals(new object()).ShouldBeFalse(); + new JSRuntimeInvocation().Equals(new object()).ShouldBeFalse(); } - [Theory(DisplayName = "GetHashCode returns same result for equal JsRuntimeInvocations")] + [Theory(DisplayName = "GetHashCode returns same result for equal JSRuntimeInvocations")] [MemberData(nameof(GetEqualsTestData))] - public void Test004(JsRuntimeInvocation left, JsRuntimeInvocation right, bool expectedResult) + public void Test004(JSRuntimeInvocation left, JSRuntimeInvocation right, bool expectedResult) { left.GetHashCode().Equals(right.GetHashCode()).ShouldBe(expectedResult); } diff --git a/src/bunit.web.tests/Mocking/JSInterop/MockJsRuntimeInvokeHandlerTest.cs b/src/bunit.web.tests/TestDoubles/JSInterop/MockJSRuntimeInvokeHandlerTest.cs similarity index 69% rename from src/bunit.web.tests/Mocking/JSInterop/MockJsRuntimeInvokeHandlerTest.cs rename to src/bunit.web.tests/TestDoubles/JSInterop/MockJSRuntimeInvokeHandlerTest.cs index a346e7960..a26431e81 100644 --- a/src/bunit.web.tests/Mocking/JSInterop/MockJsRuntimeInvokeHandlerTest.cs +++ b/src/bunit.web.tests/TestDoubles/JSInterop/MockJSRuntimeInvokeHandlerTest.cs @@ -2,23 +2,22 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; - using Microsoft.JSInterop; using Shouldly; using Xunit; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { - public class MockJsRuntimeInvokeHandlerTest + public class MockJSRuntimeInvokeHandlerTest { [Fact(DisplayName = "Mock returns default value in loose mode without invocation setup")] public async Task Test001() { - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Loose); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Loose); - var result = await sut.ToJsRuntime().InvokeAsync("ident", Array.Empty()); + var result = await sut.ToJSRuntime().InvokeAsync("ident", Array.Empty()); result.ShouldBe(default); } @@ -29,9 +28,9 @@ public void Test002() var identifier = "fooFunc"; var args = new[] { "bar", "baz" }; using var cts = new CancellationTokenSource(); - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Loose); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Loose); - var _ = sut.ToJsRuntime().InvokeAsync(identifier, cts.Token, args); + var _ = sut.ToJSRuntime().InvokeAsync(identifier, cts.Token, args); var invocation = sut.Invocations[identifier].Single(); invocation.Identifier.ShouldBe(identifier); @@ -42,15 +41,15 @@ public void Test002() [Fact(DisplayName = "Mock throws exception when in strict mode and invocation has not been setup")] public async Task Test003() { - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var identifier = "func"; var args = new[] { "bar", "baz" }; - var exception = await Should.ThrowAsync(sut.ToJsRuntime().InvokeVoidAsync(identifier, args).AsTask()); + var exception = await Should.ThrowAsync(sut.ToJSRuntime().InvokeVoidAsync(identifier, args).AsTask()); exception.Invocation.Identifier.ShouldBe(identifier); exception.Invocation.Arguments.ShouldBe(args); - exception = Should.Throw(() => { var _ = sut.ToJsRuntime().InvokeAsync(identifier, args); }); + exception = Should.Throw(() => { var _ = sut.ToJSRuntime().InvokeAsync(identifier, args); }); exception.Invocation.Identifier.ShouldBe(identifier); exception.Invocation.Arguments.ShouldBe(args); } @@ -61,8 +60,8 @@ public async Task Test005x() { var identifier = "func"; var expectedResult = Guid.NewGuid(); - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); - var jsRuntime = sut.ToJsRuntime(); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); + var jsRuntime = sut.ToJSRuntime(); var plannedInvoke = sut.Setup(identifier); plannedInvoke.SetResult(expectedResult); @@ -80,8 +79,8 @@ public async Task Test005() { var identifier = "func"; var expectedResult = Guid.NewGuid(); - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); - var jsRuntime = sut.ToJsRuntime(); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); + var jsRuntime = sut.ToJSRuntime(); var plannedInvoke = sut.Setup(identifier); var i1 = jsRuntime.InvokeAsync(identifier); @@ -97,9 +96,9 @@ public async Task Test005() public async Task Test006x() { var identifier = "func"; - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var plannedInvoke = sut.Setup(identifier); - var jsRuntime = sut.ToJsRuntime(); + var jsRuntime = sut.ToJSRuntime(); var expectedResult1 = Guid.NewGuid(); plannedInvoke.SetResult(expectedResult1); @@ -117,9 +116,9 @@ public async Task Test006x() public void Test007() { var identifier = "func"; - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var plannedInvoke = sut.Setup(identifier); - var invocation = sut.ToJsRuntime().InvokeAsync(identifier); + var invocation = sut.ToJSRuntime().InvokeAsync(identifier); plannedInvoke.SetCanceled(); @@ -130,9 +129,9 @@ public void Test007() public async Task Test008() { var identifier = "func"; - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var plannedInvoke = sut.Setup(identifier); - var invocation = sut.ToJsRuntime().InvokeAsync(identifier); + var invocation = sut.ToJSRuntime().InvokeAsync(identifier); var expectedException = new InvalidOperationException("TADA"); plannedInvoke.SetException(expectedException); @@ -146,10 +145,10 @@ public async Task Test008() public void Test009() { var identifier = "func"; - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var plannedInvoke = sut.Setup(identifier, x => true); - var i1 = sut.ToJsRuntime().InvokeAsync(identifier, "first"); - var i2 = sut.ToJsRuntime().InvokeAsync(identifier, "second"); + var i1 = sut.ToJSRuntime().InvokeAsync(identifier, "first"); + var i2 = sut.ToJSRuntime().InvokeAsync(identifier, "second"); var invocations = plannedInvoke.Invocations; @@ -161,12 +160,12 @@ public void Test009() [Fact(DisplayName = "Arguments used in Setup are matched with invocations")] public void Test010() { - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var planned = sut.Setup("foo", "bar", 42); - var _ = sut.ToJsRuntime().InvokeAsync("foo", "bar", 42); + var _ = sut.ToJSRuntime().InvokeAsync("foo", "bar", 42); - Should.Throw(() => { var _ = sut.ToJsRuntime().InvokeAsync("foo", "bar", 41); }); + Should.Throw(() => { var _ = sut.ToJSRuntime().InvokeAsync("foo", "bar", 41); }); planned.Invocations.Count.ShouldBe(1); var invocation = planned.Invocations[0]; @@ -178,12 +177,12 @@ public void Test010() [Fact(DisplayName = "Argument matcher used in Setup are matched with invocations")] public void Test011() { - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var planned = sut.Setup("foo", args => args.Count == 1); - var _ = sut.ToJsRuntime().InvokeAsync("foo", 42); + var _ = sut.ToJSRuntime().InvokeAsync("foo", 42); - Should.Throw(() => { var _ = sut.ToJsRuntime().InvokeAsync("foo", "bar", 42); }); + Should.Throw(() => { var _ = sut.ToJSRuntime().InvokeAsync("foo", "bar", 42); }); planned.Invocations.Count.ShouldBe(1); var invocation = planned.Invocations[0]; @@ -196,10 +195,10 @@ public void Test011() public async Task Test012() { var identifier = "func"; - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var plannedInvoke = sut.SetupVoid(identifier); - var invocation = sut.ToJsRuntime().InvokeVoidAsync(identifier); + var invocation = sut.ToJSRuntime().InvokeVoidAsync(identifier); plannedInvoke.SetVoidResult(); await invocation; @@ -210,13 +209,13 @@ public async Task Test012() [Fact(DisplayName = "Arguments used in SetupVoid are matched with invocations")] public async Task Test013() { - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var planned = sut.SetupVoid("foo", "bar", 42); - var _ = sut.ToJsRuntime().InvokeVoidAsync("foo", "bar", 42); + var _ = sut.ToJSRuntime().InvokeVoidAsync("foo", "bar", 42); - await Should.ThrowAsync( - sut.ToJsRuntime().InvokeVoidAsync("foo", "bar", 41).AsTask() + await Should.ThrowAsync( + sut.ToJSRuntime().InvokeVoidAsync("foo", "bar", 41).AsTask() ); planned.Invocations.Count.ShouldBe(1); @@ -229,17 +228,17 @@ await Should.ThrowAsync( [Fact(DisplayName = "Argument matcher used in SetupVoid are matched with invocations")] public async Task Test014() { - var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Strict); + var sut = new MockJSRuntimeInvokeHandler(JSRuntimeMockMode.Strict); var planned = sut.SetupVoid("foo", args => args.Count == 2); - var i1 = sut.ToJsRuntime().InvokeVoidAsync("foo", "bar", 42); + var i1 = sut.ToJSRuntime().InvokeVoidAsync("foo", "bar", 42); - await Should.ThrowAsync( - sut.ToJsRuntime().InvokeVoidAsync("foo", 42).AsTask() + await Should.ThrowAsync( + sut.ToJSRuntime().InvokeVoidAsync("foo", 42).AsTask() ); - await Should.ThrowAsync( - sut.ToJsRuntime().InvokeVoidAsync("foo").AsTask() + await Should.ThrowAsync( + sut.ToJSRuntime().InvokeVoidAsync("foo").AsTask() ); planned.Invocations.Count.ShouldBe(1); diff --git a/src/bunit.web/Extensions/TestServiceProviderExtensions.cs b/src/bunit.web/Extensions/TestServiceProviderExtensions.cs index 816826955..831e3b80b 100644 --- a/src/bunit.web/Extensions/TestServiceProviderExtensions.cs +++ b/src/bunit.web/Extensions/TestServiceProviderExtensions.cs @@ -1,7 +1,6 @@ using Bunit.Diffing; -using Bunit.Mocking.JSInterop; using Bunit.Rendering; - +using Bunit.TestDoubles.JSInterop; using Microsoft.Extensions.DependencyInjection; using Microsoft.JSInterop; @@ -17,7 +16,7 @@ public static class TestServiceProviderExtensions /// public static IServiceCollection AddDefaultTestContextServices(this IServiceCollection services) { - services.AddSingleton(new PlaceholderJsRuntime()); + services.AddSingleton(new PlaceholderJSRuntime()); services.AddSingleton(srv => new HtmlComparer()); services.AddSingleton(srv => new HtmlParser( srv.GetRequiredService(), diff --git a/src/bunit.web/Mocking/JSInterop/JsRuntimeMockMode.cs b/src/bunit.web/Mocking/JSInterop/JsRuntimeMockMode.cs deleted file mode 100644 index 55adb8a24..000000000 --- a/src/bunit.web/Mocking/JSInterop/JsRuntimeMockMode.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Microsoft.JSInterop; - -namespace Bunit.Mocking.JSInterop -{ - /// - /// The execution mode of the . - /// - public enum JsRuntimeMockMode - { - /// - /// configures the to return default TValue - /// for calls to the mock. - /// - Loose = 0, - /// - /// configures the to throw an - /// exception when a call to - /// for has not been - /// setup in the mock. - /// - Strict - } -} diff --git a/src/bunit.web/Mocking/JSInterop/MockJsRuntimeExtensions.cs b/src/bunit.web/Mocking/JSInterop/MockJsRuntimeExtensions.cs deleted file mode 100644 index e54830769..000000000 --- a/src/bunit.web/Mocking/JSInterop/MockJsRuntimeExtensions.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; - -using Microsoft.Extensions.DependencyInjection; - -namespace Bunit.Mocking.JSInterop -{ - /// - /// Helper methods for registering the MockJsRuntime with a . - /// - public static class MockJsRuntimeExtensions - { - /// - /// Adds the to the . - /// - /// The added . - public static MockJsRuntimeInvokeHandler AddMockJsRuntime(this TestServiceProvider serviceProvider, JsRuntimeMockMode mode = JsRuntimeMockMode.Loose) - { - if (serviceProvider is null) - throw new ArgumentNullException(nameof(serviceProvider)); - - var result = new MockJsRuntimeInvokeHandler(mode); - - serviceProvider.AddSingleton(result.ToJsRuntime()); - - return result; - } - } -} diff --git a/src/bunit.web/Mocking/JSInterop/JsInvokeCountExpectedException.cs b/src/bunit.web/TestDoubles/JSInterop/JSInvokeCountExpectedException.cs similarity index 85% rename from src/bunit.web/Mocking/JSInterop/JsInvokeCountExpectedException.cs rename to src/bunit.web/TestDoubles/JSInterop/JSInvokeCountExpectedException.cs index 321798a0f..bba025b21 100644 --- a/src/bunit.web/Mocking/JSInterop/JsInvokeCountExpectedException.cs +++ b/src/bunit.web/TestDoubles/JSInterop/JSInvokeCountExpectedException.cs @@ -1,15 +1,14 @@ using System; using System.Diagnostics.CodeAnalysis; - -using Bunit.Mocking.JSInterop; +using Bunit.TestDoubles.JSInterop; namespace Bunit { /// - /// Represents a number of unexpected invocation to a . + /// Represents a number of unexpected invocation to a . /// [SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "")] - public class JsInvokeCountExpectedException : Exception + public class JSInvokeCountExpectedException : Exception { /// /// Gets the expected invocation count. @@ -27,9 +26,9 @@ public class JsInvokeCountExpectedException : Exception public string Identifier { get; } /// - /// Creates an instance of the . + /// Creates an instance of the . /// - public JsInvokeCountExpectedException(string identifier, int expectedCount, int actualCount, string assertMethod, string? userMessage = null) + public JSInvokeCountExpectedException(string identifier, int expectedCount, int actualCount, string assertMethod, string? userMessage = null) : base(CreateMessage(identifier, expectedCount, actualCount, assertMethod, userMessage)) { ExpectedInvocationCount = expectedCount; diff --git a/src/bunit.web/Mocking/JSInterop/JsRuntimeAssertExtensions.cs b/src/bunit.web/TestDoubles/JSInterop/JSRuntimeAssertExtensions.cs similarity index 82% rename from src/bunit.web/Mocking/JSInterop/JsRuntimeAssertExtensions.cs rename to src/bunit.web/TestDoubles/JSInterop/JSRuntimeAssertExtensions.cs index 6e3435823..5e4e9699e 100644 --- a/src/bunit.web/Mocking/JSInterop/JsRuntimeAssertExtensions.cs +++ b/src/bunit.web/TestDoubles/JSInterop/JSRuntimeAssertExtensions.cs @@ -7,12 +7,12 @@ using Microsoft.AspNetCore.Components; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// - /// Assert extensions for JsRuntimeMock + /// Assert extensions for JSRuntimeMock /// - public static class JsRuntimeAssertExtensions + public static class JSRuntimeAssertExtensions { /// /// Verifies that the was never invoked on the . @@ -20,13 +20,13 @@ public static class JsRuntimeAssertExtensions /// Handler to verify against. /// Identifier of invocation that should not have happened. /// A custom user message to display if the assertion fails. - public static void VerifyNotInvoke(this MockJsRuntimeInvokeHandler handler, string identifier, string? userMessage = null) + public static void VerifyNotInvoke(this MockJSRuntimeInvokeHandler handler, string identifier, string? userMessage = null) { if (handler is null) throw new ArgumentNullException(nameof(handler)); if (handler.Invocations.TryGetValue(identifier, out var invocations) && invocations.Count > 0) { - throw new JsInvokeCountExpectedException(identifier, 0, invocations.Count, nameof(VerifyNotInvoke), userMessage); + throw new JSInvokeCountExpectedException(identifier, 0, invocations.Count, nameof(VerifyNotInvoke), userMessage); } } @@ -36,9 +36,9 @@ public static void VerifyNotInvoke(this MockJsRuntimeInvokeHandler handler, stri /// Handler to verify against. /// Identifier of invocation that should have been invoked. /// A custom user message to display if the assertion fails. - /// The . - public static JsRuntimeInvocation VerifyInvoke(this MockJsRuntimeInvokeHandler handler, string identifier, string? userMessage = null) - => VerifyInvoke(handler, identifier, 1, userMessage)[0]; + /// The . + public static JSRuntimeInvocation VerifyInvoke(this MockJSRuntimeInvokeHandler handler, string identifier, string? userMessage = null) + => handler.VerifyInvoke(identifier, 1, userMessage)[0]; /// /// Verifies that the has been invoked times. @@ -47,8 +47,8 @@ public static JsRuntimeInvocation VerifyInvoke(this MockJsRuntimeInvokeHandler h /// Identifier of invocation that should have been invoked. /// The number of times the invocation is expected to have been called. /// A custom user message to display if the assertion fails. - /// The . - public static IReadOnlyList VerifyInvoke(this MockJsRuntimeInvokeHandler handler, string identifier, int calledTimes, string? userMessage = null) + /// The . + public static IReadOnlyList VerifyInvoke(this MockJSRuntimeInvokeHandler handler, string identifier, int calledTimes, string? userMessage = null) { if (handler is null) throw new ArgumentNullException(nameof(handler)); @@ -58,12 +58,12 @@ public static IReadOnlyList VerifyInvoke(this MockJsRuntime if (!handler.Invocations.TryGetValue(identifier, out var invocations)) { - throw new JsInvokeCountExpectedException(identifier, calledTimes, 0, nameof(VerifyInvoke), userMessage); + throw new JSInvokeCountExpectedException(identifier, calledTimes, 0, nameof(VerifyInvoke), userMessage); } if (invocations.Count != calledTimes) { - throw new JsInvokeCountExpectedException(identifier, calledTimes, invocations.Count, nameof(VerifyInvoke), userMessage); + throw new JSInvokeCountExpectedException(identifier, calledTimes, invocations.Count, nameof(VerifyInvoke), userMessage); } return invocations; @@ -71,7 +71,7 @@ public static IReadOnlyList VerifyInvoke(this MockJsRuntime /// /// Verifies that an argument - /// passed to an JsRuntime invocation is an + /// passed to an JSRuntime invocation is an /// to the . /// /// object to verify. diff --git a/src/bunit.web/Mocking/JSInterop/JsRuntimeInvocation.cs b/src/bunit.web/TestDoubles/JSInterop/JSRuntimeInvocation.cs similarity index 72% rename from src/bunit.web/Mocking/JSInterop/JsRuntimeInvocation.cs rename to src/bunit.web/TestDoubles/JSInterop/JSRuntimeInvocation.cs index c347d66eb..222d19cad 100644 --- a/src/bunit.web/Mocking/JSInterop/JsRuntimeInvocation.cs +++ b/src/bunit.web/TestDoubles/JSInterop/JSRuntimeInvocation.cs @@ -1,15 +1,15 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Threading; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// - /// Represents an invocation of JavaScript via the JsRuntime Mock + /// Represents an invocation of JavaScript via the JSRuntime Mock /// [SuppressMessage("Design", "CA1068:CancellationToken parameters must come last", Justification = "")] - public readonly struct JsRuntimeInvocation : IEquatable + public readonly struct JSRuntimeInvocation : IEquatable { /// /// Gets the identifier used in the invocation. @@ -28,9 +28,9 @@ namespace Bunit.Mocking.JSInterop /// - /// Creates an instance of the . + /// Creates an instance of the . /// - public JsRuntimeInvocation(string identifier, CancellationToken cancellationToken, object[] args) + public JSRuntimeInvocation(string identifier, CancellationToken cancellationToken, object[] args) { Identifier = identifier; CancellationToken = cancellationToken; @@ -38,13 +38,13 @@ public JsRuntimeInvocation(string identifier, CancellationToken cancellationToke } /// - public bool Equals(JsRuntimeInvocation other) + public bool Equals(JSRuntimeInvocation other) => Identifier.Equals(other.Identifier, StringComparison.Ordinal) && CancellationToken == other.CancellationToken && ArgumentsEqual(Arguments, other.Arguments); /// - public override bool Equals(object obj) => obj is JsRuntimeInvocation other && Equals(other); + public override bool Equals(object obj) => obj is JSRuntimeInvocation other && Equals(other); /// public override int GetHashCode() @@ -53,7 +53,7 @@ public override int GetHashCode() hash.Add(Identifier); hash.Add(CancellationToken); - for (int i = 0; i < Arguments.Count; i++) + for (var i = 0; i < Arguments.Count; i++) { hash.Add(Arguments[i]); } @@ -62,17 +62,17 @@ public override int GetHashCode() } /// - public static bool operator ==(JsRuntimeInvocation left, JsRuntimeInvocation right) => left.Equals(right); + public static bool operator ==(JSRuntimeInvocation left, JSRuntimeInvocation right) => left.Equals(right); /// - public static bool operator !=(JsRuntimeInvocation left, JsRuntimeInvocation right) => !(left == right); + public static bool operator !=(JSRuntimeInvocation left, JSRuntimeInvocation right) => !(left == right); private static bool ArgumentsEqual(IReadOnlyList left, IReadOnlyList right) { if (left.Count != right.Count) return false; - for (int i = 0; i < left.Count; i++) + for (var i = 0; i < left.Count; i++) { if (!left[i].Equals(right[i])) return false; diff --git a/src/bunit.web/TestDoubles/JSInterop/JSRuntimeMockMode.cs b/src/bunit.web/TestDoubles/JSInterop/JSRuntimeMockMode.cs new file mode 100644 index 000000000..10e202266 --- /dev/null +++ b/src/bunit.web/TestDoubles/JSInterop/JSRuntimeMockMode.cs @@ -0,0 +1,23 @@ +using Microsoft.JSInterop; + +namespace Bunit.TestDoubles.JSInterop +{ + /// + /// The execution mode of the . + /// + public enum JSRuntimeMockMode + { + /// + /// configures the to return default TValue + /// for calls to the mock. + /// + Loose = 0, + /// + /// configures the to throw an + /// exception when a call to + /// for has not been + /// setup in the mock. + /// + Strict + } +} diff --git a/src/bunit.web/Mocking/JSInterop/JsRuntimePlannedInvocation.cs b/src/bunit.web/TestDoubles/JSInterop/JSRuntimePlannedInvocation.cs similarity index 68% rename from src/bunit.web/Mocking/JSInterop/JsRuntimePlannedInvocation.cs rename to src/bunit.web/TestDoubles/JSInterop/JSRuntimePlannedInvocation.cs index 302cbd969..339fa16da 100644 --- a/src/bunit.web/Mocking/JSInterop/JsRuntimePlannedInvocation.cs +++ b/src/bunit.web/TestDoubles/JSInterop/JSRuntimePlannedInvocation.cs @@ -1,14 +1,14 @@ using System; using System.Collections.Generic; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// /// Represents a planned invocation of a JavaScript function which returns nothing, with specific arguments. /// - public class JsRuntimePlannedInvocation : JsRuntimePlannedInvocationBase + public class JSRuntimePlannedInvocation : JSRuntimePlannedInvocationBase { - internal JsRuntimePlannedInvocation(string identifier, Func, bool> matcher) : base(identifier, matcher) + internal JSRuntimePlannedInvocation(string identifier, Func, bool> matcher) : base(identifier, matcher) { } @@ -17,7 +17,7 @@ internal JsRuntimePlannedInvocation(string identifier, Func public void SetVoidResult() { - base.SetResultBase(default!); + SetResultBase(default!); } } @@ -25,9 +25,9 @@ public void SetVoidResult() /// Represents a planned invocation of a JavaScript function with specific arguments. /// /// - public class JsRuntimePlannedInvocation : JsRuntimePlannedInvocationBase + public class JSRuntimePlannedInvocation : JSRuntimePlannedInvocationBase { - internal JsRuntimePlannedInvocation(string identifier, Func, bool> matcher) : base(identifier, matcher) + internal JSRuntimePlannedInvocation(string identifier, Func, bool> matcher) : base(identifier, matcher) { } @@ -35,6 +35,6 @@ internal JsRuntimePlannedInvocation(string identifier, Func result that invocations will receive. /// /// - public void SetResult(TResult result) => base.SetResultBase(result); + public void SetResult(TResult result) => SetResultBase(result); } } diff --git a/src/bunit.web/Mocking/JSInterop/JsRuntimePlannedInvocationBase.cs b/src/bunit.web/TestDoubles/JSInterop/JSRuntimePlannedInvocationBase.cs similarity index 78% rename from src/bunit.web/Mocking/JSInterop/JsRuntimePlannedInvocationBase.cs rename to src/bunit.web/TestDoubles/JSInterop/JSRuntimePlannedInvocationBase.cs index ad7583db6..0d58a6c73 100644 --- a/src/bunit.web/Mocking/JSInterop/JsRuntimePlannedInvocationBase.cs +++ b/src/bunit.web/TestDoubles/JSInterop/JSRuntimePlannedInvocationBase.cs @@ -2,15 +2,15 @@ using System.Collections.Generic; using System.Threading.Tasks; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// /// Represents a planned invocation of a JavaScript function with specific arguments. /// /// - public abstract class JsRuntimePlannedInvocationBase + public abstract class JSRuntimePlannedInvocationBase { - private readonly List _invocations; + private readonly List _invocations; private Func, bool> InvocationMatcher { get; } @@ -22,17 +22,17 @@ public abstract class JsRuntimePlannedInvocationBase public string Identifier { get; } /// - /// Gets the invocations that this has matched with. + /// Gets the invocations that this has matched with. /// - public IReadOnlyList Invocations => _invocations.AsReadOnly(); + public IReadOnlyList Invocations => _invocations.AsReadOnly(); /// - /// Creates an instance of a . + /// Creates an instance of a . /// - protected JsRuntimePlannedInvocationBase(string identifier, Func, bool> matcher) + protected JSRuntimePlannedInvocationBase(string identifier, Func, bool> matcher) { Identifier = identifier; - _invocations = new List(); + _invocations = new List(); InvocationMatcher = matcher; _completionSource = new TaskCompletionSource(); } @@ -73,13 +73,13 @@ public void SetCanceled() _completionSource.SetCanceled(); } - internal Task RegisterInvocation(JsRuntimeInvocation invocation) + internal Task RegisterInvocation(JSRuntimeInvocation invocation) { _invocations.Add(invocation); return _completionSource.Task; } - internal bool Matches(JsRuntimeInvocation invocation) + internal bool Matches(JSRuntimeInvocation invocation) { return Identifier.Equals(invocation.Identifier, StringComparison.Ordinal) && InvocationMatcher(invocation.Arguments); diff --git a/src/bunit.web/Mocking/JSInterop/MissingMockJsRuntimeException.cs b/src/bunit.web/TestDoubles/JSInterop/MissingMockJSRuntimeException.cs similarity index 69% rename from src/bunit.web/Mocking/JSInterop/MissingMockJsRuntimeException.cs rename to src/bunit.web/TestDoubles/JSInterop/MissingMockJSRuntimeException.cs index 08baa308b..dd6f3cf2d 100644 --- a/src/bunit.web/Mocking/JSInterop/MissingMockJsRuntimeException.cs +++ b/src/bunit.web/TestDoubles/JSInterop/MissingMockJSRuntimeException.cs @@ -2,14 +2,14 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// - /// Exception use to indicate that a MockJsRuntime is required by a test + /// Exception use to indicate that a MockJSRuntime is required by a test /// but was not provided. /// [SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "")] - public class MissingMockJsRuntimeException : Exception + public class MissingMockJSRuntimeException : Exception { /// /// Identifier string used in the JSInvoke method. @@ -22,13 +22,13 @@ public class MissingMockJsRuntimeException : Exception public IReadOnlyList Arguments { get; } /// - /// Creates a new instance of the + /// Creates a new instance of the /// with the arguments used in the invocation. /// /// The identifer used in the invocation. /// The args used in the invocation, if any - 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.") + 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; diff --git a/src/bunit.web/TestDoubles/JSInterop/MockJSRuntimeExtensions.cs b/src/bunit.web/TestDoubles/JSInterop/MockJSRuntimeExtensions.cs new file mode 100644 index 000000000..4370fbd7d --- /dev/null +++ b/src/bunit.web/TestDoubles/JSInterop/MockJSRuntimeExtensions.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.Extensions.DependencyInjection; + +namespace Bunit.TestDoubles.JSInterop +{ + /// + /// Helper methods for registering the MockJSRuntime with a . + /// + public static class MockJSRuntimeExtensions + { + /// + /// Adds the to the . + /// + /// The added . + public static MockJSRuntimeInvokeHandler AddMockJSRuntime(this TestServiceProvider serviceProvider, JSRuntimeMockMode mode = JSRuntimeMockMode.Loose) + { + if (serviceProvider is null) + throw new ArgumentNullException(nameof(serviceProvider)); + + var result = new MockJSRuntimeInvokeHandler(mode); + + serviceProvider.AddSingleton(result.ToJSRuntime()); + + return result; + } + } +} diff --git a/src/bunit.web/Mocking/JSInterop/MockJsRuntimeInvokeHandler.cs b/src/bunit.web/TestDoubles/JSInterop/MockJSRuntimeInvokeHandler.cs similarity index 65% rename from src/bunit.web/Mocking/JSInterop/MockJsRuntimeInvokeHandler.cs rename to src/bunit.web/TestDoubles/JSInterop/MockJSRuntimeInvokeHandler.cs index 7245963b1..ca9eb1c06 100644 --- a/src/bunit.web/Mocking/JSInterop/MockJsRuntimeInvokeHandler.cs +++ b/src/bunit.web/TestDoubles/JSInterop/MockJSRuntimeInvokeHandler.cs @@ -3,35 +3,34 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; - using Microsoft.JSInterop; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// /// Represents an invoke handler for a mock of a . /// - public class MockJsRuntimeInvokeHandler + public class MockJSRuntimeInvokeHandler { - private readonly Dictionary> _invocations = new Dictionary>(); + private readonly Dictionary> _invocations = new Dictionary>(); private readonly Dictionary> _plannedInvocations = new Dictionary>(); /// - /// Gets a dictionary of all this mock has observed. + /// Gets a dictionary of all this mock has observed. /// - public IReadOnlyDictionary> Invocations => _invocations; + public IReadOnlyDictionary> Invocations => _invocations; /// - /// Gets whether the mock is running in or - /// . + /// Gets whether the mock is running in or + /// . /// - public JsRuntimeMockMode Mode { get; } + public JSRuntimeMockMode Mode { get; } /// - /// Creates a . + /// Creates a . /// - /// The the handler should use. - public MockJsRuntimeInvokeHandler(JsRuntimeMockMode mode = JsRuntimeMockMode.Loose) + /// The the handler should use. + public MockJSRuntimeInvokeHandler(JSRuntimeMockMode mode = JSRuntimeMockMode.Loose) { Mode = mode; } @@ -40,9 +39,9 @@ public MockJsRuntimeInvokeHandler(JsRuntimeMockMode mode = JsRuntimeMockMode.Loo /// Gets the mocked instance. /// /// - public IJSRuntime ToJsRuntime() + public IJSRuntime ToJSRuntime() { - return new MockJsRuntime(this); + return new MockJSRuntime(this); } /// @@ -52,10 +51,10 @@ public IJSRuntime ToJsRuntime() /// The result type of the invocation /// The identifier to setup a response for /// A matcher that is passed arguments received in invocations to . If it returns true the invocation is matched. - /// A . - public JsRuntimePlannedInvocation Setup(string identifier, Func, bool> argumentsMatcher) + /// A . + public JSRuntimePlannedInvocation Setup(string identifier, Func, bool> argumentsMatcher) { - var result = new JsRuntimePlannedInvocation(identifier, argumentsMatcher); + var result = new JSRuntimePlannedInvocation(identifier, argumentsMatcher); AddPlannedInvocation(result); @@ -68,10 +67,10 @@ public JsRuntimePlannedInvocation Setup(string identifier, Fun /// /// The identifier to setup a response for /// The arguments that an invocation to should match. - /// A . - public JsRuntimePlannedInvocation Setup(string identifier, params object[] arguments) + /// A . + public JSRuntimePlannedInvocation Setup(string identifier, params object[] arguments) { - return Setup(identifier, args => Enumerable.SequenceEqual(args, arguments)); + return Setup(identifier, args => args.SequenceEqual(arguments)); } /// @@ -80,10 +79,10 @@ public JsRuntimePlannedInvocation Setup(string identifier, par /// /// The identifier to setup a response for /// A matcher that is passed arguments received in invocations to . If it returns true the invocation is matched. - /// A . - public JsRuntimePlannedInvocation SetupVoid(string identifier, Func, bool> argumentsMatcher) + /// A . + public JSRuntimePlannedInvocation SetupVoid(string identifier, Func, bool> argumentsMatcher) { - var result = new JsRuntimePlannedInvocation(identifier, argumentsMatcher); + var result = new JSRuntimePlannedInvocation(identifier, argumentsMatcher); AddPlannedInvocation(result); @@ -96,13 +95,13 @@ public JsRuntimePlannedInvocation SetupVoid(string identifier, Func /// The identifier to setup a response for /// The arguments that an invocation to should match. - /// A . - public JsRuntimePlannedInvocation SetupVoid(string identifier, params object[] arguments) + /// A . + public JSRuntimePlannedInvocation SetupVoid(string identifier, params object[] arguments) { - return SetupVoid(identifier, args => Enumerable.SequenceEqual(args, arguments)); + return SetupVoid(identifier, args => args.SequenceEqual(arguments)); } - private void AddPlannedInvocation(JsRuntimePlannedInvocationBase planned) + private void AddPlannedInvocation(JSRuntimePlannedInvocationBase planned) { if (!_plannedInvocations.ContainsKey(planned.Identifier)) { @@ -111,22 +110,22 @@ private void AddPlannedInvocation(JsRuntimePlannedInvocationBase()); + _invocations.Add(invocation.Identifier, new List()); } _invocations[invocation.Identifier].Add(invocation); } - private class MockJsRuntime : IJSRuntime + private class MockJSRuntime : IJSRuntime { - private readonly MockJsRuntimeInvokeHandler _handlers; + private readonly MockJSRuntimeInvokeHandler _handlers; - public MockJsRuntime(MockJsRuntimeInvokeHandler mockJsRuntimeInvokeHandler) + public MockJSRuntime(MockJSRuntimeInvokeHandler mockJSRuntimeInvokeHandler) { - _handlers = mockJsRuntimeInvokeHandler; + _handlers = mockJSRuntimeInvokeHandler; } public ValueTask InvokeAsync(string identifier, object[] args) @@ -134,20 +133,20 @@ public ValueTask InvokeAsync(string identifier, object[] args) public ValueTask InvokeAsync(string identifier, CancellationToken cancellationToken, object[] args) { - var invocation = new JsRuntimeInvocation(identifier, cancellationToken, args); + var invocation = new JSRuntimeInvocation(identifier, cancellationToken, args); _handlers.AddInvocation(invocation); return TryHandlePlannedInvocation(identifier, invocation) ?? new ValueTask(default(TValue)!); } - private ValueTask? TryHandlePlannedInvocation(string identifier, JsRuntimeInvocation invocation) + private ValueTask? TryHandlePlannedInvocation(string identifier, JSRuntimeInvocation invocation) { ValueTask? result = default; if (_handlers._plannedInvocations.TryGetValue(identifier, out var plannedInvocations)) { - var planned = plannedInvocations.OfType>() + var planned = plannedInvocations.OfType>() .SingleOrDefault(x => x.Matches(invocation)); if (planned is { }) @@ -157,9 +156,9 @@ public ValueTask InvokeAsync(string identifier, CancellationToke } } - if (result is null && _handlers.Mode == JsRuntimeMockMode.Strict) + if (result is null && _handlers.Mode == JSRuntimeMockMode.Strict) { - throw new UnplannedJsInvocationException(invocation); + throw new UnplannedJSInvocationException(invocation); } return result; diff --git a/src/bunit.web/Mocking/JSInterop/PlaceholderJsRuntime.cs b/src/bunit.web/TestDoubles/JSInterop/PlaceholderJSRuntime.cs similarity index 58% rename from src/bunit.web/Mocking/JSInterop/PlaceholderJsRuntime.cs rename to src/bunit.web/TestDoubles/JSInterop/PlaceholderJSRuntime.cs index a0785f23b..69ed048f6 100644 --- a/src/bunit.web/Mocking/JSInterop/PlaceholderJsRuntime.cs +++ b/src/bunit.web/TestDoubles/JSInterop/PlaceholderJSRuntime.cs @@ -1,23 +1,22 @@ using System.Threading; using System.Threading.Tasks; - using Microsoft.JSInterop; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// - /// This JsRuntime is used to provide users with helpful exceptions if they fail to provide a mock when required. + /// This JSRuntime is used to provide users with helpful exceptions if they fail to provide a mock when required. /// - internal class PlaceholderJsRuntime : IJSRuntime + internal class PlaceholderJSRuntime : IJSRuntime { public ValueTask InvokeAsync(string identifier, object[] args) { - throw new MissingMockJsRuntimeException(identifier, args); + throw new MissingMockJSRuntimeException(identifier, args); } public ValueTask InvokeAsync(string identifier, CancellationToken cancellationToken, object[] args) { - throw new MissingMockJsRuntimeException(identifier, args); + throw new MissingMockJSRuntimeException(identifier, args); } } } diff --git a/src/bunit.web/Mocking/JSInterop/UnplannedJsInvocationException.cs b/src/bunit.web/TestDoubles/JSInterop/UnplannedJSInvocationException.cs similarity index 73% rename from src/bunit.web/Mocking/JSInterop/UnplannedJsInvocationException.cs rename to src/bunit.web/TestDoubles/JSInterop/UnplannedJSInvocationException.cs index 394b5b82a..20b069e6e 100644 --- a/src/bunit.web/Mocking/JSInterop/UnplannedJsInvocationException.cs +++ b/src/bunit.web/TestDoubles/JSInterop/UnplannedJSInvocationException.cs @@ -5,26 +5,26 @@ using Bunit; -namespace Bunit.Mocking.JSInterop +namespace Bunit.TestDoubles.JSInterop { /// /// Exception use to indicate that an unplanned invocation was - /// received by the running in . + /// received by the running in . /// [SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "")] - public class UnplannedJsInvocationException : Exception + public class UnplannedJSInvocationException : Exception { /// /// Gets the unplanned invocation. /// - public JsRuntimeInvocation Invocation { get; } + public JSRuntimeInvocation Invocation { get; } /// - /// Creates a new instance of the + /// Creates a new instance of the /// with the provided attached. /// /// The unplanned invocation. - public UnplannedJsInvocationException(JsRuntimeInvocation invocation) + public UnplannedJSInvocationException(JSRuntimeInvocation invocation) : base($"The invocation of '{invocation.Identifier}' {PrintArguments(invocation.Arguments)} was not expected.") { Invocation = invocation;