public async Task Test001()
        {
            var sut = new MockJsRuntimeInvokeHandler(JsRuntimeMockMode.Loose);

            var result = await sut.ToJsRuntime().InvokeAsync <object>("ident", Array.Empty <object>());

            result.ShouldBe(default);
Exemple #2
0
        public async Task Test002()
        {
            var identifier = "test";
            var handler    = new MockJsRuntimeInvokeHandler();
            await handler.ToJsRuntime().InvokeVoidAsync(identifier);

            Should.Throw <JsInvokeCountExpectedException>(() => handler.VerifyNotInvoke(identifier));
        }
Exemple #3
0
        public async Task Test003()
        {
            var identifier = "test";
            var errMsg     = "HELLO WORLD";
            var handler    = new MockJsRuntimeInvokeHandler();
            await handler.ToJsRuntime().InvokeVoidAsync(identifier);

            Should.Throw <JsInvokeCountExpectedException>(() => handler.VerifyNotInvoke(identifier, errMsg))
            .Message.ShouldContain(errMsg);
        }
 /// <summary>
 /// Verifies that the <paramref name="identifier"/> was never invoked on the <paramref name="handler"/>.
 /// </summary>
 /// <param name="handler">Handler to verify against.</param>
 /// <param name="identifier">Identifier of invocation that should not have happened.</param>
 /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
 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);
     }
 }
Exemple #5
0
        public async Task Test103()
        {
            var identifier = "test";
            var handler    = new MockJsRuntimeInvokeHandler();
            await handler.ToJsRuntime().InvokeVoidAsync(identifier);

            var actual = Should.Throw <JsInvokeCountExpectedException>(() => handler.VerifyInvoke(identifier, 2));

            actual.ExpectedInvocationCount.ShouldBe(2);
            actual.ActualInvocationCount.ShouldBe(1);
            actual.Identifier.ShouldBe(identifier);
        }
        /// <summary>
        /// Adds the <see cref="MockJsRuntimeInvokeHandler"/> to the <see cref="TestServiceProvider"/>.
        /// </summary>
        /// <returns>The added <see cref="MockJsRuntimeInvokeHandler"/>.</returns>
        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);
        }
Exemple #7
0
        public async Task Test104()
        {
            var identifier = "test";
            var handler    = new MockJsRuntimeInvokeHandler();
            await handler.ToJsRuntime().InvokeVoidAsync(identifier);

            var invocations = handler.VerifyInvoke(identifier, 1);

            invocations.ShouldBeSameAs(handler.Invocations[identifier]);

            var invocation = handler.VerifyInvoke(identifier);

            invocation.ShouldBe(handler.Invocations[identifier][0]);
        }
        /// <summary>
        /// Verifies that the <paramref name="identifier"/> has been invoked <paramref name="calledTimes"/> times.
        /// </summary>
        /// <param name="handler">Handler to verify against.</param>
        /// <param name="identifier">Identifier of invocation that should have been invoked.</param>
        /// <param name="calledTimes">The number of times the invocation is expected to have been called.</param>
        /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
        /// <returns>The <see cref="JsRuntimeInvocation"/>.</returns>
        public static IReadOnlyList <JsRuntimeInvocation> VerifyInvoke(this MockJsRuntimeInvokeHandler handler, string identifier, int calledTimes, string?userMessage = null)
        {
            if (handler is null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (calledTimes < 1)
            {
                throw new ArgumentException($"Use {nameof(VerifyNotInvoke)} to verify an identifier has not been invoked.", nameof(calledTimes));
            }

            if (!handler.Invocations.TryGetValue(identifier, out var invocations))
            {
                throw new JsInvokeCountExpectedException(identifier, calledTimes, 0, nameof(VerifyInvoke), userMessage);
            }

            if (invocations.Count != calledTimes)
            {
                throw new JsInvokeCountExpectedException(identifier, calledTimes, invocations.Count, nameof(VerifyInvoke), userMessage);
            }

            return(invocations);
        }
Exemple #9
0
        public void Test101()
        {
            var handler = new MockJsRuntimeInvokeHandler();

            Should.Throw <ArgumentException>(() => handler.VerifyInvoke("", 0));
        }
Exemple #10
0
        public void Test004()
        {
            var handler = new MockJsRuntimeInvokeHandler();

            handler.VerifyNotInvoke("FOOBAR");
        }
 public MockJsRuntime(MockJsRuntimeInvokeHandler mockJsRuntimeInvokeHandler)
 {
     _handlers = mockJsRuntimeInvokeHandler;
 }
 /// <summary>
 /// Verifies that the <paramref name="identifier"/> has been invoked one time.
 /// </summary>
 /// <param name="handler">Handler to verify against.</param>
 /// <param name="identifier">Identifier of invocation that should have been invoked.</param>
 /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
 /// <returns>The <see cref="JsRuntimeInvocation"/>.</returns>
 public static JsRuntimeInvocation VerifyInvoke(this MockJsRuntimeInvokeHandler handler, string identifier, string?userMessage = null)
 => VerifyInvoke(handler, identifier, 1, userMessage)[0];