Skip to content

Commit 84b9818

Browse files
committed
Added tests for giscus
1 parent 59bd053 commit 84b9818

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

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
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

LinkDotNet.Blog.Web/Pages/BlogPostPage.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@inject IRepository<BlogPost> blogPostRepository
99
@inject IJSRuntime jsRuntime
1010
@inject IUserRecordService userRecordService
11-
@inject ICommentService commentService
11+
@inject IGiscusService gisgusService
1212
@inherits MarkdownComponentBase
1313

1414
<div class="page">
@@ -69,7 +69,7 @@
6969
{
7070
await userRecordService.StoreUserRecordAsync();
7171
await jsRuntime.InvokeVoidAsync("hljs.highlightAll");
72-
await commentService.EnableCommentSection("giscus");
72+
await gisgusService.EnableCommentSection("giscus");
7373
StateHasChanged();
7474
}
7575
}

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,20 @@
33

44
namespace LinkDotNet.Blog.Web.Shared.Services
55
{
6-
public class GiscusService : ICommentService
6+
public class GiscusService : IGiscusService
77
{
88
private readonly IJSRuntime jsRuntime;
9+
private readonly AppConfiguration appConfiguration;
910

10-
public GiscusService(IJSRuntime jsRuntime)
11+
public GiscusService(IJSRuntime jsRuntime, AppConfiguration appConfiguration)
1112
{
1213
this.jsRuntime = jsRuntime;
14+
this.appConfiguration = appConfiguration;
1315
}
1416

1517
public async Task EnableCommentSection(string className)
1618
{
17-
var giscus = new Giscus
18-
{
19-
Repository = "linkdotnet/Blog.Discussions",
20-
RepositoryId = "MDEwOlJlcG9zaXRvcnk0MDc1MzQ0OTA=",
21-
Category = "General",
22-
CategoryId = "DIC_kwDOGEp7ms4B_Fx_",
23-
};
24-
25-
await jsRuntime.InvokeVoidAsync("initGiscus", "giscus", giscus);
19+
await jsRuntime.InvokeVoidAsync("initGiscus", "giscus", appConfiguration.Giscus);
2620
}
2721
}
2822
}

LinkDotNet.Blog.Web/Shared/Services/ICommentService.cs renamed to LinkDotNet.Blog.Web/Shared/Services/IGiscusService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LinkDotNet.Blog.Web.Shared.Services
44
{
5-
public interface ICommentService
5+
public interface IGiscusService
66
{
77
Task EnableCommentSection(string className);
88
}

LinkDotNet.Blog.Web/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void ConfigureServices(IServiceCollection services)
5454
services.AddSingleton<ISortOrderCalculator, SortOrderCalculator>();
5555
services.AddScoped<IUserRecordService, UserRecordService>();
5656
services.AddScoped<IDashboardService, DashboardService>();
57-
services.AddScoped<ICommentService, GiscusService>();
57+
services.AddScoped<IGiscusService, GiscusService>();
5858
}
5959

6060
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

0 commit comments

Comments
 (0)