Skip to content

Commit 6dedd14

Browse files
committed
Added missing files
1 parent 46385db commit 6dedd14

File tree

9 files changed

+197
-4
lines changed

9 files changed

+197
-4
lines changed

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/Disqus.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
if (firstRender && appConfiguration.IsDisqusEnabled)
1010
{
11-
await jsRuntime.InvokeAsync<IJSObjectReference>("import", "./Shared/Disqus.razor.js");
11+
await jsRuntime.InvokeAsync<IJSObjectReference>("import", "./Features/ShowBlogPost/Components/Disqus.razor.js");
1212
await jsRuntime.InvokeVoidAsync("initDisqus", appConfiguration.DisqusConfiguration);
1313
}
1414
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
window.initDisqus = (disqus) => {
2+
var d = document, s = d.createElement('script');
3+
4+
s.src = `https://${disqus.shortname}.disqus.com/embed.js`;
5+
6+
s.setAttribute('data-timestamp', +new Date());
7+
d.body.appendChild(s);
8+
}

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/Giscus.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
if (firstRender && appConfiguration.IsGiscusEnabled)
1010
{
11-
await jsRuntime.InvokeAsync<IJSObjectReference>("import", "./Shared/Giscus.razor.js");
11+
await jsRuntime.InvokeAsync<IJSObjectReference>("import", "./Features/ShowBlogPost/Components/Giscus.razor.js");
1212
await jsRuntime.InvokeVoidAsync("initGiscus", "giscus", appConfiguration.GiscusConfiguration);
1313
}
1414
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
window.initGiscus = (divClass, giscus) => {
2+
const script = document.createElement('script');
3+
script.src = 'https://giscus.app/client.js'
4+
script.setAttribute('data-repo', giscus.repository)
5+
script.setAttribute('data-repo-id', giscus.repositoryId)
6+
script.setAttribute('data-category', giscus.category)
7+
script.setAttribute('data-category-id', giscus.categoryId)
8+
script.setAttribute('data-mapping', 'title')
9+
script.setAttribute('data-reactions-enabled', '0')
10+
script.setAttribute('data-emit-metadata', '0')
11+
script.setAttribute('data-theme', 'light')
12+
script.crossOrigin = 'anonymous'
13+
14+
const elementToAppend = document.getElementsByClassName(divClass)[0]
15+
if (elementToAppend) {
16+
elementToAppend.appendChild(script)
17+
}
18+
}

src/LinkDotNet.Blog.Web/LinkDotNet.Blog.Web.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@
2828
<InternalsVisibleTo Include="LinkDotNet.Blog.IntegrationTests" />
2929
</ItemGroup>
3030

31+
<ItemGroup>
32+
<None Update="Features\ShowBlogPost\Components\Giscus.razor.js">
33+
<DependentUpon>Giscus.razor</DependentUpon>
34+
</None>
35+
<None Update="Features\ShowBlogPost\Components\Disqus.razor.js">
36+
<DependentUpon>Disqus.razor</DependentUpon>
37+
</None>
38+
</ItemGroup>
39+
3140
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Blazored.Toast.Services;
4+
using Bunit;
5+
using Bunit.TestDoubles;
6+
using LinkDotNet.Blog.Domain;
7+
using LinkDotNet.Blog.Infrastructure.Persistence;
8+
using LinkDotNet.Blog.TestUtilities;
9+
using LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor;
10+
using LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components;
11+
using Microsoft.EntityFrameworkCore;
12+
using Microsoft.Extensions.DependencyInjection;
13+
14+
namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Admin.BlogPostEditor;
15+
16+
public class UpdateBlogPostPageTests : SqlDatabaseTestBase<BlogPost>
17+
{
18+
[Fact]
19+
public async Task ShouldSaveBlogPostOnSave()
20+
{
21+
using var ctx = new TestContext();
22+
var toastService = new Mock<IToastService>();
23+
var blogPost = new BlogPostBuilder().WithTitle("Title").WithShortDescription("Sub").Build();
24+
await Repository.StoreAsync(blogPost);
25+
ctx.AddTestAuthorization().SetAuthorized("some username");
26+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
27+
ctx.Services.AddScoped(_ => toastService.Object);
28+
ctx.ComponentFactories.AddStub<UploadFile>();
29+
using var cut = ctx.RenderComponent<UpdateBlogPostPage>(
30+
p => p.Add(s => s.BlogPostId, blogPost.Id));
31+
var newBlogPost = cut.FindComponent<CreateNewBlogPost>();
32+
33+
TriggerUpdate(newBlogPost);
34+
35+
var blogPostFromDb = await DbContext.BlogPosts.SingleOrDefaultAsync(t => t.Id == blogPost.Id);
36+
blogPostFromDb.Should().NotBeNull();
37+
blogPostFromDb.ShortDescription.Should().Be("My new Description");
38+
toastService.Verify(t => t.ShowInfo("Updated BlogPost Title", string.Empty, null), Times.Once);
39+
}
40+
41+
[Fact]
42+
public void ShouldThrowWhenNoIdProvided()
43+
{
44+
using var ctx = new TestContext();
45+
ctx.AddTestAuthorization().SetAuthorized("some username");
46+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
47+
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
48+
49+
Action act = () => ctx.RenderComponent<UpdateBlogPostPage>(
50+
p => p.Add(s => s.BlogPostId, null));
51+
52+
act.Should().ThrowExactly<ArgumentNullException>();
53+
}
54+
55+
private static void TriggerUpdate(IRenderedFragment cut)
56+
{
57+
cut.Find("#short").Input("My new Description");
58+
59+
cut.Find("form").Submit();
60+
}
61+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using LinkDotNet.Blog.Domain;
4+
using LinkDotNet.Blog.Web.Features.Admin.Dashboard.Services;
5+
6+
namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Admin.Dashboard.Service;
7+
8+
public class DashboardServiceTests : SqlDatabaseTestBase<UserRecord>
9+
{
10+
private readonly DashboardService sut;
11+
12+
public DashboardServiceTests()
13+
{
14+
sut = new DashboardService(Repository);
15+
}
16+
17+
[Fact]
18+
public async Task ShouldGetTotalUsers()
19+
{
20+
var record1 = new UserRecord
21+
{
22+
UserIdentifierHash = 2,
23+
DateTimeUtcClicked = DateTime.UtcNow,
24+
UrlClicked = string.Empty,
25+
};
26+
var record2 = new UserRecord
27+
{
28+
UserIdentifierHash = 1,
29+
UrlClicked = string.Empty,
30+
};
31+
var record3 = new UserRecord
32+
{
33+
UserIdentifierHash = 2,
34+
UrlClicked = string.Empty,
35+
};
36+
await Repository.StoreAsync(record1);
37+
await Repository.StoreAsync(record2);
38+
await Repository.StoreAsync(record3);
39+
40+
var data = await sut.GetDashboardDataAsync();
41+
42+
data.TotalAmountOfUsers.Should().Be(2);
43+
data.AmountOfUsersLast30Days.Should().Be(1);
44+
}
45+
46+
[Fact]
47+
public async Task ShouldGetTotalClicks()
48+
{
49+
var record1 = new UserRecord
50+
{
51+
DateTimeUtcClicked = DateTime.UtcNow,
52+
UrlClicked = "index",
53+
};
54+
var record2 = new UserRecord
55+
{
56+
UrlClicked = string.Empty,
57+
};
58+
var record3 = new UserRecord
59+
{
60+
UrlClicked = string.Empty,
61+
};
62+
await Repository.StoreAsync(record1);
63+
await Repository.StoreAsync(record2);
64+
await Repository.StoreAsync(record3);
65+
66+
var data = await sut.GetDashboardDataAsync();
67+
68+
data.TotalPageClicks.Should().Be(3);
69+
data.PageClicksLast30Days.Should().Be(1);
70+
}
71+
72+
[Fact]
73+
public async Task ShouldGetAboutMeClicks()
74+
{
75+
var record1 = new UserRecord
76+
{
77+
DateTimeUtcClicked = DateTime.UtcNow,
78+
UrlClicked = "AboutMe",
79+
};
80+
var record2 = new UserRecord
81+
{
82+
UrlClicked = string.Empty,
83+
};
84+
var record3 = new UserRecord
85+
{
86+
UrlClicked = "AboutMe",
87+
};
88+
await Repository.StoreAsync(record1);
89+
await Repository.StoreAsync(record2);
90+
await Repository.StoreAsync(record3);
91+
92+
var data = await sut.GetDashboardDataAsync();
93+
94+
data.TotalAboutMeClicks.Should().Be(2);
95+
data.AboutMeClicksLast30Days.Should().Be(1);
96+
}
97+
}

tests/LinkDotNet.Blog.UnitTests/Web/Features/ShowBlogPost/Components/DisqusTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void ShouldLoadJavascript()
1616
Shortname = "blog",
1717
};
1818
Services.AddScoped(_ => new AppConfiguration { DisqusConfiguration = disqusData });
19-
JSInterop.SetupModule("./Shared/Disqus.razor.js");
19+
JSInterop.SetupModule("./Features/ShowBlogPost/Components/Disqus.razor.js");
2020
JSInterop.Mode = JSRuntimeMode.Loose;
2121

2222
RenderComponent<Disqus>();

tests/LinkDotNet.Blog.UnitTests/Web/Features/ShowBlogPost/Components/GiscusTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ShouldLoadJavascript()
1919
CategoryId = "GeneralId",
2020
};
2121
Services.AddScoped(_ => new AppConfiguration { GiscusConfiguration = giscusData });
22-
JSInterop.SetupModule("./Shared/Giscus.razor.js");
22+
JSInterop.SetupModule("./Features/ShowBlogPost/Components/Giscus.razor.js");
2323
JSInterop.Mode = JSRuntimeMode.Loose;
2424

2525
RenderComponent<Giscus>();

0 commit comments

Comments
 (0)