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
+ }
0 commit comments