Skip to content

Commit 223645f

Browse files
committed
Added Tests for Pagination / Index
1 parent e0444ce commit 223645f

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

LinkDotNet.Blog.IntegrationTests/SqlDatabaseTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ protected SqlDatabaseTestBase()
1919
BlogPostRepository = new BlogPostRepository(new BlogPostContext(options));
2020
}
2121

22-
protected BlogPostRepository BlogPostRepository { get; private set; }
22+
protected BlogPostRepository BlogPostRepository { get; }
2323

24-
protected BlogPostContext DbContext { get; private set; }
24+
protected BlogPostContext DbContext { get; }
2525

2626
public Task InitializeAsync()
2727
{

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,54 @@ public async Task ShouldOnlyShowPublishedPosts()
5353
blogPosts[0].Find(".description h1").InnerHtml.Should().Be("Published");
5454
}
5555

56+
[Fact]
57+
public async Task ShouldOnlyLoadTenEntities()
58+
{
59+
await CreatePublishedBlogPosts(11);
60+
using var ctx = new TestContext();
61+
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
62+
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
63+
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
64+
var cut = ctx.RenderComponent<Index>();
65+
66+
var blogPosts = cut.FindComponents<ShortBlogPost>();
67+
68+
blogPosts.Count.Should().Be(10);
69+
}
70+
71+
[Fact]
72+
public async Task ShouldLoadNextBatchOnClick()
73+
{
74+
await CreatePublishedBlogPosts(11);
75+
using var ctx = new TestContext();
76+
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
77+
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
78+
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
79+
var cut = ctx.RenderComponent<Index>();
80+
81+
cut.FindComponent<BlogPostNavigation>().Find("li:last-child a").Click();
82+
83+
var blogPosts = cut.FindComponents<ShortBlogPost>();
84+
blogPosts.Count.Should().Be(1);
85+
}
86+
87+
[Fact]
88+
public async Task ShouldLoadPreviousBatchOnClick()
89+
{
90+
await CreatePublishedBlogPosts(11);
91+
using var ctx = new TestContext();
92+
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
93+
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
94+
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
95+
var cut = ctx.RenderComponent<Index>();
96+
cut.FindComponent<BlogPostNavigation>().Find("li:last-child a").Click();
97+
98+
cut.FindComponent<BlogPostNavigation>().Find("li:first-child a").Click();
99+
100+
var blogPosts = cut.FindComponents<ShortBlogPost>();
101+
blogPosts.Count.Should().Be(1);
102+
}
103+
56104
private static AppConfiguration CreateSampleAppConfiguration()
57105
{
58106
return new()
@@ -66,5 +114,14 @@ private static AppConfiguration CreateSampleAppConfiguration()
66114
},
67115
};
68116
}
117+
118+
private async Task CreatePublishedBlogPosts(int amount)
119+
{
120+
for (var i = 0; i < amount; i++)
121+
{
122+
var blogPost = new BlogPostBuilder().IsPublished().Build();
123+
await BlogPostRepository.StoreAsync(blogPost);
124+
}
125+
}
69126
}
70127
}

LinkDotNet.Blog.Web/Pages/Index.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
protected override async Task OnInitializedAsync()
2828
{
29-
_currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 3);
29+
_currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 10);
3030
}
3131

3232
private string GetAbsolutePreviewImageUrl()
@@ -48,7 +48,7 @@
4848

4949
private async Task GetPage(int newPage)
5050
{
51-
_currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 3, page:
51+
_currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 10, page:
5252
newPage);
5353
}
5454
}

LinkDotNet.Blog.Web/Pages/SearchByTag.razor

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
protected override async Task OnInitializedAsync()
1919
{
2020
Tag = Uri.UnescapeDataString(Tag);
21-
_blogPosts = (await _repository.GetAllAsync(b => b.Tags.Any(t => t.Content == Tag), b => b.UpdatedDate)).ToList();
21+
_blogPosts = (await _repository.GetAllAsync(
22+
b => b.Tags.Any(t => t.Content == Tag),
23+
b => b.UpdatedDate))
24+
.ToList();
2225
}
2326
}

0 commit comments

Comments
 (0)