Skip to content

Commit d0e1f9a

Browse files
committed
Added test for UploadFile content
1 parent fed4bd1 commit d0e1f9a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

tests/LinkDotNet.Blog.IntegrationTests/Web/Pages/Admin/CreateNewBlogPostPageTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using LinkDotNet.Blog.Web.Shared;
99
using LinkDotNet.Blog.Web.Shared.Admin;
1010
using LinkDotNet.Blog.Web.Shared.Services;
11+
using Microsoft.AspNetCore.Components.Forms;
1112
using Microsoft.EntityFrameworkCore;
1213
using Microsoft.Extensions.DependencyInjection;
1314

@@ -36,6 +37,23 @@ public async Task ShouldSaveBlogPostOnSave()
3637
toastService.Verify(t => t.ShowInfo("Created BlogPost My Title", string.Empty, null), Times.Once);
3738
}
3839

40+
[Fact]
41+
public async Task ShouldSetContentFromFile()
42+
{
43+
using var ctx = new TestContext();
44+
const string contentFromFile = "content";
45+
ctx.AddTestAuthorization().SetAuthorized("some username");
46+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => Repository);
47+
ctx.Services.AddScoped(_ => Mock.Of<IToastService>());
48+
var args = SetupUploadFile(contentFromFile, ctx);
49+
var cut = ctx.RenderComponent<CreateNewBlogPost>();
50+
var uploadFile = cut.FindComponent<UploadFile>();
51+
52+
await uploadFile.InvokeAsync(() => cut.FindComponent<InputFile>().Instance.OnChange.InvokeAsync(args));
53+
54+
cut.Find("#content").TextContent.Should().Be(contentFromFile);
55+
}
56+
3957
private static void TriggerNewBlogPost(IRenderedComponent<CreateNewBlogPost> cut)
4058
{
4159
cut.Find("#title").Input("My Title");
@@ -47,4 +65,19 @@ private static void TriggerNewBlogPost(IRenderedComponent<CreateNewBlogPost> cut
4765

4866
cut.Find("form").Submit();
4967
}
68+
69+
private static InputFileChangeEventArgs SetupUploadFile(string contentFromFile, TestContext ctx)
70+
{
71+
var file = new Mock<IBrowserFile>();
72+
var fileProcessor = new Mock<IFileProcessor>();
73+
fileProcessor.Setup(f => f.GetContent(file.Object)).ReturnsAsync(contentFromFile);
74+
var args = new InputFileChangeEventArgs(new[]
75+
{
76+
file.Object,
77+
});
78+
ctx.Services.AddScoped(_ => fileProcessor.Object);
79+
ctx.JSInterop.SetupVoid(invocation => invocation.Identifier == "Blazor._internal.InputFile.init")
80+
.SetVoidResult();
81+
return args;
82+
}
5083
}

0 commit comments

Comments
 (0)