Skip to content

Commit 3091072

Browse files
committed
Added tests for service
1 parent 3914395 commit 3091072

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Blazored.LocalStorage;
4+
using Bunit;
5+
using Bunit.TestDoubles;
6+
using FluentAssertions;
7+
using LinkDotNet.Blog.Web.Shared;
8+
using LinkDotNet.Domain;
9+
using LinkDotNet.Infrastructure.Persistence;
10+
using Moq;
11+
using Xunit;
12+
13+
namespace LinkDotNet.Blog.UnitTests.Web.Shared
14+
{
15+
public class UserRecordServiceTests : TestContext
16+
{
17+
private readonly Mock<IRepository<UserRecord>> repositoryMock;
18+
private readonly FakeNavigationManager fakeNavigationManager;
19+
private readonly FakeAuthenticationStateProvider fakeAuthenticationStateProvider;
20+
private readonly Mock<ILocalStorageService> localStorageService;
21+
private readonly UserRecordService sut;
22+
23+
public UserRecordServiceTests()
24+
{
25+
repositoryMock = new Mock<IRepository<UserRecord>>();
26+
fakeNavigationManager = new FakeNavigationManager(this.Renderer);
27+
fakeAuthenticationStateProvider = new FakeAuthenticationStateProvider();
28+
localStorageService = new Mock<ILocalStorageService>();
29+
sut = new UserRecordService(
30+
repositoryMock.Object,
31+
fakeNavigationManager,
32+
fakeAuthenticationStateProvider,
33+
localStorageService.Object);
34+
}
35+
36+
[Fact]
37+
public async Task ShouldStoreInformation()
38+
{
39+
const string Url = "http://localhost/subpart";
40+
fakeNavigationManager.NavigateTo(Url);
41+
UserRecord recordToDb = null;
42+
repositoryMock.Setup(r => r.StoreAsync(It.IsAny<UserRecord>()))
43+
.Callback<UserRecord>(u => recordToDb = u);
44+
45+
await sut.StoreUserRecordAsync();
46+
47+
recordToDb.Should().NotBeNull();
48+
recordToDb.UrlClicked.Should().Be("subpart");
49+
recordToDb.UserIdentifierHash.Should().NotBe(0);
50+
recordToDb.DateTimeUtcClicked.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
51+
}
52+
53+
[Fact]
54+
public async Task ShouldGetUserHashAgain()
55+
{
56+
UserRecord recordToDb = null;
57+
var guidForUser = Guid.NewGuid();
58+
localStorageService.Setup(l => l.ContainKeyAsync("user", default))
59+
.ReturnsAsync(true);
60+
localStorageService.Setup(l => l.GetItemAsync<Guid>("user", default))
61+
.ReturnsAsync(guidForUser);
62+
repositoryMock.Setup(r => r.StoreAsync(It.IsAny<UserRecord>()))
63+
.Callback<UserRecord>(u => recordToDb = u);
64+
65+
await sut.StoreUserRecordAsync();
66+
67+
recordToDb.Should().NotBeNull();
68+
var hashCode = guidForUser.GetHashCode();
69+
recordToDb.UserIdentifierHash.Should().Be(hashCode);
70+
}
71+
72+
[Fact]
73+
public async Task ShouldNotStoreForAdmin()
74+
{
75+
fakeAuthenticationStateProvider.TriggerAuthenticationStateChanged("Steven");
76+
77+
await sut.StoreUserRecordAsync();
78+
79+
repositoryMock.Verify(r => r.StoreAsync(It.IsAny<UserRecord>()), Times.Never);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)