Skip to content

Commit a652195

Browse files
committed
Copying link will not result in total failure of app
1 parent 04c620f commit a652195

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/LinkDotNet.Blog.Web/Shared/ShareBlogPost.razor

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111

1212
private async Task CopyToClipboard()
1313
{
14-
await jsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", navigationManager.Uri);
15-
toastService.ShowSuccess("Copied link to clipboard");
14+
try
15+
{
16+
await jsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", navigationManager.Uri);
17+
toastService.ShowSuccess("Copied link to clipboard");
18+
}
19+
catch (Exception e)
20+
{
21+
toastService.ShowError("There was an error copying the link. Please copy the link from your address bar instead.");
22+
Console.WriteLine(e);
23+
}
1624
}
1725
}

tests/LinkDotNet.Blog.IntegrationTests/LinkDotNet.Blog.IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="bunit" Version="1.3.42" />
9+
<PackageReference Include="bunit" Version="1.4.15" />
1010
<PackageReference Include="FluentAssertions" Version="6.2.0" />
1111
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.1" />
1212
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />

tests/LinkDotNet.Blog.UnitTests/LinkDotNet.Blog.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="bunit" Version="1.3.42" />
10+
<PackageReference Include="bunit" Version="1.4.15" />
1111
<PackageReference Include="FluentAssertions" Version="6.2.0" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0-preview-20211130-02" />
1313
<PackageReference Include="Moq" Version="4.16.1" />

tests/LinkDotNet.Blog.UnitTests/Web/Shared/ShareBlogPostTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using AngleSharp.Html.Dom;
34
using AngleSharpWrappers;
45
using Blazored.Toast.Services;
@@ -39,4 +40,16 @@ public void ShouldShareToLinkedIn()
3940
var linkedInShare = (IHtmlAnchorElement)cut.Find("#share-linkedin").Unwrap();
4041
linkedInShare.Href.Should().Be("https://www.linkedin.com/shareArticle?mini=true&url=http://localhost/blogPost/1");
4142
}
43+
44+
[Fact]
45+
public void ShouldNotCrashWhenCopyingLinkNotWorking()
46+
{
47+
Services.AddScoped(_ => new Mock<IToastService>().Object);
48+
JSInterop.SetupVoid(s => s.InvocationMethodName == "navigator.clipboard.writeText").SetException(new Exception());
49+
var cut = RenderComponent<ShareBlogPost>();
50+
51+
var act = () => cut.Find("#share-clipboard").Click();
52+
53+
act.Should().NotThrow<Exception>();
54+
}
4255
}

0 commit comments

Comments
 (0)