Skip to content

Commit 6b13dc9

Browse files
authored
Merge pull request #30 from linkdotnet/feature/giscus
Feature/giscus
2 parents b3f1fa0 + 02173a5 commit 6b13dc9

File tree

17 files changed

+210
-28
lines changed

17 files changed

+210
-28
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private void RegisterComponents(TestContextBase ctx, ILocalStorageService localS
8787
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
8888
ctx.Services.AddScoped(_ => new Mock<IHeadElementHelper>().Object);
8989
ctx.Services.AddScoped(_ => new Mock<IUserRecordService>().Object);
90+
ctx.Services.AddScoped(_ => new Mock<IGiscusService>().Object);
9091
}
9192
}
9293
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
<ItemGroup>
4848
<Folder Include="Infrastructure\Persistence" />
4949
</ItemGroup>
50+
51+
<ItemGroup>
52+
<Compile Remove="Web\Shared\Services\LocalStorageServiceTests.cs" />
53+
</ItemGroup>
5054
<PropertyGroup>
5155
<CodeAnalysisRuleSet>..\stylecop.analyzers.ruleset</CodeAnalysisRuleSet>
5256
</PropertyGroup>

LinkDotNet.Blog.UnitTests/Web/AppConfigurationFactoryTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public void ShouldMapFromAppConfiguration()
2525
{ "AboutMeProfileInformation:Name", "Steven" },
2626
{ "AboutMeProfileInformation:Heading", "Dev" },
2727
{ "AboutMeProfileInformation:ProfilePictureUrl", "Url" },
28+
{ "Giscus:Repository", "repo" },
29+
{ "Giscus:RepositoryId", "repoid" },
30+
{ "Giscus:Category", "general" },
31+
{ "Giscus:CategoryId", "generalid" },
2832
};
2933
var configuration = new ConfigurationBuilder()
3034
.AddInMemoryCollection(inMemorySettings)
@@ -47,6 +51,10 @@ public void ShouldMapFromAppConfiguration()
4751
appConfiguration.ProfileInformation.Name.Should().Be("Steven");
4852
appConfiguration.ProfileInformation.Heading.Should().Be("Dev");
4953
appConfiguration.ProfileInformation.ProfilePictureUrl.Should().Be("Url");
54+
appConfiguration.Giscus.Repository.Should().Be("repo");
55+
appConfiguration.Giscus.RepositoryId.Should().Be("repoid");
56+
appConfiguration.Giscus.Category.Should().Be("general");
57+
appConfiguration.Giscus.CategoryId.Should().Be("generalid");
5058
}
5159

5260
[Theory]
@@ -96,5 +104,24 @@ public void ShouldSetIsAboutMeEnabledToFalseWhenNoInformation()
96104

97105
appConfiguration.IsAboutMeEnabled.Should().BeFalse();
98106
}
107+
108+
[Fact]
109+
public void ShouldSetGiscusToFalseWhenNoInformation()
110+
{
111+
var inMemorySettings = new Dictionary<string, string>
112+
{
113+
{ "Introduction:BackgroundUrl", "someurl" },
114+
{ "Introduction:ProfilePictureUrl", "anotherurl" },
115+
{ "Introduction:Description", "desc" },
116+
{ "BlogPostsPerPage", "2" },
117+
};
118+
var configuration = new ConfigurationBuilder()
119+
.AddInMemoryCollection(inMemorySettings)
120+
.Build();
121+
122+
var appConfiguration = AppConfigurationFactory.Create(configuration);
123+
124+
appConfiguration.IsGiscusEnabled.Should().BeFalse();
125+
}
99126
}
100127
}

LinkDotNet.Blog.UnitTests/Web/Pages/AboutMeTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class AboutMeTests : TestContext
2626
public void ShouldPassIsAuthenticated()
2727
{
2828
this.AddTestAuthorization().SetAuthorized("test");
29-
var config = CreateAppConfiguration(true);
29+
var config = CreateAppConfiguration(new ProfileInformation { ProfilePictureUrl = string.Empty });
3030
SetupMocks(config);
3131

3232
var cut = RenderComponent<AboutMe>();
@@ -39,7 +39,7 @@ public void ShouldPassIsAuthenticated()
3939
public void ShouldNotShowWhenEnabledFalse()
4040
{
4141
this.AddTestAuthorization().SetNotAuthorized();
42-
var config = CreateAppConfiguration(false);
42+
var config = CreateAppConfiguration();
4343
SetupMocks(config);
4444

4545
var cut = RenderComponent<AboutMe>();
@@ -57,7 +57,7 @@ public void ShouldSetOgData()
5757
Name = "My Name",
5858
ProfilePictureUrl = "someurl",
5959
};
60-
var config = CreateAppConfiguration(false, profileInformation);
60+
var config = CreateAppConfiguration(profileInformation);
6161
SetupMocks(config);
6262

6363
var cut = RenderComponent<AboutMe>();
@@ -69,15 +69,11 @@ public void ShouldSetOgData()
6969
ogData.Description.Should().Contain("About Me,My Name");
7070
}
7171

72-
private static AppConfiguration CreateAppConfiguration(bool pageEnabled, ProfileInformation info = null)
72+
private static AppConfiguration CreateAppConfiguration(ProfileInformation info = null)
7373
{
7474
return new AppConfiguration
7575
{
76-
IsAboutMeEnabled = pageEnabled,
77-
ProfileInformation = info ?? new ProfileInformation
78-
{
79-
ProfilePictureUrl = "not null",
80-
},
76+
ProfileInformation = info,
8177
};
8278
}
8379

LinkDotNet.Blog.UnitTests/Web/Pages/Admin/DashboardTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static AppConfiguration CreateAppConfiguration(bool aboutMeEnabled)
3838
{
3939
return new AppConfiguration
4040
{
41-
IsAboutMeEnabled = aboutMeEnabled,
41+
ProfileInformation = aboutMeEnabled ? new ProfileInformation() : null,
4242
};
4343
}
4444
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Bunit;
4+
using FluentAssertions;
5+
using LinkDotNet.Blog.Web;
6+
using LinkDotNet.Blog.Web.Shared.Services;
7+
using Xunit;
8+
9+
namespace LinkDotNet.Blog.UnitTests.Web.Shared.Services
10+
{
11+
public class GiscusServiceTests : TestContext
12+
{
13+
[Fact]
14+
public async Task ShouldInitGiscus()
15+
{
16+
var giscus = new Giscus();
17+
JSInterop.Mode = JSRuntimeMode.Loose;
18+
var sut = new GiscusService(JSInterop.JSRuntime, new AppConfiguration { Giscus = giscus });
19+
20+
await sut.EnableCommentSection("div");
21+
22+
var init = JSInterop.Invocations.SingleOrDefault(i => i.Identifier == "initGiscus");
23+
init.Should().NotBeNull();
24+
init.Arguments.Should().Contain("giscus");
25+
init.Arguments.Should().Contain(giscus);
26+
}
27+
28+
[Fact]
29+
public async Task ShouldNotEnabledGiscussWhenNotSet()
30+
{
31+
JSInterop.Mode = JSRuntimeMode.Loose;
32+
var sut = new GiscusService(JSInterop.JSRuntime, new AppConfiguration { Giscus = null });
33+
34+
await sut.EnableCommentSection("div");
35+
36+
JSInterop.Invocations.Any(i => i.Identifier == "initGiscus").Should().BeFalse();
37+
}
38+
}
39+
}

LinkDotNet.Blog.Web/AppConfiguration.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LinkDotNet.Domain;
1+
using LinkDotNet.Blog.Web.Shared.Services;
2+
using LinkDotNet.Domain;
23

34
namespace LinkDotNet.Blog.Web
45
{
@@ -22,8 +23,12 @@ public record AppConfiguration
2223

2324
public int BlogPostsPerPage { get; init; }
2425

25-
public bool IsAboutMeEnabled { get; init; }
26+
public bool IsAboutMeEnabled => ProfileInformation != null;
2627

27-
public ProfileInformation ProfileInformation { get; set; }
28+
public ProfileInformation ProfileInformation { get; init; }
29+
30+
public Giscus Giscus { get; init; }
31+
32+
public bool IsGiscusEnabled => Giscus != null;
2833
}
2934
}

LinkDotNet.Blog.Web/AppConfigurationFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LinkDotNet.Domain;
1+
using LinkDotNet.Blog.Web.Shared.Services;
2+
using LinkDotNet.Domain;
23
using Microsoft.Extensions.Configuration;
34

45
namespace LinkDotNet.Blog.Web
@@ -8,6 +9,7 @@ public static class AppConfigurationFactory
89
public static AppConfiguration Create(IConfiguration config)
910
{
1011
var profileInformation = config.GetSection("AboutMeProfileInformation").Get<ProfileInformation>();
12+
var giscus = config.GetSection("Giscus").Get<Giscus>();
1113
var configuration = new AppConfiguration
1214
{
1315
BlogName = config["BlogName"],
@@ -18,7 +20,7 @@ public static AppConfiguration Create(IConfiguration config)
1820
DatabaseName = config["DatabaseName"],
1921
BlogPostsPerPage = int.Parse(config["BlogPostsPerPage"]),
2022
ProfileInformation = profileInformation,
21-
IsAboutMeEnabled = profileInformation != null,
23+
Giscus = giscus,
2224
};
2325

2426
return configuration;

LinkDotNet.Blog.Web/Pages/AboutMe.razor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
@inject AuthenticationStateProvider authenticationStateProvider
66
@inject IUserRecordService userRecordService
77
@inject NavigationManager navigationManager
8-
<OgData Title="@("About Me - " + appConfiguration.ProfileInformation.Name)"
9-
Description="@("About Me," + appConfiguration.ProfileInformation.Name)"
10-
Keywords="@appConfiguration.ProfileInformation.Name"
11-
AbsolutePreviewImageUrl="@ImageUrl"></OgData>
12-
138
@if (appConfiguration.IsAboutMeEnabled)
149
{
10+
<OgData Title="@("About Me - " + appConfiguration.ProfileInformation.Name)"
11+
Description="@("About Me," + appConfiguration.ProfileInformation.Name)"
12+
Keywords="@appConfiguration.ProfileInformation.Name"
13+
AbsolutePreviewImageUrl="@ImageUrl"></OgData>
14+
1515
<div class="page">
1616
<div class="container">
1717
<div class="row">

LinkDotNet.Blog.Web/Pages/BlogPostPage.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@inject IRepository<BlogPost> blogPostRepository
99
@inject IJSRuntime jsRuntime
1010
@inject IUserRecordService userRecordService
11+
@inject IGiscusService gisgusService
1112
@inherits MarkdownComponentBase
1213

1314
<div class="page">
@@ -40,6 +41,8 @@
4041
</div>
4142
</div>
4243
<Like BlogPost="@BlogPost" OnBlogPostLiked="@UpdateLikes"></Like>
44+
<div class="giscus">
45+
</div>
4346
</div>
4447
</div>
4548
}
@@ -66,6 +69,7 @@
6669
{
6770
await userRecordService.StoreUserRecordAsync();
6871
await jsRuntime.InvokeVoidAsync("hljs.highlightAll");
72+
await gisgusService.EnableCommentSection("giscus");
6973
StateHasChanged();
7074
}
7175
}

0 commit comments

Comments
 (0)