1
+ using System . Threading . Tasks ;
2
+ using Blazored . LocalStorage ;
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 ;
9
+ using LinkDotNet . Blog . Web . Shared ;
10
+ using LinkDotNet . Infrastructure . Persistence ;
11
+ using Microsoft . EntityFrameworkCore ;
12
+ using Microsoft . Extensions . DependencyInjection ;
13
+ using Moq ;
14
+ using Xunit ;
15
+
16
+ namespace LinkDotNet . Blog . IntegrationTests . Web . Pages
17
+ {
18
+ public class BlogPostPageTests : SqlDatabaseTestBase
19
+ {
20
+ [ Fact ]
21
+ public async Task ShouldAddLikeOnEvent ( )
22
+ {
23
+ var publishedPost = new BlogPostBuilder ( ) . WithLikes ( 2 ) . IsPublished ( ) . Build ( ) ;
24
+ await BlogPostRepository . StoreAsync ( publishedPost ) ;
25
+ using var ctx = new TestContext ( ) ;
26
+ ctx . JSInterop . Mode = JSRuntimeMode . Loose ;
27
+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
28
+ ctx . Services . AddScoped ( _ => new Mock < ILocalStorageService > ( ) . Object ) ;
29
+ ctx . Services . AddScoped ( _ => new Mock < IToastService > ( ) . Object ) ;
30
+ ctx . AddTestAuthorization ( ) . SetAuthorized ( "s" ) ;
31
+ var cut = ctx . RenderComponent < BlogPostPage > (
32
+ p => p . Add ( b => b . BlogPostId , publishedPost . Id ) ) ;
33
+ var likeComponent = cut . FindComponent < Like > ( ) ;
34
+ likeComponent . SetParametersAndRender ( c => c . Add ( p => p . BlogPost , publishedPost ) ) ;
35
+
36
+ likeComponent . Find ( "button" ) . Click ( ) ;
37
+
38
+ var fromDb = await DbContext . BlogPosts . AsNoTracking ( ) . SingleAsync ( d => d . Id == publishedPost . Id ) ;
39
+ fromDb . Likes . Should ( ) . Be ( 3 ) ;
40
+ }
41
+
42
+ [ Fact ]
43
+ public async Task ShouldSubtractLikeOnEvent ( )
44
+ {
45
+ var publishedPost = new BlogPostBuilder ( ) . WithLikes ( 2 ) . IsPublished ( ) . Build ( ) ;
46
+ await BlogPostRepository . StoreAsync ( publishedPost ) ;
47
+ using var ctx = new TestContext ( ) ;
48
+ var localStorage = new Mock < ILocalStorageService > ( ) ;
49
+ localStorage . Setup ( l => l . ContainKeyAsync ( "hasLiked" , default ) ) . ReturnsAsync ( true ) ;
50
+ localStorage . Setup ( l => l . GetItemAsync < bool > ( "hasLiked" , default ) ) . ReturnsAsync ( true ) ;
51
+ ctx . JSInterop . Mode = JSRuntimeMode . Loose ;
52
+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
53
+ ctx . Services . AddScoped ( _ => localStorage . Object ) ;
54
+ ctx . Services . AddScoped ( _ => new Mock < IToastService > ( ) . Object ) ;
55
+ ctx . AddTestAuthorization ( ) . SetAuthorized ( "s" ) ;
56
+ var cut = ctx . RenderComponent < BlogPostPage > (
57
+ p => p . Add ( b => b . BlogPostId , publishedPost . Id ) ) ;
58
+ var likeComponent = cut . FindComponent < Like > ( ) ;
59
+ likeComponent . SetParametersAndRender ( c => c . Add ( p => p . BlogPost , publishedPost ) ) ;
60
+
61
+ likeComponent . Find ( "button" ) . Click ( ) ;
62
+
63
+ var fromDb = await DbContext . BlogPosts . AsNoTracking ( ) . SingleAsync ( d => d . Id == publishedPost . Id ) ;
64
+ fromDb . Likes . Should ( ) . Be ( 1 ) ;
65
+ }
66
+ }
67
+ }
0 commit comments