Skip to content

Commit 103a2d8

Browse files
committed
Added more tests
1 parent 2f9baab commit 103a2d8

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Blazored.Toast.Services;
4+
using Bunit;
5+
using Bunit.TestDoubles;
6+
using FluentAssertions;
7+
using LinkDotNet.Blog.TestUtilities;
8+
using LinkDotNet.Blog.Web.Pages.Admin;
9+
using LinkDotNet.Blog.Web.Shared.Admin;
10+
using LinkDotNet.Domain;
11+
using LinkDotNet.Infrastructure.Persistence;
12+
using Microsoft.EntityFrameworkCore;
13+
using Microsoft.Extensions.DependencyInjection;
14+
using Moq;
15+
using Xunit;
16+
17+
namespace LinkDotNet.Blog.IntegrationTests.Web.Pages.Admin
18+
{
19+
public class UpdateBlogPostPageTests : SqlDatabaseTestBase<BlogPost>
20+
{
21+
[Fact]
22+
public async Task ShouldSaveBlogPostOnSave()
23+
{
24+
using var ctx = new TestContext();
25+
var toastService = new Mock<IToastService>();
26+
var blogPost = new BlogPostBuilder().WithTitle("Title").WithShortDescription("Sub").Build();
27+
await Repository.StoreAsync(blogPost);
28+
ctx.AddTestAuthorization().SetAuthorized("some username");
29+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
30+
ctx.Services.AddScoped(_ => toastService.Object);
31+
using var cut = ctx.RenderComponent<UpdateBlogPostPage>(
32+
p => p.Add(s => s.BlogPostId, blogPost.Id));
33+
var newBlogPost = cut.FindComponent<CreateNewBlogPost>();
34+
35+
TriggerUpdate(newBlogPost);
36+
37+
var blogPostFromDb = await DbContext.BlogPosts.SingleOrDefaultAsync(t => t.Id == blogPost.Id);
38+
blogPostFromDb.Should().NotBeNull();
39+
blogPostFromDb.ShortDescription.Should().Be("My new Description");
40+
toastService.Verify(t => t.ShowInfo("Updated BlogPost Title", string.Empty), Times.Once);
41+
}
42+
43+
[Fact]
44+
public void ShouldThrowWhenNoIdProvided()
45+
{
46+
using var ctx = new TestContext();
47+
ctx.AddTestAuthorization().SetAuthorized("some username");
48+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
49+
ctx.Services.AddScoped(_ => new Mock<IToastService>().Object);
50+
51+
Action act = () => ctx.RenderComponent<UpdateBlogPostPage>(
52+
p => p.Add(s => s.BlogPostId, null));
53+
54+
act.Should().ThrowExactly<ArgumentNullException>();
55+
}
56+
57+
private static void TriggerUpdate(IRenderedFragment cut)
58+
{
59+
cut.Find("#short").Change("My new Description");
60+
61+
cut.Find("form").Submit();
62+
}
63+
}
64+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Linq;
2+
using Bunit;
3+
using Bunit.TestDoubles;
4+
using FluentAssertions;
5+
using LinkDotNet.Blog.Web;
6+
using LinkDotNet.Blog.Web.Pages.Admin;
7+
using LinkDotNet.Blog.Web.Shared.Admin.Dashboard;
8+
using LinkDotNet.Domain;
9+
using LinkDotNet.Infrastructure.Persistence;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Moq;
12+
using Xunit;
13+
14+
namespace LinkDotNet.Blog.UnitTests.Web.Pages.Admin
15+
{
16+
public class DashboardTests : TestContext
17+
{
18+
[Fact]
19+
public void ShouldNotShowAboutMeStatisticsWhenDisabled()
20+
{
21+
var dashboardService = new Mock<IDashboardService>();
22+
this.AddTestAuthorization().SetAuthorized("test");
23+
Services.AddScoped(_ => CreateAppConfiguration(false));
24+
Services.AddScoped(_ => dashboardService.Object);
25+
Services.AddScoped(_ => new Mock<IRepository<BlogPost>>().Object);
26+
dashboardService.Setup(d => d.GetDashboardDataAsync())
27+
.ReturnsAsync(new DashboardData());
28+
29+
var cut = RenderComponent<Dashboard>();
30+
31+
cut.FindComponents<DashboardCard>()
32+
.Any(c => c.Instance.Text == "About Me:")
33+
.Should()
34+
.BeFalse();
35+
}
36+
37+
private static AppConfiguration CreateAppConfiguration(bool aboutMeEnabled)
38+
{
39+
return new AppConfiguration
40+
{
41+
IsAboutMeEnabled = aboutMeEnabled,
42+
};
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)