Skip to content

Commit

Permalink
Merge pull request #453 from WiseTechGlobal/tolu/changediagramcanvas
Browse files Browse the repository at this point in the history
Change DiagramCanvas to await unsubscription to Resizes subscription
  • Loading branch information
zHaytam authored Aug 11, 2024
2 parents ac4eef3 + 6dd0b38 commit 62ca0e5
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 62ca0e5

Please sign in to comment.