Skip to content

Commit 3103eb1

Browse files
committed
Simplified navigation logic
1 parent faf2476 commit 3103eb1

File tree

4 files changed

+18
-78
lines changed

4 files changed

+18
-78
lines changed

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using LinkDotNet.Blog.Web;
99
using LinkDotNet.Blog.Web.Shared;
1010
using LinkDotNet.Blog.Web.Shared.Services;
11-
using Microsoft.AspNetCore.Components;
1211
using Microsoft.Extensions.DependencyInjection;
1312
using Moq;
1413
using Toolbelt.Blazor.HeadElement;
@@ -74,39 +73,6 @@ public async Task ShouldOnlyLoadTenEntities()
7473
blogPosts.Count.Should().Be(10);
7574
}
7675

77-
[Fact]
78-
public async Task ShouldGoToNextPageOnNextClick()
79-
{
80-
await CreatePublishedBlogPosts(11);
81-
using var ctx = new TestContext();
82-
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
83-
RegisterComponents(ctx);
84-
var cut = ctx.RenderComponent<Index>();
85-
86-
cut.FindComponent<BlogPostNavigation>().Find("li:last-child a").Click();
87-
88-
var navigationManager = ctx.Services.GetService<NavigationManager>();
89-
cut.WaitForState(() => navigationManager.Uri.Contains("/2"));
90-
navigationManager.Uri.Should().Contain("/2");
91-
}
92-
93-
[Fact]
94-
public async Task ShouldGoToPreviousPageOnPreviousClick()
95-
{
96-
await CreatePublishedBlogPosts(11);
97-
using var ctx = new TestContext();
98-
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
99-
RegisterComponents(ctx);
100-
var cut = ctx.RenderComponent<Index>(
101-
p => p.Add(s => s.Page, 2));
102-
103-
cut.FindComponent<BlogPostNavigation>().Find("li:first-child a").Click();
104-
105-
var navigationManager = ctx.Services.GetService<NavigationManager>();
106-
cut.WaitForState(() => navigationManager.Uri.Contains("/1"));
107-
navigationManager.Uri.Should().Contain("/1");
108-
}
109-
11076
[Fact]
11177
public async Task ShouldLoadOnlyItemsInPage()
11278
{

LinkDotNet.Blog.UnitTests/Web/Shared/BlogPostNavigationTests.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Bunit;
1+
using System.Linq;
2+
using AngleSharp.Html.Dom;
3+
using Bunit;
24
using FluentAssertions;
35
using LinkDotNet.Blog.Domain;
46
using LinkDotNet.Blog.Web.Shared;
@@ -13,35 +15,31 @@ public class BlogPostNavigationTests : TestContext
1315
[Fact]
1416
public void ShouldFireEventWhenGoingToNextPage()
1517
{
16-
var actualNewPage = 0;
1718
var page = CreatePagedList(2, 3);
18-
var cut = RenderComponent<BlogPostNavigation>(p => p.Add(param => param.CurrentPage, page.Object)
19-
.Add(param => param.OnPageChanged, newPage => actualNewPage = newPage));
2019

21-
cut.Find("li:last-child a").Click();
20+
var cut = RenderComponent<BlogPostNavigation>(p =>
21+
p.Add(param => param.PageList, page.Object));
2222

23-
actualNewPage.Should().Be(3);
23+
cut.FindAll("a").Cast<IHtmlAnchorElement>().Last().Href.Should().EndWith("/3");
2424
}
2525

2626
[Fact]
2727
public void ShouldFireEventWhenGoingToPreviousPage()
2828
{
29-
var actualNewPage = 0;
3029
var page = CreatePagedList(2, 3);
31-
var cut = RenderComponent<BlogPostNavigation>(p => p.Add(param => param.CurrentPage, page.Object)
32-
.Add(param => param.OnPageChanged, newPage => actualNewPage = newPage));
3330

34-
cut.Find("li:first-child a").Click();
31+
var cut = RenderComponent<BlogPostNavigation>(p =>
32+
p.Add(param => param.PageList, page.Object));
3533

36-
actualNewPage.Should().Be(1);
34+
cut.FindAll("a").Cast<IHtmlAnchorElement>().First().Href.Should().EndWith("/1");
3735
}
3836

3937
[Fact]
4038
public void ShouldNotFireNextWhenOnLastPage()
4139
{
4240
var page = CreatePagedList(2, 2);
4341
var cut = RenderComponent<BlogPostNavigation>(p =>
44-
p.Add(param => param.CurrentPage, page.Object));
42+
p.Add(param => param.PageList, page.Object));
4543

4644
cut.Find("li:last-child").ClassList.Should().Contain("disabled");
4745
}
@@ -51,7 +49,7 @@ public void ShouldNotFireNextWhenOnFirstPage()
5149
{
5250
var page = CreatePagedList(1, 2);
5351
var cut = RenderComponent<BlogPostNavigation>(p =>
54-
p.Add(param => param.CurrentPage, page.Object));
52+
p.Add(param => param.PageList, page.Object));
5553

5654
cut.Find("li:first-child").ClassList.Should().Contain("disabled");
5755
}
@@ -61,7 +59,7 @@ public void ShouldNotFireNextWhenNoPage()
6159
{
6260
var page = CreatePagedList(0, 0);
6361
var cut = RenderComponent<BlogPostNavigation>(p =>
64-
p.Add(param => param.CurrentPage, page.Object));
62+
p.Add(param => param.PageList, page.Object));
6563

6664
cut.Find("li:first-child").ClassList.Should().Contain("disabled");
6765
cut.Find("li:last-child").ClassList.Should().Contain("disabled");

LinkDotNet.Blog.Web/Pages/Index.razor

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<ShortBlogPost BlogPost="context" UseAlternativeStyle="@(currentLine++ % 2 != 0)"></ShortBlogPost>
2929
</Virtualize>
3030
</div>
31-
<BlogPostNavigation CurrentPage="@currentPage" OnPageChanged="@SetPageNumber"></BlogPostNavigation>
31+
<BlogPostNavigation PageList="@currentPage"></BlogPostNavigation>
3232
</section>
3333

3434
@code {
@@ -56,9 +56,4 @@
5656
await userRecordService.StoreUserRecordAsync();
5757
}
5858
}
59-
60-
private void SetPageNumber(int newPage)
61-
{
62-
navigationManager.NavigateTo($"/{newPage}");
63-
}
6459
}

LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,17 @@
22
@using LinkDotNet.Blog.Domain
33
<nav aria-label="Page navigation">
44
<ul class="pagination justify-content-center">
5-
<li class="page-item @(!CurrentPage.IsFirstPage && CurrentPage.Count > 0 ? string.Empty : "disabled")">
6-
<a class="page-link" href="" tabindex="-1" @onclick="PreviousPage">Previous</a>
5+
<li class="page-item @(!PageList.IsFirstPage && PageList.Count > 0 ? string.Empty : "disabled")">
6+
<a class="page-link" href="/@(PageList.PageNumber - 1)" tabindex="-1">Previous</a>
77
</li>
8-
<li class="page-item @(!CurrentPage.IsLastPage && CurrentPage.Count > 0 ? string.Empty : "disabled")">
8+
<li class="page-item @(!PageList.IsLastPage && PageList.Count > 0 ? string.Empty : "disabled")">
99
<a class="page-link"
10-
@onclick="NextPage"
11-
href="">Next</a>
10+
href="/@(PageList.PageNumber + 1)">Next</a>
1211
</li>
1312
</ul>
1413
</nav>
1514

1615
@code {
1716
[Parameter]
18-
public IPagedList<BlogPost> CurrentPage { get; set; }
19-
20-
[Parameter]
21-
public EventCallback<int> OnPageChanged { get; set; }
22-
23-
private async Task PageHasChanged(int newPage)
24-
{
25-
await OnPageChanged.InvokeAsync(newPage);
26-
}
27-
28-
private async Task PreviousPage()
29-
{
30-
await PageHasChanged(CurrentPage.PageNumber - 1);
31-
}
32-
33-
private async Task NextPage()
34-
{
35-
await PageHasChanged(CurrentPage.PageNumber + 1);
36-
}
17+
public IPagedList<BlogPost> PageList { get; set; }
3718
}

0 commit comments

Comments
 (0)