Skip to content

Commit 67b577a

Browse files
committed
Fixed possible migration issues from Blazored.LocalStorage to MS LocalStorage
1 parent 5b323cb commit 67b577a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

LinkDotNet.Blog.UnitTests/Web/Shared/Services/UserRecordServiceTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,24 @@ public async Task ShouldNotStoreForAdmin()
8181
[Fact]
8282
public async Task ShouldNotThrowExceptionToOutsideWorld()
8383
{
84-
localStorageService.Setup(l => l.ContainKeyAsync("user")).Throws<Exception>();
84+
localStorageService.Setup(l => l.SetItemAsync("user", "some value")).Throws<Exception>();
8585

8686
Func<Task> act = () => sut.StoreUserRecordAsync();
8787

8888
await act.Should().NotThrowAsync<Exception>();
8989
}
9090

91+
[Fact]
92+
public async Task ShouldReturnFalseWhenContainKeyOnExceptionAndCreateNewOne()
93+
{
94+
localStorageService.Setup(l => l.ContainKeyAsync("user")).Throws<Exception>();
95+
96+
await sut.StoreUserRecordAsync();
97+
98+
repositoryMock.Verify(l => l.StoreAsync(It.IsAny<UserRecord>()), Times.Once);
99+
localStorageService.Verify(l => l.SetItemAsync("user", It.IsAny<Guid>()), Times.Once);
100+
}
101+
91102
[InlineData("http://localhost/blogPost/12?q=3", "blogPost/12")]
92103
[InlineData("http://localhost/?q=3", "")]
93104
[InlineData("", "")]

LinkDotNet.Blog.Web/Shared/Services/UserRecordService.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Threading.Tasks;
43
using LinkDotNet.Blog.Domain;
54
using LinkDotNet.Blog.Infrastructure.Persistence;
@@ -35,7 +34,7 @@ public async Task StoreUserRecordAsync()
3534
}
3635
catch (Exception e)
3736
{
38-
Trace.Write($"Exception: {e}");
37+
Console.Write($"Exception: {e}");
3938
}
4039
}
4140

@@ -63,7 +62,7 @@ private async Task GetAndStoreUserRecordAsync()
6362

6463
private async Task<int> GetIdentifierHashAsync()
6564
{
66-
var hasKey = await localStorageService.ContainKeyAsync("user");
65+
var hasKey = await TryGetKey();
6766
if (hasKey)
6867
{
6968
var key = await localStorageService.GetItemAsync<Guid>("user");
@@ -75,6 +74,20 @@ private async Task<int> GetIdentifierHashAsync()
7574
return id.GetHashCode();
7675
}
7776

77+
private async Task<bool> TryGetKey()
78+
{
79+
try
80+
{
81+
var hasKey = await localStorageService.ContainKeyAsync("user");
82+
return hasKey;
83+
}
84+
catch (Exception e)
85+
{
86+
Console.WriteLine($"Couldn't obtain key: \"user\": {e}");
87+
return false;
88+
}
89+
}
90+
7891
private string GetClickedUrl()
7992
{
8093
var basePath = navigationManager.ToBaseRelativePath(navigationManager.Uri);

0 commit comments

Comments
 (0)