3
3
using System . Threading . Tasks ;
4
4
using AngleSharp . Html . Dom ;
5
5
using AngleSharpWrappers ;
6
- using Bunit ;
7
6
using LinkDotNet . Blog . Domain ;
7
+ using LinkDotNet . Blog . Infrastructure . Persistence ;
8
+ using LinkDotNet . Blog . Infrastructure . Persistence . Sql ;
8
9
using LinkDotNet . Blog . TestUtilities ;
9
10
using LinkDotNet . Blog . Web . Features . Admin . Dashboard . Components ;
11
+ using LinkDotNet . Blog . Web . Features . Admin . Dashboard . Services ;
12
+ using Microsoft . AspNetCore . Components ;
10
13
using Microsoft . Extensions . DependencyInjection ;
11
14
12
15
namespace LinkDotNet . Blog . IntegrationTests . Web . Features . Admin . Dashboard . Components ;
@@ -19,7 +22,8 @@ public async Task ShouldShowCounts()
19
22
var blogPost = new BlogPostBuilder ( ) . WithTitle ( "I was clicked" ) . WithLikes ( 2 ) . Build ( ) ;
20
23
await Repository . StoreAsync ( blogPost ) ;
21
24
using var ctx = new TestContext ( ) ;
22
- ctx . Services . AddScoped ( _ => DbContext ) ;
25
+ ctx . Services . AddScoped < IRepository < BlogPost > > ( _ => new Repository < BlogPost > ( DbContext ) ) ;
26
+ ctx . Services . AddScoped < IRepository < UserRecord > > ( _ => new Repository < UserRecord > ( DbContext ) ) ;
23
27
await SaveBlogPostArticleClicked ( blogPost . Id , 10 ) ;
24
28
25
29
var cut = ctx . RenderComponent < VisitCountPerPage > ( ) ;
@@ -53,20 +57,22 @@ public async Task ShouldFilterByDate()
53
57
await DbContext . UserRecords . AddRangeAsync ( new [ ] { clicked1 , clicked2 , clicked3 , clicked4 } ) ;
54
58
await DbContext . SaveChangesAsync ( ) ;
55
59
using var ctx = new TestContext ( ) ;
56
- ctx . Services . AddScoped ( _ => DbContext ) ;
60
+ ctx . ComponentFactories . Add < DateRangeSelector , FilterStubComponent > ( ) ;
61
+ ctx . Services . AddScoped < IRepository < BlogPost > > ( _ => new Repository < BlogPost > ( DbContext ) ) ;
62
+ ctx . Services . AddScoped < IRepository < UserRecord > > ( _ => new Repository < UserRecord > ( DbContext ) ) ;
57
63
var cut = ctx . RenderComponent < VisitCountPerPage > ( ) ;
64
+ var filter = new Filter { StartDate = new DateTime ( 2019 , 1 , 1 ) , EndDate = new DateTime ( 2020 , 12 , 31 ) } ;
58
65
59
- cut . FindComponent < DateRangeSelector > ( ) . Find ( "#startDate" ) . Change ( new DateTime ( 2019 , 1 , 1 ) ) ;
60
- cut . FindComponent < DateRangeSelector > ( ) . Find ( "#endDate" ) . Change ( new DateTime ( 2020 , 12 , 31 ) ) ;
66
+ await cut . InvokeAsync ( ( ) => cut . FindComponent < FilterStubComponent > ( ) . Instance . FireFilterChanged ( filter ) ) ;
61
67
62
- cut . WaitForState ( ( ) => cut . FindAll ( "td" ) . Any ( ) ) ;
68
+ cut . WaitForState ( ( ) => cut . FindAll ( "td" ) . Count == 3 ) ;
63
69
var elements = cut . FindAll ( "td" ) . ToList ( ) ;
64
70
elements . Count . Should ( ) . Be ( 3 ) ;
65
71
var titleData = elements [ 0 ] . ChildNodes . Single ( ) as IHtmlAnchorElement ;
66
72
titleData . Should ( ) . NotBeNull ( ) ;
67
73
titleData . InnerHtml . Should ( ) . Be ( blogPost1 . Title ) ;
68
74
titleData . Href . Should ( ) . Contain ( $ "blogPost/{ blogPost1 . Id } ") ;
69
- elements [ 1 ] . InnerHtml . Should ( ) . Be ( "1" ) ;
75
+ cut . WaitForAssertion ( ( ) => elements [ 1 ] . InnerHtml . Should ( ) . Be ( "1" ) ) ;
70
76
}
71
77
72
78
[ Fact ]
@@ -87,7 +93,8 @@ public async Task ShouldShowTotalClickCount()
87
93
await DbContext . UserRecords . AddRangeAsync ( new [ ] { clicked1 , clicked2 , clicked3 , clicked4 } ) ;
88
94
await DbContext . SaveChangesAsync ( ) ;
89
95
using var ctx = new TestContext ( ) ;
90
- ctx . Services . AddScoped ( _ => DbContext ) ;
96
+ ctx . Services . AddScoped < IRepository < BlogPost > > ( _ => new Repository < BlogPost > ( DbContext ) ) ;
97
+ ctx . Services . AddScoped < IRepository < UserRecord > > ( _ => new Repository < UserRecord > ( DbContext ) ) ;
91
98
92
99
var cut = ctx . RenderComponent < VisitCountPerPage > ( ) ;
93
100
@@ -109,4 +116,12 @@ private async Task SaveBlogPostArticleClicked(string blogPostId, int count)
109
116
110
117
await DbContext . SaveChangesAsync ( ) ;
111
118
}
119
+
120
+ private class FilterStubComponent : ComponentBase
121
+ {
122
+ [ Parameter ]
123
+ public EventCallback < Filter > FilterChanged { get ; set ; }
124
+
125
+ public void FireFilterChanged ( Filter filter ) => FilterChanged . InvokeAsync ( filter ) ;
126
+ }
112
127
}
0 commit comments