22using System . Collections . Generic ;
33using AngleSharp . Dom ;
44using Bunit . Asserting ;
5+ using Bunit . JSInterop ;
6+ using Bunit . JSInterop . InvocationHandlers ;
57using Microsoft . AspNetCore . Components ;
68
79namespace Bunit
@@ -11,21 +13,44 @@ namespace Bunit
1113 /// </summary>
1214 public static class JSRuntimeAssertExtensions
1315 {
16+ /// <summary>
17+ /// Verifies that the <paramref name="identifier"/> was never invoked on the <paramref name="jsInterop"/>.
18+ /// </summary>
19+ /// <param name="jsInterop">The bUnit JSInterop to verify against.</param>
20+ /// <param name="identifier">Identifier of invocation that should not have happened.</param>
21+ /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
22+ public static void VerifyNotInvoke ( this BunitJSInterop jsInterop , string identifier , string ? userMessage = null )
23+ => VerifyNotInvoke ( jsInterop ? . Invocations ?? throw new ArgumentNullException ( nameof ( jsInterop ) ) , identifier , userMessage ) ;
24+
1425 /// <summary>
1526 /// Verifies that the <paramref name="identifier"/> was never invoked on the <paramref name="handler"/>.
1627 /// </summary>
1728 /// <param name="handler">Handler to verify against.</param>
1829 /// <param name="identifier">Identifier of invocation that should not have happened.</param>
1930 /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
20- public static void VerifyNotInvoke ( this BunitJSInterop handler , string identifier , string ? userMessage = null )
21- {
22- if ( handler is null )
23- throw new ArgumentNullException ( nameof ( handler ) ) ;
24- if ( handler . Invocations . TryGetValue ( identifier , out var invocations ) && invocations . Count > 0 )
25- {
26- throw new JSInvokeCountExpectedException ( identifier , 0 , invocations . Count , nameof ( VerifyNotInvoke ) , userMessage ) ;
27- }
28- }
31+ public static void VerifyNotInvoke < TResult > ( this JSRuntimeInvocationHandlerBase < TResult > handler , string identifier , string ? userMessage = null )
32+ => VerifyNotInvoke ( handler ? . Invocations ?? throw new ArgumentNullException ( nameof ( handler ) ) , identifier , userMessage ) ;
33+
34+ /// <summary>
35+ /// Verifies that the <paramref name="identifier"/> has been invoked one time.
36+ /// </summary>
37+ /// <param name="jsInterop">The bUnit JSInterop to verify against.</param>
38+ /// <param name="identifier">Identifier of invocation that should have been invoked.</param>
39+ /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
40+ /// <returns>The <see cref="JSRuntimeInvocation"/>.</returns>
41+ public static JSRuntimeInvocation VerifyInvoke ( this BunitJSInterop jsInterop , string identifier , string ? userMessage = null )
42+ => jsInterop . VerifyInvoke ( identifier , 1 , userMessage ) [ 0 ] ;
43+
44+ /// <summary>
45+ /// Verifies that the <paramref name="identifier"/> has been invoked <paramref name="calledTimes"/> times.
46+ /// </summary>
47+ /// <param name="jsInterop">The bUnit JSInterop to verify against.</param>
48+ /// <param name="identifier">Identifier of invocation that should have been invoked.</param>
49+ /// <param name="calledTimes">The number of times the invocation is expected to have been called.</param>
50+ /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
51+ /// <returns>The <see cref="JSRuntimeInvocation"/>.</returns>
52+ public static IReadOnlyList < JSRuntimeInvocation > VerifyInvoke ( this BunitJSInterop jsInterop , string identifier , int calledTimes , string ? userMessage = null )
53+ => VerifyInvoke ( jsInterop ? . Invocations ?? throw new ArgumentNullException ( nameof ( jsInterop ) ) , identifier , calledTimes , userMessage ) ;
2954
3055 /// <summary>
3156 /// Verifies that the <paramref name="identifier"/> has been invoked one time.
@@ -34,7 +59,7 @@ public static void VerifyNotInvoke(this BunitJSInterop handler, string identifie
3459 /// <param name="identifier">Identifier of invocation that should have been invoked.</param>
3560 /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
3661 /// <returns>The <see cref="JSRuntimeInvocation"/>.</returns>
37- public static JSRuntimeInvocation VerifyInvoke ( this BunitJSInterop handler , string identifier , string ? userMessage = null )
62+ public static JSRuntimeInvocation VerifyInvoke < TResult > ( this JSRuntimeInvocationHandlerBase < TResult > handler , string identifier , string ? userMessage = null )
3863 => handler . VerifyInvoke ( identifier , 1 , userMessage ) [ 0 ] ;
3964
4065 /// <summary>
@@ -45,26 +70,8 @@ public static JSRuntimeInvocation VerifyInvoke(this BunitJSInterop handler, stri
4570 /// <param name="calledTimes">The number of times the invocation is expected to have been called.</param>
4671 /// <param name="userMessage">A custom user message to display if the assertion fails.</param>
4772 /// <returns>The <see cref="JSRuntimeInvocation"/>.</returns>
48- public static IReadOnlyList < JSRuntimeInvocation > VerifyInvoke ( this BunitJSInterop handler , string identifier , int calledTimes , string ? userMessage = null )
49- {
50- if ( handler is null )
51- throw new ArgumentNullException ( nameof ( handler ) ) ;
52-
53- if ( calledTimes < 1 )
54- throw new ArgumentException ( $ "Use { nameof ( VerifyNotInvoke ) } to verify an identifier has not been invoked.", nameof ( calledTimes ) ) ;
55-
56- if ( ! handler . Invocations . TryGetValue ( identifier , out var invocations ) )
57- {
58- throw new JSInvokeCountExpectedException ( identifier , calledTimes , 0 , nameof ( VerifyInvoke ) , userMessage ) ;
59- }
60-
61- if ( invocations . Count != calledTimes )
62- {
63- throw new JSInvokeCountExpectedException ( identifier , calledTimes , invocations . Count , nameof ( VerifyInvoke ) , userMessage ) ;
64- }
65-
66- return invocations ;
67- }
73+ public static IReadOnlyList < JSRuntimeInvocation > VerifyInvoke < TResult > ( this JSRuntimeInvocationHandlerBase < TResult > handler , string identifier , int calledTimes , string ? userMessage = null )
74+ => VerifyInvoke ( handler ? . Invocations ?? throw new ArgumentNullException ( nameof ( handler ) ) , identifier , calledTimes , userMessage ) ;
6875
6976 /// <summary>
7077 /// Verifies that an argument <paramref name="actualArgument"/>
@@ -93,5 +100,41 @@ public static void ShouldBeElementReferenceTo(this object? actualArgument, IElem
93100 "Element does not have a the expected element reference." ) ;
94101 }
95102 }
103+
104+ private static IReadOnlyList < JSRuntimeInvocation > VerifyInvoke ( JSRuntimeInvocationDictionary allInvocations , string identifier , int calledTimes , string ? userMessage = null )
105+ {
106+ if ( string . IsNullOrWhiteSpace ( identifier ) )
107+ throw new ArgumentException ( $ "'{ nameof ( identifier ) } ' cannot be null or whitespace.", nameof ( identifier ) ) ;
108+
109+ if ( calledTimes < 1 )
110+ throw new ArgumentException ( $ "Use { nameof ( VerifyNotInvoke ) } to verify an identifier has not been invoked.", nameof ( calledTimes ) ) ;
111+
112+ var invocations = allInvocations [ identifier ] ;
113+
114+ if ( invocations . Count == 0 )
115+ {
116+ throw new JSInvokeCountExpectedException ( identifier , calledTimes , 0 , nameof ( VerifyInvoke ) , userMessage ) ;
117+ }
118+
119+ if ( invocations . Count != calledTimes )
120+ {
121+ throw new JSInvokeCountExpectedException ( identifier , calledTimes , allInvocations . Count , nameof ( VerifyInvoke ) , userMessage ) ;
122+ }
123+
124+ return invocations ;
125+ }
126+
127+ private static void VerifyNotInvoke ( JSRuntimeInvocationDictionary allInvocations , string identifier , string ? userMessage = null )
128+ {
129+ if ( string . IsNullOrWhiteSpace ( identifier ) )
130+ throw new ArgumentException ( $ "'{ nameof ( identifier ) } ' cannot be null or whitespace.", nameof ( identifier ) ) ;
131+
132+ var invocationCount = allInvocations [ identifier ] . Count ;
133+
134+ if ( invocationCount > 0 )
135+ {
136+ throw new JSInvokeCountExpectedException ( identifier , 0 , invocationCount , nameof ( VerifyNotInvoke ) , userMessage ) ;
137+ }
138+ }
96139 }
97140}
0 commit comments