Skip to content

Commit 2f9baab

Browse files
authored
Merge pull request #27 from linkdotnet/feature/smaller-test
Smaller refactoring and added tests
2 parents 6db0353 + 6c189ac commit 2f9baab

File tree

5 files changed

+157
-19
lines changed

5 files changed

+157
-19
lines changed

LinkDotNet.Blog.IntegrationTests/Web/Pages/IndexTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ public async Task ShouldLoadTags()
132132
tags.Select(t => t.Attributes.Single(a => a.Name == "href").Value).Should().Contain("/searchByTag/Tag2");
133133
}
134134

135+
[Theory]
136+
[InlineData("relative/url", "http://localhost/relative/url")]
137+
[InlineData("http://localhost/relative/url", "http://localhost/relative/url")]
138+
public void ShouldSetAbsoluteUriForOgData(string givenUri, string expectedUri)
139+
{
140+
using var ctx = new TestContext();
141+
RegisterComponents(ctx);
142+
ctx.Services.GetService<AppConfiguration>()!.Introduction.BackgroundUrl = givenUri;
143+
144+
var cut = ctx.RenderComponent<Index>();
145+
146+
cut.WaitForState(() => cut.FindComponents<OgData>().Any());
147+
cut.FindComponent<OgData>().Instance.AbsolutePreviewImageUrl.Should().Be(expectedUri);
148+
}
149+
135150
private static AppConfiguration CreateSampleAppConfiguration()
136151
{
137152
return new()
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Linq;
3+
using System.Linq.Expressions;
4+
using Blazored.Toast.Services;
5+
using Bunit;
6+
using Bunit.TestDoubles;
7+
using FluentAssertions;
8+
using LinkDotNet.Blog.Web;
9+
using LinkDotNet.Blog.Web.Pages;
10+
using LinkDotNet.Blog.Web.Shared;
11+
using LinkDotNet.Blog.Web.Shared.Services;
12+
using LinkDotNet.Blog.Web.Shared.Skills;
13+
using LinkDotNet.Domain;
14+
using LinkDotNet.Infrastructure.Persistence;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Moq;
17+
using Toolbelt.Blazor.HeadElement;
18+
using X.PagedList;
19+
using Xunit;
20+
21+
namespace LinkDotNet.Blog.UnitTests.Web.Pages
22+
{
23+
public class AboutMeTests : TestContext
24+
{
25+
[Fact]
26+
public void ShouldPassIsAuthenticated()
27+
{
28+
this.AddTestAuthorization().SetAuthorized("test");
29+
var config = CreateAppConfiguration(true);
30+
SetupMocks(config);
31+
32+
var cut = RenderComponent<AboutMe>();
33+
34+
cut.FindComponent<Profile>().Instance.IsAuthenticated.Should().BeTrue();
35+
cut.FindComponent<SkillTable>().Instance.IsAuthenticated.Should().BeTrue();
36+
}
37+
38+
[Fact]
39+
public void ShouldNotShowWhenEnabledFalse()
40+
{
41+
this.AddTestAuthorization().SetNotAuthorized();
42+
var config = CreateAppConfiguration(false);
43+
SetupMocks(config);
44+
45+
var cut = RenderComponent<AboutMe>();
46+
47+
cut.FindComponents<Profile>().Any().Should().BeFalse();
48+
cut.FindComponents<SkillTable>().Any().Should().BeFalse();
49+
}
50+
51+
[Fact]
52+
public void ShouldSetOgData()
53+
{
54+
this.AddTestAuthorization().SetNotAuthorized();
55+
var profileInformation = new ProfileInformation
56+
{
57+
Name = "My Name",
58+
ProfilePictureUrl = "someurl",
59+
};
60+
var config = CreateAppConfiguration(false, profileInformation);
61+
SetupMocks(config);
62+
63+
var cut = RenderComponent<AboutMe>();
64+
65+
var ogData = cut.FindComponent<OgData>().Instance;
66+
ogData.AbsolutePreviewImageUrl.Should().Be("http://localhost/someurl");
67+
ogData.Keywords.Should().Contain("My Name");
68+
ogData.Title.Should().Contain("About Me - My Name");
69+
ogData.Description.Should().Contain("About Me,My Name");
70+
}
71+
72+
private static AppConfiguration CreateAppConfiguration(bool pageEnabled, ProfileInformation info = null)
73+
{
74+
return new AppConfiguration
75+
{
76+
IsAboutMeEnabled = pageEnabled,
77+
ProfileInformation = info ?? new ProfileInformation
78+
{
79+
ProfilePictureUrl = "not null",
80+
},
81+
};
82+
}
83+
84+
private void SetupMocks(AppConfiguration config)
85+
{
86+
var skillRepo = new Mock<IRepository<Skill>>();
87+
skillRepo.Setup(s => s.GetAllAsync(
88+
It.IsAny<Expression<Func<Skill, bool>>>(),
89+
It.IsAny<Expression<Func<Skill, object>>>(),
90+
It.IsAny<bool>(),
91+
It.IsAny<int>(),
92+
It.IsAny<int>()))
93+
.ReturnsAsync(new PagedList<Skill>(Array.Empty<Skill>(), 1, 1));
94+
var profileRepo = new Mock<IRepository<ProfileInformationEntry>>();
95+
profileRepo.Setup(s => s.GetAllAsync(
96+
It.IsAny<Expression<Func<ProfileInformationEntry, bool>>>(),
97+
It.IsAny<Expression<Func<ProfileInformationEntry, object>>>(),
98+
It.IsAny<bool>(),
99+
It.IsAny<int>(),
100+
It.IsAny<int>()))
101+
.ReturnsAsync(new PagedList<ProfileInformationEntry>(Array.Empty<ProfileInformationEntry>(), 1, 1));
102+
103+
Services.AddScoped(_ => config);
104+
Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
105+
Services.AddScoped(_ => new Mock<ISortOrderCalculator>().Object);
106+
Services.AddScoped(_ => skillRepo.Object);
107+
Services.AddScoped(_ => profileRepo.Object);
108+
Services.AddScoped(_ => new Mock<IHeadElementHelper>().Object);
109+
Services.AddScoped(_ => new Mock<IToastService>().Object);
110+
}
111+
}
112+
}

LinkDotNet.Blog.Web/Pages/AboutMe.razor

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
@inject AppConfiguration appConfiguration
55
@inject AuthenticationStateProvider authenticationStateProvider
66
@inject IUserRecordService userRecordService
7+
@inject NavigationManager navigationManager
78
<OgData Title="@("About Me - " + appConfiguration.ProfileInformation.Name)"
89
Description="@("About Me," + appConfiguration.ProfileInformation.Name)"
910
Keywords="@appConfiguration.ProfileInformation.Name"
10-
AbsolutePreviewImageUrl="@appConfiguration.ProfileInformation.ProfilePictureUrl"></OgData>
11+
AbsolutePreviewImageUrl="@ImageUrl"></OgData>
1112

1213
@if (appConfiguration.IsAboutMeEnabled)
1314
{
@@ -34,6 +35,8 @@
3435
@code {
3536
private bool isAuthenticated;
3637

38+
private string ImageUrl => appConfiguration.ProfileInformation.ProfilePictureUrl.ToAbsoluteUrl(navigationManager.BaseUri);
39+
3740
protected override async Task OnInitializedAsync()
3841
{
3942
var userIdentity = (await authenticationStateProvider.GetAuthenticationStateAsync()).User.Identity;

LinkDotNet.Blog.Web/Pages/Index.razor

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@inject IUserRecordService userRecordService
1111

1212
<OgData Title="@(Markdown.ToPlainText(appConfiguration.BlogName))"
13-
AbsolutePreviewImageUrl="@GetAbsolutePreviewImageUrl()"
13+
AbsolutePreviewImageUrl="@ImageUrl"
1414
Description="@(Markdown.ToPlainText(appConfiguration.Introduction.Description))"></OgData>
1515
<section class="introduction">
1616
<IntroductionCard Introduction="appConfiguration.Introduction"></IntroductionCard>
@@ -34,6 +34,8 @@
3434
@code {
3535
IPagedList<BlogPost> currentPage = new PagedList<BlogPost>(null, 1, 1);
3636

37+
private string ImageUrl => appConfiguration.Introduction.BackgroundUrl.ToAbsoluteUrl(navigationManager.BaseUri);
38+
3739
protected override async Task OnInitializedAsync()
3840
{
3941
currentPage = await blogPostRepository.GetAllAsync(
@@ -50,23 +52,6 @@
5052
}
5153
}
5254

53-
private string GetAbsolutePreviewImageUrl()
54-
{
55-
var backgroundUrl = appConfiguration.Introduction.BackgroundUrl;
56-
if (IsAbsoluteUrl(backgroundUrl))
57-
{
58-
return backgroundUrl;
59-
}
60-
61-
var successful = Uri.TryCreate(new Uri(navigationManager.BaseUri, UriKind.Absolute), new Uri(backgroundUrl, UriKind.RelativeOrAbsolute), out var uri);
62-
return successful ? uri.ToString() : backgroundUrl;
63-
}
64-
65-
private bool IsAbsoluteUrl(string url)
66-
{
67-
return Uri.TryCreate(url, UriKind.Absolute, out _);
68-
}
69-
7055
private async Task GetPage(int newPage)
7156
{
7257
currentPage = await blogPostRepository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: appConfiguration.BlogPostsPerPage, page:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace LinkDotNet.Blog.Web.Shared
4+
{
5+
public static class UrlExtensions
6+
{
7+
public static string ToAbsoluteUrl(this string url, string baseUrl)
8+
{
9+
if (IsAbsoluteUrl(url))
10+
{
11+
return url;
12+
}
13+
14+
var successful = Uri.TryCreate(new Uri(baseUrl, UriKind.Absolute), new Uri(url, UriKind.RelativeOrAbsolute), out var uri);
15+
return successful ? uri.ToString() : url;
16+
}
17+
18+
private static bool IsAbsoluteUrl(string url)
19+
{
20+
return Uri.TryCreate(url, UriKind.Absolute, out _);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)