Skip to content

Commit a3f4682

Browse files
committed
Smaller refactoring in VisitCountPerPage
1 parent cfa4490 commit a3f4682

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

LinkDotNet.Blog.IntegrationTests/Web/Pages/Admin/Dashboard/VisitCountPerPageTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public async Task ShouldShowCounts()
2222
await Repository.StoreAsync(blogPost);
2323
using var ctx = new TestContext();
2424
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
25-
var visits = new List<KeyValuePair<string, int>>();
26-
visits.Add(new KeyValuePair<string, int>($"blogPost/{blogPost.Id}", 5));
25+
var visits = new List<KeyValuePair<string, int>> { new($"blogPost/{blogPost.Id}", 5) };
2726
var pageVisitCounts = visits.OrderByDescending(s => s.Value);
2827

2928
var cut = ctx.RenderComponent<VisitCountPerPage>(p => p.Add(
@@ -39,5 +38,33 @@ public async Task ShouldShowCounts()
3938
elements[1].InnerHtml.Should().Be("5");
4039
elements[2].InnerHtml.Should().Be("2");
4140
}
41+
42+
[Fact]
43+
public void ShouldIgnoreNullForBlogPostVisits()
44+
{
45+
using var ctx = new TestContext();
46+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
47+
48+
var cut = ctx.RenderComponent<VisitCountPerPage>(p => p.Add(
49+
s => s.PageVisitCount, null));
50+
51+
var elements = cut.FindAll("td").ToList();
52+
elements.Should().BeEmpty();
53+
}
54+
55+
[Fact]
56+
public void ShouldIgnoreNotBlogPosts()
57+
{
58+
using var ctx = new TestContext();
59+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
60+
var visits = new List<KeyValuePair<string, int>> { new("notablogpost", 5) };
61+
var pageVisitCounts = visits.OrderByDescending(s => s.Value);
62+
63+
var cut = ctx.RenderComponent<VisitCountPerPage>(p => p.Add(
64+
s => s.PageVisitCount, pageVisitCounts));
65+
66+
var elements = cut.FindAll("td").ToList();
67+
elements.Should().BeEmpty();
68+
}
4269
}
4370
}

LinkDotNet.Blog.Web/Shared/Admin/Dashboard/VisitCountPerPage.razor

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@
4343

4444
foreach (var (blogPostUrl, clickCount) in PageVisitCount)
4545
{
46-
var blogPostId = blogPostUrl[(blogPostUrl.IndexOf('/') + 1)..];
46+
const string urlToBlogPost = "blogPost/";
47+
var possibleBlogPostId = blogPostUrl.IndexOf(urlToBlogPost, StringComparison.InvariantCultureIgnoreCase);
4748

48-
if (string.IsNullOrEmpty(blogPostId))
49+
if (possibleBlogPostId == -1)
4950
{
5051
continue;
5152
}
5253

53-
var blogPost = (await blogPostRepository.GetByIdAsync(blogPostId));
54+
var blogPostId = blogPostUrl[urlToBlogPost.Length..];
55+
var blogPost = await blogPostRepository.GetByIdAsync(blogPostId);
5456

5557
blogPostToCountList.Add(new KeyValuePair<BlogPost, int>(blogPost, clickCount));
5658
}

0 commit comments

Comments
 (0)