Skip to content

Commit

Permalink
Copying link will not result in total failure of app
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Dec 19, 2021
1 parent 04c620f commit a652195
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/LinkDotNet.Blog.Web/Shared/ShareBlogPost.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@

private async Task CopyToClipboard()
{
await jsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", navigationManager.Uri);
toastService.ShowSuccess("Copied link to clipboard");
try
{
await jsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", navigationManager.Uri);
toastService.ShowSuccess("Copied link to clipboard");
}
catch (Exception e)
{
toastService.ShowError("There was an error copying the link. Please copy the link from your address bar instead.");
Console.WriteLine(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit" Version="1.3.42" />
<PackageReference Include="bunit" Version="1.4.15" />
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit" Version="1.3.42" />
<PackageReference Include="bunit" Version="1.4.15" />
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0-preview-20211130-02" />
<PackageReference Include="Moq" Version="4.16.1" />
Expand Down
15 changes: 14 additions & 1 deletion tests/LinkDotNet.Blog.UnitTests/Web/Shared/ShareBlogPostTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using AngleSharp.Html.Dom;
using AngleSharpWrappers;
using Blazored.Toast.Services;
Expand Down Expand Up @@ -39,4 +40,16 @@ public void ShouldShareToLinkedIn()
var linkedInShare = (IHtmlAnchorElement)cut.Find("#share-linkedin").Unwrap();
linkedInShare.Href.Should().Be("https://www.linkedin.com/shareArticle?mini=true&url=http://localhost/blogPost/1");
}

[Fact]
public void ShouldNotCrashWhenCopyingLinkNotWorking()
{
Services.AddScoped(_ => new Mock<IToastService>().Object);
JSInterop.SetupVoid(s => s.InvocationMethodName == "navigator.clipboard.writeText").SetException(new Exception());
var cut = RenderComponent<ShareBlogPost>();

var act = () => cut.Find("#share-clipboard").Click();

act.Should().NotThrow<Exception>();
}
}

0 comments on commit a652195

Please sign in to comment.