8
8
using LinkDotNet . Blog . Web . Shared ;
9
9
using LinkDotNet . Blog . Web . Shared . Admin ;
10
10
using LinkDotNet . Blog . Web . Shared . Services ;
11
+ using Microsoft . AspNetCore . Components . Forms ;
11
12
using Microsoft . EntityFrameworkCore ;
12
13
using Microsoft . Extensions . DependencyInjection ;
13
14
@@ -36,6 +37,23 @@ public async Task ShouldSaveBlogPostOnSave()
36
37
toastService . Verify ( t => t . ShowInfo ( "Created BlogPost My Title" , string . Empty , null ) , Times . Once ) ;
37
38
}
38
39
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
+
39
57
private static void TriggerNewBlogPost ( IRenderedComponent < CreateNewBlogPost > cut )
40
58
{
41
59
cut . Find ( "#title" ) . Input ( "My Title" ) ;
@@ -47,4 +65,19 @@ private static void TriggerNewBlogPost(IRenderedComponent<CreateNewBlogPost> cut
47
65
48
66
cut . Find ( "form" ) . Submit ( ) ;
49
67
}
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
+ }
50
83
}
0 commit comments