Skip to content

Commit 0cb627b

Browse files
committed
Don't show unpublished in archive (Fixes #56)
1 parent 9b5552c commit 0cb627b

File tree

13 files changed

+119
-101
lines changed

13 files changed

+119
-101
lines changed

src/LinkDotNet.Blog.Web/Features/Archive/ArchivePage.razor

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929

3030
protected override async Task OnInitializedAsync()
3131
{
32-
blogPostsPerYear = (await repository.GetAllAsync()).GroupBy(r => r.UpdatedDate.Year).ToList();
32+
blogPostsPerYear = (await repository.GetAllAsync(p => p.IsPublished))
33+
.GroupBy(r => r.UpdatedDate.Year)
34+
.ToList();
3335
}
3436

3537
}

tests/LinkDotNet.Blog.IntegrationTests/SqlDatabaseTestBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Data.Common;
33
using System.Threading.Tasks;
44
using LinkDotNet.Blog.Domain;
5+
using LinkDotNet.Blog.Infrastructure.Persistence;
56
using LinkDotNet.Blog.Infrastructure.Persistence.Sql;
67
using Microsoft.Data.Sqlite;
78
using Microsoft.EntityFrameworkCore;
@@ -20,7 +21,7 @@ protected SqlDatabaseTestBase()
2021
Repository = new Repository<TEntity>(new BlogDbContext(options));
2122
}
2223

23-
protected Repository<TEntity> Repository { get; }
24+
protected IRepository<TEntity> Repository { get; }
2425

2526
protected BlogDbContext DbContext { get; }
2627

@@ -48,4 +49,4 @@ private static DbConnection CreateInMemoryConnection()
4849

4950
return connection;
5051
}
51-
}
52+
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/Admin/BlogPostEditor/CreateNewBlogPostPageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task ShouldSaveBlogPostOnSave()
2121
using var ctx = new TestContext();
2222
var toastService = new Mock<IToastService>();
2323
ctx.AddTestAuthorization().SetAuthorized("some username");
24-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
24+
ctx.Services.AddScoped(_ => Repository);
2525
ctx.Services.AddScoped(_ => toastService.Object);
2626
ctx.ComponentFactories.AddStub<UploadFile>();
2727
ctx.Services.AddScoped(_ => Mock.Of<IFileProcessor>());
@@ -42,7 +42,7 @@ public async Task ShouldSetContentFromFile()
4242
using var ctx = new TestContext();
4343
const string contentFromFile = "content";
4444
ctx.AddTestAuthorization().SetAuthorized("some username");
45-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
45+
ctx.Services.AddScoped(_ => Repository);
4646
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
4747
var args = SetupUploadFile(contentFromFile, ctx);
4848
var cut = ctx.RenderComponent<CreateNewBlogPost>();

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/Admin/BlogPostEditor/UpdateBlogPostPageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task ShouldSaveBlogPostOnSave()
2323
var blogPost = new BlogPostBuilder().WithTitle("Title").WithShortDescription("Sub").Build();
2424
await Repository.StoreAsync(blogPost);
2525
ctx.AddTestAuthorization().SetAuthorized("some username");
26-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
26+
ctx.Services.AddScoped(_ => Repository);
2727
ctx.Services.AddScoped(_ => toastService.Object);
2828
ctx.ComponentFactories.AddStub<UploadFile>();
2929
using var cut = ctx.RenderComponent<UpdateBlogPostPage>(
@@ -43,7 +43,7 @@ public void ShouldThrowWhenNoIdProvided()
4343
{
4444
using var ctx = new TestContext();
4545
ctx.AddTestAuthorization().SetAuthorized("some username");
46-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
46+
ctx.Services.AddScoped(_ => Repository);
4747
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
4848

4949
Action act = () => ctx.RenderComponent<UpdateBlogPostPage>(

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/Admin/DraftBlogPost/DraftBlogPostPageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task ShouldOnlyShowPublishedPosts()
2121
await Repository.StoreAsync(unpublishedPost);
2222
using var ctx = new TestContext();
2323
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
24-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
24+
ctx.Services.AddScoped(_ => Repository);
2525
var cut = ctx.RenderComponent<DraftBlogPostPage>();
2626
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
2727

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Bunit;
4+
using LinkDotNet.Blog.Domain;
5+
using LinkDotNet.Blog.TestUtilities;
6+
using LinkDotNet.Blog.Web.Features.Archive;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Archive;
10+
11+
public class ArchivePageTests : SqlDatabaseTestBase<BlogPost>
12+
{
13+
[Fact]
14+
public async Task ShouldDisplayAllBlogPosts()
15+
{
16+
using var ctx = new TestContext();
17+
ctx.Services.AddScoped(_ => Repository);
18+
await Repository.StoreAsync(CreateBlogPost(new DateTime(2021, 1, 1), "Blog Post 1"));
19+
await Repository.StoreAsync(CreateBlogPost(new DateTime(2021, 2, 1), "Blog Post 2"));
20+
await Repository.StoreAsync(CreateBlogPost(new DateTime(2022, 1, 1), "Blog Post 3"));
21+
22+
var cut = ctx.RenderComponent<ArchivePage>();
23+
24+
cut.WaitForElements("h2");
25+
var yearHeader = cut.FindAll("h2");
26+
yearHeader.Should().HaveCount(2);
27+
yearHeader[0].TextContent.Should().Be("2022");
28+
yearHeader[1].TextContent.Should().Be("2021");
29+
var entries = cut.FindAll("li");
30+
entries.Should().HaveCount(3);
31+
entries[0].TextContent.Should().Be("Blog Post 3");
32+
entries[1].TextContent.Should().Be("Blog Post 2");
33+
entries[2].TextContent.Should().Be("Blog Post 1");
34+
}
35+
36+
[Fact]
37+
public async Task ShouldOnlyShowPublishedBlogPosts()
38+
{
39+
using var ctx = new TestContext();
40+
ctx.Services.AddScoped(_ => Repository);
41+
var publishedBlogPost = new BlogPostBuilder().WithUpdatedDate(new DateTime(2022, 1, 1)).IsPublished().Build();
42+
var unPublishedBlogPost = new BlogPostBuilder().WithUpdatedDate(new DateTime(2022, 1, 1)).IsPublished(false).Build();
43+
await Repository.StoreAsync(publishedBlogPost);
44+
await Repository.StoreAsync(unPublishedBlogPost);
45+
46+
var cut = ctx.RenderComponent<ArchivePage>();
47+
48+
cut.FindAll("h2").Should().HaveCount(1);
49+
}
50+
51+
private static BlogPost CreateBlogPost(DateTime date, string title)
52+
{
53+
return new BlogPostBuilder()
54+
.WithTitle(title)
55+
.WithUpdatedDate(date)
56+
.Build();
57+
}
58+
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/Home/IndexTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private async Task CreatePublishedBlogPosts(int amount)
167167

168168
private void RegisterComponents(TestContextBase ctx)
169169
{
170-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
170+
ctx.Services.AddScoped(_ => Repository);
171171
ctx.Services.AddScoped(_ => CreateSampleAppConfiguration());
172172
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
173173
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/Search/SearchPageTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task ShouldFindBlogPostWhenTitleMatches()
2121
await Repository.StoreAsync(blogPost1);
2222
await Repository.StoreAsync(blogPost2);
2323
using var ctx = new TestContext();
24-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
24+
ctx.Services.AddScoped(_ => Repository);
2525
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
2626

2727
var cut = ctx.RenderComponent<SearchPage>(p => p.Add(s => s.SearchTerm, "Title 1"));
@@ -40,7 +40,7 @@ public async Task ShouldFindBlogPostWhenTagMatches()
4040
await Repository.StoreAsync(blogPost1);
4141
await Repository.StoreAsync(blogPost2);
4242
using var ctx = new TestContext();
43-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
43+
ctx.Services.AddScoped(_ => Repository);
4444
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
4545

4646
var cut = ctx.RenderComponent<SearchPage>(p => p.Add(s => s.SearchTerm, "Cat"));
@@ -57,7 +57,7 @@ public async Task ShouldUnescapeQuery()
5757
var blogPost1 = new BlogPostBuilder().WithTitle("Title 1").Build();
5858
await Repository.StoreAsync(blogPost1);
5959
using var ctx = new TestContext();
60-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
60+
ctx.Services.AddScoped(_ => Repository);
6161
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
6262

6363
var cut = ctx.RenderComponent<SearchPage>(p => p.Add(s => s.SearchTerm, "Title%201"));

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/SearchByTag/SearchByTagPageTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public async Task ShouldOnlyDisplayTagsGivenByParameter()
2222
await AddBlogPostWithTagAsync("Tag 1");
2323
await AddBlogPostWithTagAsync("Tag 1");
2424
await AddBlogPostWithTagAsync("Tag 2");
25-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
25+
ctx.Services.AddScoped(_ => Repository);
2626
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
2727
var cut = ctx.RenderComponent<SearchByTagPage>(p => p.Add(s => s.Tag, "Tag 1"));
2828
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
@@ -37,7 +37,7 @@ public async Task ShouldHandleSpecialCharacters()
3737
{
3838
using var ctx = new TestContext();
3939
await AddBlogPostWithTagAsync("C#");
40-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
40+
ctx.Services.AddScoped(_ => Repository);
4141
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
4242
var cut = ctx.RenderComponent<SearchByTagPage>(p => p.Add(s => s.Tag, Uri.EscapeDataString("C#")));
4343
cut.WaitForState(() => cut.FindAll(".blog-card").Any());
@@ -51,7 +51,7 @@ public async Task ShouldHandleSpecialCharacters()
5151
public void ShouldSetTitleToTag()
5252
{
5353
using var ctx = new TestContext();
54-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
54+
ctx.Services.AddScoped(_ => Repository);
5555
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());
5656
ctx.ComponentFactories.AddStub<PageTitle>();
5757

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/ShowBlogPost/ShowBlogPostPageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public async Task ShouldSetTagsWhenAvailable()
8080

8181
private void RegisterComponents(TestContextBase ctx, ILocalStorageService localStorageService = null)
8282
{
83-
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
83+
ctx.Services.AddScoped(_ => Repository);
8484
ctx.Services.AddScoped(_ => localStorageService ?? Mock.Of<ILocalStorageService>());
8585
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
8686
ctx.Services.AddScoped(_ => Mock.Of<IUserRecordService>());

0 commit comments

Comments
 (0)