Skip to content

Commit c2cf2b3

Browse files
authored
Fix/naming convention jsinterop (#150)
* Remove files In preparation to refactor of the files * Refactor names * Updated README.md with suggestion. Suggestion how the change could sound. * Update sample project * Update string reference * Update project description * Update for documentation with new naming * Add credit reference * Added issue reference * Update reference to version that would work * Remove issue link * Move from Mocking to TestDoubles folder * Update sample project to use new folder for test doubles * Fix template project * Moved tests for bunit.web
1 parent a54efc3 commit c2cf2b3

40 files changed

+310
-325
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ List of new features.
1313
List of changes in existing functionality.
1414

1515
- `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).
16+
- 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)
1617

1718
### Deprecated
1819
List of soon-to-be removed features.

docs/site/docs/csharp-test-examples.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,11 @@ public class ThemedButtonTest : ComponentTestFixture
396396
- `Test002` above uses the `CascadingValue(object value)` helper method to pass an **unnamed** cascading value to the CUT.
397397
- `Test003` above demonstrates how multiple (named) cascading values can be passed to a component under test.
398398

399-
## Testing components that use on `IJsRuntime`
399+
## Testing components that use on `IJSRuntime`
400400

401401
It is not uncommon to have components use Blazor's JSInterop functionality to call JavaScript, e.g. after first render.
402402

403-
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.
403+
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.
404404

405405
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.
406406

@@ -441,10 +441,10 @@ public class WikiSearchTest : ComponentTestFixture
441441
public void Test001()
442442
{
443443
// Arrange
444-
// Registered the MockJsRuntime in "Loose" mode with the service provider used when rendering components.
445-
// JsRuntimeMockMode.Loose is the default. It configures the mock to just return the default
444+
// Registered the MockJSRuntime in "Loose" mode with the service provider used when rendering components.
445+
// JSRuntimeMockMode.Loose is the default. It configures the mock to just return the default
446446
// value for whatever is requested in a InvokeAsync call if no call has explicitly been set up.
447-
var jsMock = Services.AddMockJsRuntime();
447+
var jsMock = Services.AddMockJSRuntime();
448448

449449
// Act - render the WikiSearch component
450450
var cut = RenderComponent<WikiSearch>();
@@ -460,10 +460,10 @@ public class WikiSearchTest : ComponentTestFixture
460460
public void Test002()
461461
{
462462
// Arrange
463-
// Registered the MockJsRuntime in "strict" mode with the service provider used when rendering components.
464-
// JsRuntimeMockMode.Strict mode configures the mock to throw an error if it receives an InvokeAsync call
463+
// Registered the MockJSRuntime in "strict" mode with the service provider used when rendering components.
464+
// JSRuntimeMockMode.Strict mode configures the mock to throw an error if it receives an InvokeAsync call
465465
// it has not been set up to handle.
466-
var jsMock = Services.AddMockJsRuntime(JsRuntimeMockMode.Strict);
466+
var jsMock = Services.AddMockJSRuntime(JSRuntimeMockMode.Strict);
467467

468468
// Set up the mock to handle the expected call
469469
var expectedSearchResult = "SEARCH RESULT";
@@ -486,7 +486,7 @@ public class WikiSearchTest : ComponentTestFixture
486486
}
487487
```
488488

489-
- `Test001` just injects the mock in "Loose" mode. It means it will only returns a `default(TValue)` for calls to `InvokeAsync<TValue>(...)` 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.
489+
- `Test001` just injects the mock in "Loose" mode. It means it will only returns a `default(TValue)` for calls to `InvokeAsync<TValue>(...)` 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.
490490

491491
In "Loose" mode it is still possible to call `VerifyInvoke(identifier)` and assert against the expected invocation.
492492

@@ -497,7 +497,7 @@ public class WikiSearchTest : ComponentTestFixture
497497

498498
### Verifying element references passed to InvokeAsync
499499

500-
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.
500+
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.
501501

502502
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:
503503

@@ -530,8 +530,8 @@ public class FocussingInputTest : ComponentTestFixture
530530
[Fact(DisplayName = "After first render, the new input field has focus")]
531531
public void Test001()
532532
{
533-
// Arrange - add the IJsRuntime mock
534-
var jsRtMock = Services.AddMockJsRuntime();
533+
// Arrange - add the IJSRuntime mock
534+
var jsRtMock = Services.AddMockJSRuntime();
535535

536536
// Act - render the FocussingInput component, causing
537537
// the OnAfterRender(firstRender: true) to be called

sample/tests/RazorTestComponents/Components/AlertRazorTest.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
@inherits TestComponentBase
22

33
@code {
4-
MockJsRuntimeInvokeHandler MockJsRuntime { get; set; } = default!;
4+
MockJSRuntimeInvokeHandler MockJSRuntime { get; set; } = default!;
55

66
void Setup(Fixture fixture)
77
{
8-
MockJsRuntime = fixture.Services.AddMockJsRuntime();
8+
MockJSRuntime = fixture.Services.AddMockJSRuntime();
99
}
1010
}
1111

@@ -190,7 +190,7 @@
190190
void Test007(Fixture fixture)
191191
{
192192
// arrange
193-
var plannedInvocation = MockJsRuntime.Setup<object>("window.transitionFinished");
193+
var plannedInvocation = MockJSRuntime.Setup<object>("window.transitionFinished");
194194
var cut = fixture.GetComponentUnderTest<Alert>();
195195

196196
// Act

sample/tests/RazorTestComponents/Components/TodoListTest.razor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@inherits TestComponentBase
22

3-
<Fixture Setup="(fixture) => fixture.Services.AddMockJsRuntime()"
3+
<Fixture Setup="(fixture) => fixture.Services.AddMockJSRuntime()"
44
Test="EmptyTodoListRendersCorrectly">
55
<ComponentUnderTest>
66
<TodoList>
@@ -36,7 +36,7 @@
3636
}
3737
</Fixture>
3838

39-
<Fixture Setup="(fixture) => fixture.Services.AddMockJsRuntime()"
39+
<Fixture Setup="(fixture) => fixture.Services.AddMockJSRuntime()"
4040
Test="SettingLabelRendersCorrectly">
4141
<ComponentUnderTest>
4242
<TodoList>
@@ -65,7 +65,7 @@
6565
}
6666
</Fixture>
6767

68-
<Fixture Setup="(fixture) => fixture.Services.AddMockJsRuntime()"
68+
<Fixture Setup="(fixture) => fixture.Services.AddMockJSRuntime()"
6969
Test="TaskListRendersItemsUsingItemTemplate">
7070
<ComponentUnderTest>
7171
<TodoList>
@@ -101,14 +101,14 @@
101101
</Fixture>
102102

103103
@code {
104-
MockJsRuntimeInvokeHandler jsRtMock = default!;
104+
MockJSRuntimeInvokeHandler jsRtMock = default!;
105105
Todo? createdTodo;
106106

107107
void OnAddingTodoHandler(Todo todo) => createdTodo = todo;
108108

109109
void Setup(Fixture fixture)
110110
{
111-
jsRtMock = fixture.Services.AddMockJsRuntime();
111+
jsRtMock = fixture.Services.AddMockJSRuntime();
112112
}
113113
}
114114

sample/tests/SampleApp.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="bunit" Version="1.0.0-beta-7" />
14+
<PackageReference Include="bunit" Version="1.0.0-beta-8" />
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
1616
<PackageReference Include="Moq" Version="4.13.1" />
1717
<PackageReference Include="Shouldly" Version="3.0.2" />

sample/tests/SnapshotTests/AlertSnapshotTest.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@inherits TestComponentBase
22

3-
<SnapshotTest Setup="(test) => test.Services.AddMockJsRuntime()"
3+
<SnapshotTest Setup="(test) => test.Services.AddMockJSRuntime()"
44
Description="Alert renders correctly when all input is provided">
55
<TestInput>
66
<Alert Header="It is time to focus on Blazor">

sample/tests/SnapshotTests/TodoListTest.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@inherits TestComponentBase
22

33
<SnapshotTest Description="A todolist with one todo added should render correctly"
4-
Setup="(test) => test.Services.AddMockJsRuntime()">
4+
Setup="(test) => test.Services.AddMockJSRuntime()">
55
<TestInput>
66
<TodoList Label="My label" Items=@(new Todo[]{ new Todo{ Id=42, Text="Check out this new thing called Blazor" } })>
77
<ItemsTemplate Context="todo">

sample/tests/Tests/Components/AlertTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
3-
using Bunit.Mocking.JSInterop;
3+
using Bunit.TestDoubles.JSInterop;
44
using SampleApp.Components;
55
using SampleApp.Data;
66
using Microsoft.AspNetCore.Authentication;
@@ -13,11 +13,11 @@ namespace SampleApp.Tests.Components
1313
{
1414
public class AlertTest2 : TestContext
1515
{
16-
MockJsRuntimeInvokeHandler MockJsRuntime { get; }
16+
MockJSRuntimeInvokeHandler MockJSRuntime { get; }
1717

1818
public AlertTest2()
1919
{
20-
MockJsRuntime = Services.AddMockJsRuntime();
20+
MockJSRuntime = Services.AddMockJSRuntime();
2121
}
2222

2323
[Fact(DisplayName = "Given no parameters, " +
@@ -156,8 +156,8 @@ public void Test006()
156156
public void Test()
157157
{
158158
// Arrange
159-
var mockJsRuntime = Services.AddMockJsRuntime();
160-
var plannedInvocation = mockJsRuntime.Setup<object>("window.transitionFinished");
159+
var mockJSRuntime = Services.AddMockJSRuntime();
160+
var plannedInvocation = mockJSRuntime.Setup<object>("window.transitionFinished");
161161

162162
DismissingEventArgs? dismissingEvent = default;
163163
Alert? dismissedAlert = default;
@@ -191,7 +191,7 @@ public void Test()
191191
public void Test007()
192192
{
193193
// Arrange
194-
var plannedInvocation = MockJsRuntime.Setup<object>("window.transitionFinished");
194+
var plannedInvocation = MockJSRuntime.Setup<object>("window.transitionFinished");
195195
var cut = RenderComponent<Alert>();
196196

197197
// Act - click the button

sample/tests/Tests/Components/FocussingInputTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6-
using Bunit.Mocking.JSInterop;
6+
using Bunit.TestDoubles.JSInterop;
77
using SampleApp.Components;
88
using Xunit;
99
using Bunit;
@@ -16,8 +16,8 @@ public class FocussingInputTest : TestContext
1616
[Fact(DisplayName = "After first render, the new input field has focus")]
1717
public void Test001()
1818
{
19-
// Arrange - add the IJsRuntime mock
20-
var jsRtMock = Services.AddMockJsRuntime();
19+
// Arrange - add the IJSRuntime mock
20+
var jsRtMock = Services.AddMockJSRuntime();
2121

2222
// Act - render the FocussingInput component, causing
2323
// the OnAfterRender(firstRender: true) to be called

sample/tests/Tests/Components/TodoListTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Shouldly;
22
using AngleSharp.Dom;
3-
using Bunit.Mocking.JSInterop;
3+
using Bunit.TestDoubles.JSInterop;
44
using SampleApp.Components;
55
using SampleApp.Data;
66
using Microsoft.AspNetCore.Components;
@@ -34,7 +34,7 @@ private string GetExpectedHtml(string label = "Task description", string itemsHt
3434
public void Test001()
3535
{
3636
// arrange
37-
Services.AddMockJsRuntime();
37+
Services.AddMockJSRuntime();
3838

3939
// act
4040
var cut = RenderComponent<TodoList>();
@@ -47,7 +47,7 @@ public void Test001()
4747
public void Test002()
4848
{
4949
// arrange
50-
Services.AddMockJsRuntime();
50+
Services.AddMockJSRuntime();
5151
var label = "hello world";
5252

5353
// act
@@ -61,7 +61,7 @@ public void Test002()
6161
public void Test003()
6262
{
6363
// arrange
64-
Services.AddMockJsRuntime();
64+
Services.AddMockJSRuntime();
6565
RenderFragment<Todo> itemTemplate = todo => builder => builder.AddMarkupContent(0, $"<li>{todo.Id}</li>");
6666
var items = new[] { new Todo { Id = 42 }, new Todo { Id = 1337 } };
6767

@@ -80,7 +80,7 @@ public void Test003()
8080
public void Test004()
8181
{
8282
// arrange
83-
var jsRtMock = Services.AddMockJsRuntime();
83+
var jsRtMock = Services.AddMockJSRuntime();
8484

8585
// act
8686
var cut = RenderComponent<TodoList>();
@@ -95,7 +95,7 @@ public void Test004()
9595
public void Test0041()
9696
{
9797
// arrange
98-
var jsRtMock = Services.AddMockJsRuntime();
98+
var jsRtMock = Services.AddMockJSRuntime();
9999

100100
// act
101101
var cut = RenderComponent<TodoList>(); // first render
@@ -110,7 +110,7 @@ public void Test0041()
110110
"the OnAddingTodo is raised with a new Todo containing the entered text")]
111111
public void Test005()
112112
{
113-
var jsRtMock = Services.AddMockJsRuntime();
113+
var jsRtMock = Services.AddMockJSRuntime();
114114
var taskValue = "HELLO WORLD TASK";
115115
var createdTask = default(Todo);
116116
var cut = RenderComponent<TodoList>(
@@ -127,7 +127,7 @@ public void Test005()
127127
[Fact(DisplayName = "When add task form is submitted with no text OnAddingTodo is not called")]
128128
public void Test006()
129129
{
130-
var jsRtMock = Services.AddMockJsRuntime();
130+
var jsRtMock = Services.AddMockJSRuntime();
131131
var createdTask = default(Todo);
132132
var cut = RenderComponent<TodoList>(
133133
EventCallback<Todo>(nameof(TodoList.OnAddingTodo), task => createdTask = task)

0 commit comments

Comments
 (0)