Skip to content

Commit 930a95b

Browse files
committed
Changed bunit jsruntime such that it picks the last matching handler instead of expecting a single or none
1 parent 30227f0 commit 930a95b

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/bunit.web/JSInterop/BunitJSInterop.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToke
170170
ValueTask<TValue>? result = default;
171171
if (_jsInterop._handlers.TryGetValue(identifier, out var plannedInvocations))
172172
{
173-
var planned = plannedInvocations.OfType<JSRuntimeInvocationHandlerBase<TValue>>()
174-
.SingleOrDefault(x => x.Matches(invocation));
173+
var handler = plannedInvocations.OfType<JSRuntimeInvocationHandlerBase<TValue>>()
174+
.Where(x => x.Matches(invocation)).LastOrDefault();
175175

176-
if (planned is not null)
176+
if (handler is not null)
177177
{
178-
var task = planned.RegisterInvocation(invocation);
178+
var task = handler.RegisterInvocation(invocation);
179179
return new ValueTask<TValue>(task);
180180
}
181181
}

tests/bunit.web.tests/JSInterop/BunitJSInteropTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,5 +382,19 @@ public async Task Test022()
382382
handler.Invocations.Count.ShouldBe(1);
383383
plannedCatchall.Invocations.Count.ShouldBe(0);
384384
}
385+
386+
[Fact(DisplayName = "The last handler matching an invocation receives the invocation")]
387+
public void Test030()
388+
{
389+
var identifier = "someFunc";
390+
var sut = CreateSut(JSRuntimeMode.Strict);
391+
var h1 = sut.Setup<string>(identifier);
392+
var h2 = sut.Setup<string>(identifier);
393+
394+
sut.JSRuntime.InvokeAsync<string>(identifier);
395+
396+
h1.Invocations.ShouldBeEmpty();
397+
h2.Invocations.Count.ShouldBe(1);
398+
}
385399
}
386400
}

0 commit comments

Comments
 (0)