-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathUpdateBlogPostPageTests.cs
78 lines (69 loc) · 3.28 KB
/
UpdateBlogPostPageTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Threading;
using System.Threading.Tasks;
using Blazored.Toast.Services;
using LinkDotNet.Blog.Domain;
using LinkDotNet.Blog.Infrastructure;
using LinkDotNet.Blog.Infrastructure.Persistence;
using LinkDotNet.Blog.TestUtilities;
using LinkDotNet.Blog.TestUtilities.Fakes;
using LinkDotNet.Blog.Web.Features;
using LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor;
using LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components;
using LinkDotNet.Blog.Web.Features.Components;
using LinkDotNet.Blog.Web.Features.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NCronJob;
using TestContext = Xunit.TestContext;
namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Admin.BlogPostEditor;
public class UpdateBlogPostPageTests : SqlDatabaseTestBase<BlogPost>
{
[Fact]
public async Task ShouldSaveBlogPostOnSave()
{
await using var ctx = new BunitContext();
ctx.ComponentFactories.Add<MarkdownTextArea, MarkdownFake>();
ctx.JSInterop.SetupVoid("hljs.highlightAll");
var toastService = Substitute.For<IToastService>();
ctx.Services.AddScoped(_ => Substitute.For<ICacheInvalidator>());
var instantRegistry = Substitute.For<IInstantJobRegistry>();
var blogPost = new BlogPostBuilder().WithTitle("Title").WithShortDescription("Sub").Build();
await Repository.StoreAsync(blogPost);
ctx.AddAuthorization().SetAuthorized("some username");
ctx.Services.AddScoped(_ => Repository);
ctx.Services.AddScoped(_ => toastService);
ctx.Services.AddScoped(_ => instantRegistry);
var shortCodeRepository = Substitute.For<IRepository<ShortCode>>();
shortCodeRepository.GetAllAsync().Returns(PagedList<ShortCode>.Empty);
ctx.Services.AddScoped(_ => shortCodeRepository);
using var cut = ctx.Render<UpdateBlogPostPage>(
p => p.Add(s => s.BlogPostId, blogPost.Id));
var newBlogPost = cut.FindComponent<CreateNewBlogPost>();
TriggerUpdate(newBlogPost);
var blogPostFromDb = await DbContext.BlogPosts.SingleOrDefaultAsync(t => t.Id == blogPost.Id, TestContext.Current.CancellationToken);
blogPostFromDb.ShouldNotBeNull();
blogPostFromDb.ShortDescription.ShouldBe("My new Description");
toastService.Received(1).ShowInfo("Updated BlogPost Title", null);
instantRegistry.Received(1).RunInstantJob<SimilarBlogPostJob>(Arg.Any<object>(), Arg.Any<CancellationToken>());
}
[Fact]
public void ShouldThrowWhenNoIdProvided()
{
using var ctx = new BunitContext();
ctx.ComponentFactories.Add<MarkdownTextArea, MarkdownFake>();
ctx.AddAuthorization().SetAuthorized("some username");
ctx.Services.AddScoped(_ => Repository);
ctx.Services.AddScoped(_ => Substitute.For<IToastService>());
ctx.Services.AddScoped(_ => Substitute.For<ICacheInvalidator>());
Action act = () => ctx.Render<UpdateBlogPostPage>(
p => p.Add(s => s.BlogPostId, null));
act.ShouldThrow<ArgumentNullException>();
}
private static void TriggerUpdate(IRenderedComponent<IComponent> cut)
{
cut.Find("#short").Input("My new Description");
cut.Find("form").Submit();
}
}