Skip to content

Commit

Permalink
Change DiagramCanvas to await unsubscription to Resizes subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonamAnjos authored and toluwtglobal committed Jun 27, 2024
1 parent ac4eef3 commit 6dd0b38
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Blazor.Diagrams.Components;

public partial class DiagramCanvas : IDisposable
public partial class DiagramCanvas : IAsyncDisposable
{
private DotNetObjectReference<DiagramCanvas>? _reference;
private bool _shouldRender;
Expand All @@ -27,15 +27,15 @@ public partial class DiagramCanvas : IDisposable

[Inject] public IJSRuntime JSRuntime { get; set; } = null!;

public void Dispose()
public async ValueTask DisposeAsync()
{
BlazorDiagram.Changed -= OnDiagramChanged;

if (_reference == null)
return;

if (elementReference.Id != null)
_ = JSRuntime.UnobserveResizes(elementReference);
await JSRuntime.UnobserveResizes(elementReference);

_reference.Dispose();
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Blazor.Diagrams.Tests/Components/DiagramCanvasTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Blazor.Diagrams.Components;
using Blazor.Diagrams.Core.Geometry;
using Bunit;
using Xunit;

namespace Blazor.Diagrams.Tests.Components
{
public class DiagramCanvasTests
{
[Fact]
public void Behavior_WhenDisposing_ShouldUnsubscribeToResizes()
{
// Arrange
JSRuntimeInvocationHandler call;
using (var ctx = new TestContext())
{
ctx.JSInterop.Setup<Rectangle>("ZBlazorDiagrams.getBoundingClientRect", _ => true);
call = ctx.JSInterop.SetupVoid("ZBlazorDiagrams.unobserve", _ => true).SetVoidResult();

// Act
var cut = ctx.RenderComponent<DiagramCanvas>(p => p.Add(n => n.BlazorDiagram, new BlazorDiagram()));
}

// Assert
call.VerifyInvoke("ZBlazorDiagrams.unobserve", calledTimes: 1);
}
}
}

0 comments on commit 6dd0b38

Please sign in to comment.