Repository navigation
Conversation
# Conflicts: # src/BootstrapBlazor/Components/Toast/Toast.razor.js
Reviewer's GuideImplementationGuard SweetAlert close and confirm actions so overlapping calls do not rerun the callback, while still closing the modal after the action completes.
Make ShowModal detect when a request was ignored by comparing confirmation contexts, return false in that case, and restore the option’s original IsConfirm value.
TestsCover modal confirmation results, ignored requests while another alert is active, and suppression of concurrent confirm actions.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8777 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 663 663
Lines 30364 30377 +13
=========================================
+ Hits 30364 30377 +13
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Linked issuesChecked at commit f884b22 #8776 bug(Swal): handle concurrent modal requests and actions · 3 of 3 met
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs" line_range="199-201" />
<code_context>
+ {
+ if (callback != null)
+ {
+ await callback();
+ }
+ _actionCompleted = true;
+ }
+
</code_context>
<issue_to_address>
**Callbacks repeat after exceptions**
When a close or confirm callback performs a side effect and then throws, and the user retries the action, `CloseAsync` leaves `_actionCompleted` false and resets `_isClosing`, so a later click invokes the callback again and repeats the side effect.
Prevent the callback from being invoked again after it has performed a side effect and thrown.
</issue_to_address>
### Comment 2
<location path="src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs" line_range="201" />
<code_context>
+ {
+ await callback();
+ }
+ _actionCompleted = true;
+ }
+
</code_context>
<issue_to_address>
**Auto-hide bypasses action guard**
When an auto-hide timer expires while a user-triggered close callback is in progress, the auto-hide path invokes `option.OnCloseAsync` directly while the button path invokes it through `CloseAsync`; if the timer expires while a close-button callback is running, both paths execute the callback and duplicate its side effects.
Route auto-hide through the same shared once-only close-action guard as button-triggered closes.
</issue_to_address>
### Comment 3
<location path="test/UnitTest/Components/SwalTest.cs" line_range="30" />
<code_context>
+
+ var modal = cut.FindComponents<Modal>().Last();
+ await cut.InvokeAsync(() => cut.Find(expected ? ".btn-danger" : ".btn-secondary").Click());
+ await cut.InvokeAsync(() => modal.Instance.CloseCallback());
+
+ Assert.Equal(expected, await modalTask);
</code_context>
<issue_to_address>
**Cancel result passes without canceling**
When the secondary-button handler fails to set the confirmation value and the modal is still closed, in `ShowModal_ReturnsConfirmationResult_Ok(false)`, the assertion at `SwalTest.cs:32` still passes if the secondary-button handler never writes `false`: line 30 calls `CloseCallback` directly, which resolves the task from the default-false `SweetContext.Value`, so the broken cancel action is reported as successful.
Make the cancellation case fail unless the secondary-button handler ran, for example by asserting an observable callback side effect before invoking `CloseCallback`.
</issue_to_address>
### Comment 4
<location path="test/UnitTest/Components/SwalTest.cs" line_range="10-118" />
<code_context>
public class SwalTest : BootstrapBlazorTestBase
{
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public async Task ShowModal_ReturnsConfirmationResult_Ok(bool expected)
+ {
+ var cut = Context.Render<BootstrapBlazorRoot>(pb =>
+ {
+ pb.AddChildContent<MockSwalTest>();
+ });
+ var swal = cut.FindComponent<MockSwalTest>().Instance.SwalService;
+ var option = new SwalOption();
+ var modalTask = Task.FromResult(false);
+
+ await cut.InvokeAsync(() =>
+ {
</code_context>
<issue_to_address>
**Unrequested unit tests were added**
When the defect-fix task does not explicitly request new unit tests, `SwalTest` adds three unit tests even though no request to add tests was made, contrary to the repository’s default for defect fixes.
Remove the newly added unit tests unless adding tests is explicitly requested.
</issue_to_address>Sourcery assessment
Approval pending. 3 findings to address first.
Blocking findings: src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs:201, src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs:201, test/UnitTest/Components/SwalTest.cs:30
| await callback(); | ||
| } | ||
| _actionCompleted = true; |
There was a problem hiding this comment.
🟡 Medium · Callbacks repeat after exceptions
When a close or confirm callback performs a side effect and then throws, and the user retries the action, CloseAsync leaves _actionCompleted false and resets _isClosing, so a later click invokes the callback again and repeats the side effect.
Prevent the callback from being invoked again after it has performed a side effect and thrown.
Prompt for AI agents
In `src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs` at lines 199-201:
**Callbacks repeat after exceptions**
When a close or confirm callback performs a side effect and then throws, and the user retries the action, `CloseAsync` leaves `_actionCompleted` false and resets `_isClosing`, so a later click invokes the callback again and repeats the side effect.
Prevent the callback from being invoked again after it has performed a side effect and thrown.| { | ||
| await callback(); | ||
| } | ||
| _actionCompleted = true; |
There was a problem hiding this comment.
🟡 Medium · Auto-hide bypasses action guard
When an auto-hide timer expires while a user-triggered close callback is in progress, the auto-hide path invokes option.OnCloseAsync directly while the button path invokes it through CloseAsync; if the timer expires while a close-button callback is running, both paths execute the callback and duplicate its side effects.
Route auto-hide through the same shared once-only close-action guard as button-triggered closes.
Prompt for AI agents
In `src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs` at line 201:
**Auto-hide bypasses action guard**
When an auto-hide timer expires while a user-triggered close callback is in progress, the auto-hide path invokes `option.OnCloseAsync` directly while the button path invokes it through `CloseAsync`; if the timer expires while a close-button callback is running, both paths execute the callback and duplicate its side effects.
Route auto-hide through the same shared once-only close-action guard as button-triggered closes.|
|
||
| var modal = cut.FindComponents<Modal>().Last(); | ||
| await cut.InvokeAsync(() => cut.Find(expected ? ".btn-danger" : ".btn-secondary").Click()); | ||
| await cut.InvokeAsync(() => modal.Instance.CloseCallback()); |
There was a problem hiding this comment.
🟡 Medium · Cancel result passes without canceling
When the secondary-button handler fails to set the confirmation value and the modal is still closed, in ShowModal_ReturnsConfirmationResult_Ok(false), the assertion at SwalTest.cs:32 still passes if the secondary-button handler never writes false: line 30 calls CloseCallback directly, which resolves the task from the default-false SweetContext.Value, so the broken cancel action is reported as successful.
Make the cancellation case fail unless the secondary-button handler ran, for example by asserting an observable callback side effect before invoking CloseCallback.
Prompt for AI agents
In `test/UnitTest/Components/SwalTest.cs` at line 30:
**Cancel result passes without canceling**
When the secondary-button handler fails to set the confirmation value and the modal is still closed, in `ShowModal_ReturnsConfirmationResult_Ok(false)`, the assertion at `SwalTest.cs:32` still passes if the secondary-button handler never writes `false`: line 30 calls `CloseCallback` directly, which resolves the task from the default-false `SweetContext.Value`, so the broken cancel action is reported as successful.
Make the cancellation case fail unless the secondary-button handler ran, for example by asserting an observable callback side effect before invoking `CloseCallback`.| [Theory] | ||
| [InlineData(true)] | ||
| [InlineData(false)] | ||
| public async Task ShowModal_ReturnsConfirmationResult_Ok(bool expected) | ||
| { | ||
| var cut = Context.Render<BootstrapBlazorRoot>(pb => | ||
| { | ||
| pb.AddChildContent<MockSwalTest>(); | ||
| }); | ||
| var swal = cut.FindComponent<MockSwalTest>().Instance.SwalService; | ||
| var option = new SwalOption(); | ||
| var modalTask = Task.FromResult(false); | ||
|
|
||
| await cut.InvokeAsync(() => | ||
| { | ||
| modalTask = swal.ShowModal(option); | ||
| }); | ||
|
|
||
| var modal = cut.FindComponents<Modal>().Last(); | ||
| await cut.InvokeAsync(() => cut.Find(expected ? ".btn-danger" : ".btn-secondary").Click()); | ||
| await cut.InvokeAsync(() => modal.Instance.CloseCallback()); | ||
|
|
||
| Assert.Equal(expected, await modalTask); | ||
| Assert.True(option.IsConfirm); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShowModal_IgnoresRequestsWhileBusy_Ok() | ||
| { | ||
| var cut = Context.Render<BootstrapBlazorRoot>(pb => | ||
| { | ||
| pb.AddChildContent<MockSwalTest>(); | ||
| }); | ||
| var swal = cut.FindComponent<MockSwalTest>().Instance.SwalService; | ||
| var option = new SwalOption() | ||
| { | ||
| Content = "Active Swal" | ||
| }; | ||
| var modalTask = Task.FromResult(false); | ||
|
|
||
| await cut.InvokeAsync(() => | ||
| { | ||
| modalTask = swal.ShowModal(option); | ||
| }); | ||
|
|
||
| var newOption = new SwalOption() | ||
| { | ||
| Content = "Ignored Swal" | ||
| }; | ||
| var newRequestTask = Task.FromResult(true); | ||
| await cut.InvokeAsync(() => | ||
| { | ||
| newRequestTask = swal.ShowModal(newOption); | ||
| }); | ||
|
|
||
| Assert.False(await newRequestTask); | ||
| Assert.False(newOption.IsConfirm); | ||
|
|
||
| var repeatedRequestTask = Task.FromResult(true); | ||
| await cut.InvokeAsync(() => | ||
| { | ||
| repeatedRequestTask = swal.ShowModal(option); | ||
| }); | ||
|
|
||
| Assert.False(await repeatedRequestTask); | ||
| Assert.True(option.IsConfirm); | ||
| Assert.Contains("Active Swal", cut.Markup); | ||
| Assert.DoesNotContain("Ignored Swal", cut.Markup); | ||
|
|
||
| var modal = cut.FindComponents<Modal>().Last(); | ||
| await cut.InvokeAsync(() => cut.Find(".btn-danger").Click()); | ||
| await cut.InvokeAsync(() => modal.Instance.CloseCallback()); | ||
|
|
||
| Assert.True(await modalTask); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CloseAsync_IgnoresConcurrentCall_Ok() | ||
| { | ||
| var callbackStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var continueCallback = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var callbackCount = 0; | ||
| var cut = Context.Render<SweetAlertBody>(pb => | ||
| { | ||
| pb.Add(a => a.IsConfirm, true); | ||
| pb.Add(a => a.OnConfirmAsync, async () => | ||
| { | ||
| callbackCount++; | ||
| callbackStarted.TrySetResult(); | ||
| await continueCallback.Task; | ||
| }); | ||
| }); | ||
|
|
||
| var firstClick = cut.InvokeAsync(() => cut.Find(".btn-danger").Click()); | ||
| try | ||
| { | ||
| await callbackStarted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken); | ||
| await cut.InvokeAsync(() => cut.Find(".btn-danger").Click()); | ||
|
|
||
| Assert.Equal(1, callbackCount); | ||
| } | ||
| finally | ||
| { | ||
| continueCallback.TrySetResult(); | ||
| } | ||
|
|
||
| await firstClick; | ||
| } | ||
|
|
There was a problem hiding this comment.
⚪ Low · Unrequested unit tests were added
When the defect-fix task does not explicitly request new unit tests, SwalTest adds three unit tests even though no request to add tests was made, contrary to the repository’s default for defect fixes.
Remove the newly added unit tests unless adding tests is explicitly requested.
Prompt for AI agents
In `test/UnitTest/Components/SwalTest.cs` at lines 10-118:
**Unrequested unit tests were added**
When the defect-fix task does not explicitly request new unit tests, `SwalTest` adds three unit tests even though no request to add tests was made, contrary to the repository’s default for defect fixes.
Remove the newly added unit tests unless adding tests is explicitly requested.From .github/copilot-instructions.md L10:
普通功能开发、缺陷修复等任务,默认不新增、修改或运行单元测试,除非用户明确要求
Link issues
fixes #8776
Summary By Copilot
falsewhenShowModalis ignored because a SweetAlert is already active, including requests with a missing or previously-used confirmation context.SweetAlertBody.Regression?
No known regression; the changes are limited to SweetAlert modal request and action handling.
Risk
The changes coordinate confirmation contexts and asynchronous callbacks when modal requests or actions overlap.
Verification
Manual (required)
Automated
dotnet test test/UnitTest/UnitTest.csproj --no-restore --filter "FullyQualifiedName~UnitTest.Components.SwalTest.ShowModal_"— passed (3 tests).dotnet test test/UnitTest/UnitTest.csproj --no-restore --filter "FullyQualifiedName~UnitTest.Components.SwalTest.CloseAsync_IgnoresConcurrentCall_Ok"— passed (1 test).git diff --check origin/main...HEAD— passed.Manual browser validation was not run.
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Harden SweetAlert modal and action handling against concurrent requests and repeated user actions.
Bug Fixes:
Enhancements:
Tests: