|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using AngleSharp.Dom; |
| 7 | +using Egil.RazorComponents.Testing.Diffing; |
| 8 | +using Microsoft.AspNetCore.Components; |
| 9 | +using Microsoft.JSInterop; |
| 10 | +using Moq; |
| 11 | +using Shouldly; |
| 12 | +using Xunit; |
| 13 | +using Xunit.Sdk; |
| 14 | + |
| 15 | +namespace Egil.RazorComponents.Testing.Mocking.JSInterop |
| 16 | +{ |
| 17 | + public class JsRuntimeAssertExtensionsTest |
| 18 | + { |
| 19 | + [Fact(DisplayName = "VerifyNotInvoke throws if handler is null")] |
| 20 | + public void Test001() |
| 21 | + { |
| 22 | + MockJsRuntimeInvokeHandler? handler = null; |
| 23 | + Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.VerifyNotInvoke(handler!, "")); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact(DisplayName = "VerifyNotInvoke throws JsInvokeCountExpectedException if identifier " + |
| 27 | + "has been invoked one or more times")] |
| 28 | + public async Task Test002() |
| 29 | + { |
| 30 | + var identifier = "test"; |
| 31 | + var handler = new MockJsRuntimeInvokeHandler(); |
| 32 | + await handler.ToJsRuntime().InvokeVoidAsync(identifier); |
| 33 | + |
| 34 | + Should.Throw<JsInvokeCountExpectedException>(() => handler.VerifyNotInvoke(identifier)); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact(DisplayName = "VerifyNotInvoke throws JsInvokeCountExpectedException if identifier " + |
| 38 | + "has been invoked one or more times, with custom error message")] |
| 39 | + public async Task Test003() |
| 40 | + { |
| 41 | + var identifier = "test"; |
| 42 | + var errMsg = "HELLO WORLD"; |
| 43 | + var handler = new MockJsRuntimeInvokeHandler(); |
| 44 | + await handler.ToJsRuntime().InvokeVoidAsync(identifier); |
| 45 | + |
| 46 | + Should.Throw<JsInvokeCountExpectedException>(() => handler.VerifyNotInvoke(identifier, errMsg)) |
| 47 | + .UserMessage.ShouldEndWith(errMsg); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact(DisplayName = "VerifyNotInvoke does not throw if identifier has not been invoked")] |
| 51 | + public void Test004() |
| 52 | + { |
| 53 | + var handler = new MockJsRuntimeInvokeHandler(); |
| 54 | + |
| 55 | + handler.VerifyNotInvoke("FOOBAR"); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact(DisplayName = "VerifyInvoke throws if handler is null")] |
| 59 | + public void Test100() |
| 60 | + { |
| 61 | + MockJsRuntimeInvokeHandler? handler = null; |
| 62 | + Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.VerifyInvoke(handler!, "")); |
| 63 | + Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.VerifyInvoke(handler!, "", 42)); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact(DisplayName = "VerifyInvoke throws invokeCount is less than 1")] |
| 67 | + public void Test101() |
| 68 | + { |
| 69 | + var handler = new MockJsRuntimeInvokeHandler(); |
| 70 | + |
| 71 | + Should.Throw<ArgumentException>(() => handler.VerifyInvoke("", 0)); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact(DisplayName = "VerifyInvoke throws JsInvokeCountExpectedException when " + |
| 75 | + "invocation count doesn't match the expected")] |
| 76 | + public async Task Test103() |
| 77 | + { |
| 78 | + var identifier = "test"; |
| 79 | + var handler = new MockJsRuntimeInvokeHandler(); |
| 80 | + await handler.ToJsRuntime().InvokeVoidAsync(identifier); |
| 81 | + |
| 82 | + var actual = Should.Throw<JsInvokeCountExpectedException>(() => handler.VerifyInvoke(identifier, 2)); |
| 83 | + actual.ExpectedInvocationCount.ShouldBe(2); |
| 84 | + actual.ActualInvocationCount.ShouldBe(1); |
| 85 | + actual.Identifier.ShouldBe(identifier); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact(DisplayName = "VerifyInvoke returns the invocation(s) if the expected count matched")] |
| 89 | + public async Task Test104() |
| 90 | + { |
| 91 | + var identifier = "test"; |
| 92 | + var handler = new MockJsRuntimeInvokeHandler(); |
| 93 | + await handler.ToJsRuntime().InvokeVoidAsync(identifier); |
| 94 | + |
| 95 | + var invocations = handler.VerifyInvoke(identifier, 1); |
| 96 | + invocations.ShouldBeSameAs(handler.Invocations[identifier]); |
| 97 | + |
| 98 | + var invocation = handler.VerifyInvoke(identifier); |
| 99 | + invocation.ShouldBe(handler.Invocations[identifier][0]); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact(DisplayName = "ShouldBeElementReferenceTo throws if actualArgument or targeted element is null")] |
| 103 | + public void Test200() |
| 104 | + { |
| 105 | + Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.ShouldBeElementReferenceTo(null!, null!)) |
| 106 | + .ParamName.ShouldBe("actualArgument"); |
| 107 | + Should.Throw<ArgumentNullException>(() => JsRuntimeAssertExtensions.ShouldBeElementReferenceTo(string.Empty, null!)) |
| 108 | + .ParamName.ShouldBe("expectedTargetElement"); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact(DisplayName = "ShouldBeElementReferenceTo throws if actualArgument is not a ElementReference")] |
| 112 | + public void Test201() |
| 113 | + { |
| 114 | + var obj = new object(); |
| 115 | + Should.Throw<IsTypeException>(() => obj.ShouldBeElementReferenceTo(Mock.Of<IElement>())); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact(DisplayName = "ShouldBeElementReferenceTo throws if element reference does not point to the provided element")] |
| 119 | + public void Test202() |
| 120 | + { |
| 121 | + using var htmlParser = new TestHtmlParser(); |
| 122 | + var elmRef = new ElementReference(Guid.NewGuid().ToString()); |
| 123 | + var elm = htmlParser.Parse($"<p {Htmlizer.ELEMENT_REFERENCE_ATTR_NAME}=\"ASDF\" />").First() as IElement; |
| 124 | + |
| 125 | + Should.Throw<AssertActualExpectedException>(() => elmRef.ShouldBeElementReferenceTo(elm)); |
| 126 | + |
| 127 | + var elmWithoutRefAttr = htmlParser.Parse($"<p />").First() as IElement; |
| 128 | + |
| 129 | + Should.Throw<AssertActualExpectedException>(() => elmRef.ShouldBeElementReferenceTo(elmWithoutRefAttr)); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments