Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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`

### Deprecated
List of soon-to-be removed features.
Expand Down
4 changes: 2 additions & 2 deletions src/bunit.core.tests/ComponentParameterFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AllTypesOfParams<string>>(
("some-unmatched-attribute", "unmatched value"),
Expand Down Expand Up @@ -48,7 +48,7 @@ public void Test005()
public void Test002()
{
// arrange
Services.AddMockJsRuntime();
Services.AddMockJSRuntime();
var cut = RenderComponent<AllTypesOfParams<string>>();

// assert that no parameters have been set initially
Expand Down
2 changes: 1 addition & 1 deletion src/bunit.testassets/SampleComponents/AllTypesOfParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Bunit.TestAssets.SampleComponents
public class AllTypesOfParams<TItem> : ComponentBase
{
[Inject]
public IJSRuntime? JsRuntime { get; set; }
public IJSRuntime? JSRuntime { get; set; }

[Parameter(CaptureUnmatchedValues = true)]
public IReadOnlyDictionary<string, object> Attributes { get; set; } = default!;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inject IJSRuntime jsRuntime
@inject IJSRuntime jsRuntime
<p>@name</p>
@code{
string name = string.Empty;
Expand All @@ -11,4 +11,4 @@
StateHasChanged();
}
}
}
}
16 changes: 8 additions & 8 deletions src/bunit.web.tests/BlazorE2E/ComponentRenderingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,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<ElementRefComponent>();
// var inputElement = cut.Find("#capturedElement");
Expand All @@ -382,14 +382,14 @@ public void CanUseJsInteropToReferenceElements()
// buttonElement.Click();
// Assert.Equal("Clicks: 2", inputElement.GetAttribute("value"));

var mockJs = Services.AddMockJsRuntime();
var mockJS = Services.AddMockJSRuntime();
var cut = RenderComponent<ElementRefComponent>();
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<ElementReference>()
.Id.ShouldBe(refId);
Expand All @@ -398,7 +398,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<ElementRefComponent>();
//var buttonElement = cut.Find("button");
Expand All @@ -421,7 +421,7 @@ public void CanCaptureReferencesToDynamicallyAddedElements()
//buttonElement.Click();
//Assert.Equal("Clicks: 1", () => inputElement.GetAttribute("value"));

var mockJs = Services.AddMockJsRuntime();
var mockJS = Services.AddMockJSRuntime();

var cut = RenderComponent<ElementRefComponent>();
var buttonElement = cut.Find("button");
Expand All @@ -443,7 +443,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<ElementReference>()
.Id.ShouldBe(refId);
Expand Down Expand Up @@ -481,7 +481,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<AfterRenderInteropComponent>();
// Assert.Equal("Value set after render", () => Browser.Find("input").GetAttribute("value"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,72 +18,72 @@

namespace Bunit.Mocking.JSInterop
{
public class JsRuntimeAssertExtensionsTest
public class JSRuntimeAssertExtensionsTest
{
[Fact(DisplayName = "VerifyNotInvoke throws if handler is null")]
public void Test001()
{
MockJsRuntimeInvokeHandler? handler = null;
Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.VerifyNotInvoke(handler!, ""));
MockJSRuntimeInvokeHandler? handler = null;
Should.Throw<ArgumentNullException>(() => JSRuntimeAssertExtensions.VerifyNotInvoke(handler!, ""));
}

[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<JsInvokeCountExpectedException>(() => handler.VerifyNotInvoke(identifier));
Should.Throw<JSInvokeCountExpectedException>(() => 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<JsInvokeCountExpectedException>(() => handler.VerifyNotInvoke(identifier, errMsg))
Should.Throw<JSInvokeCountExpectedException>(() => 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");
}

[Fact(DisplayName = "VerifyInvoke throws if handler is null")]
public void Test100()
{
MockJsRuntimeInvokeHandler? handler = null;
Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.VerifyInvoke(handler!, ""));
Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.VerifyInvoke(handler!, "", 42));
MockJSRuntimeInvokeHandler? handler = null;
Should.Throw<ArgumentNullException>(() => JSRuntimeAssertExtensions.VerifyInvoke(handler!, ""));
Should.Throw<ArgumentNullException>(() => JSRuntimeAssertExtensions.VerifyInvoke(handler!, "", 42));
}

[Fact(DisplayName = "VerifyInvoke throws invokeCount is less than 1")]
public void Test101()
{
var handler = new MockJsRuntimeInvokeHandler();
var handler = new MockJSRuntimeInvokeHandler();

Should.Throw<ArgumentException>(() => 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<JsInvokeCountExpectedException>(() => handler.VerifyInvoke(identifier, 2));
var actual = Should.Throw<JSInvokeCountExpectedException>(() => handler.VerifyInvoke(identifier, 2));
actual.ExpectedInvocationCount.ShouldBe(2);
actual.ActualInvocationCount.ShouldBe(1);
actual.Identifier.ShouldBe(identifier);
Expand All @@ -93,8 +93,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]);
Expand All @@ -106,9 +106,9 @@ public async Task Test104()
[Fact(DisplayName = "ShouldBeElementReferenceTo throws if actualArgument or targeted element is null")]
public void Test200()
{
Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.ShouldBeElementReferenceTo(null!, null!))
Should.Throw<ArgumentNullException>(() => JSRuntimeAssertExtensions.ShouldBeElementReferenceTo(null!, null!))
.ParamName.ShouldBe("actualArgument");
Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.ShouldBeElementReferenceTo(string.Empty, null!))
Should.Throw<ArgumentNullException>(() => JSRuntimeAssertExtensions.ShouldBeElementReferenceTo(string.Empty, null!))
.ParamName.ShouldBe("expectedTargetElement");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading;

Expand All @@ -8,19 +8,19 @@

namespace Bunit.Mocking.JSInterop
{
public class JsRuntimeInvocationTest
public class JSRuntimeInvocationTest
{
public static IEnumerable<object[]> 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<object>());
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<object>());
var i6 = new JSRuntimeInvocation("foo", token, new object[] { 2, "woop" });

yield return new object[] { i1, i1, true };
yield return new object[] { i1, i2, true };
Expand All @@ -32,7 +32,7 @@ public static IEnumerable<object[]> 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);
Expand All @@ -45,12 +45,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);
}
Expand Down
Loading