1- using System . Threading . Tasks ;
1+ using System . Linq ;
2+ using System . Threading . Tasks ;
23using Bunit ;
34using FluentAssertions ;
45using LinkDotNet . Blog . TestUtilities ;
@@ -26,6 +27,7 @@ public async Task ShouldShowAllBlogPostsWithLatestOneFirst()
2627 ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
2728 ctx . Services . AddScoped ( _ => CreateSampleAppConfiguration ( ) ) ;
2829 var cut = ctx . RenderComponent < Index > ( ) ;
30+ cut . WaitForState ( ( ) => cut . FindAll ( ".blog-card" ) . Any ( ) ) ;
2931
3032 var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
3133
@@ -46,13 +48,67 @@ public async Task ShouldOnlyShowPublishedPosts()
4648 ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
4749 ctx . Services . AddScoped ( _ => CreateSampleAppConfiguration ( ) ) ;
4850 var cut = ctx . RenderComponent < Index > ( ) ;
51+ cut . WaitForState ( ( ) => cut . FindAll ( ".blog-card" ) . Any ( ) ) ;
4952
5053 var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
5154
5255 blogPosts . Should ( ) . HaveCount ( 1 ) ;
5356 blogPosts [ 0 ] . Find ( ".description h1" ) . InnerHtml . Should ( ) . Be ( "Published" ) ;
5457 }
5558
59+ [ Fact ]
60+ public async Task ShouldOnlyLoadTenEntities ( )
61+ {
62+ await CreatePublishedBlogPosts ( 11 ) ;
63+ using var ctx = new TestContext ( ) ;
64+ ctx . JSInterop . Mode = JSRuntimeMode . Loose ;
65+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
66+ ctx . Services . AddScoped ( _ => CreateSampleAppConfiguration ( ) ) ;
67+ var cut = ctx . RenderComponent < Index > ( ) ;
68+ cut . WaitForState ( ( ) => cut . FindAll ( ".blog-card" ) . Any ( ) ) ;
69+
70+ var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
71+
72+ blogPosts . Count . Should ( ) . Be ( 10 ) ;
73+ }
74+
75+ [ Fact ]
76+ public async Task ShouldLoadNextBatchOnClick ( )
77+ {
78+ await CreatePublishedBlogPosts ( 11 ) ;
79+ using var ctx = new TestContext ( ) ;
80+ ctx . JSInterop . Mode = JSRuntimeMode . Loose ;
81+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
82+ ctx . Services . AddScoped ( _ => CreateSampleAppConfiguration ( ) ) ;
83+ var cut = ctx . RenderComponent < Index > ( ) ;
84+
85+ cut . FindComponent < BlogPostNavigation > ( ) . Find ( "li:last-child a" ) . Click ( ) ;
86+
87+ cut . WaitForState ( ( ) => cut . FindAll ( ".blog-card" ) . Count == 1 ) ;
88+ var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
89+ blogPosts . Count . Should ( ) . Be ( 1 ) ;
90+ }
91+
92+ [ Fact ]
93+ public async Task ShouldLoadPreviousBatchOnClick ( )
94+ {
95+ await CreatePublishedBlogPosts ( 11 ) ;
96+ using var ctx = new TestContext ( ) ;
97+ ctx . JSInterop . Mode = JSRuntimeMode . Loose ;
98+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
99+ ctx . Services . AddScoped ( _ => CreateSampleAppConfiguration ( ) ) ;
100+ var cut = ctx . RenderComponent < Index > ( ) ;
101+ cut . WaitForState ( ( ) => cut . FindAll ( ".blog-card" ) . Any ( ) ) ;
102+ cut . FindComponent < BlogPostNavigation > ( ) . Find ( "li:last-child a" ) . Click ( ) ;
103+ cut . WaitForState ( ( ) => cut . FindAll ( ".blog-card" ) . Count == 1 ) ;
104+
105+ cut . FindComponent < BlogPostNavigation > ( ) . Find ( "li:first-child a" ) . Click ( ) ;
106+
107+ cut . WaitForState ( ( ) => cut . FindAll ( ".blog-card" ) . Count > 1 ) ;
108+ var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
109+ blogPosts . Count . Should ( ) . Be ( 10 ) ;
110+ }
111+
56112 private static AppConfiguration CreateSampleAppConfiguration ( )
57113 {
58114 return new ( )
@@ -66,5 +122,14 @@ private static AppConfiguration CreateSampleAppConfiguration()
66122 } ,
67123 } ;
68124 }
125+
126+ private async Task CreatePublishedBlogPosts ( int amount )
127+ {
128+ for ( var i = 0 ; i < amount ; i ++ )
129+ {
130+ var blogPost = new BlogPostBuilder ( ) . IsPublished ( ) . Build ( ) ;
131+ await BlogPostRepository . StoreAsync ( blogPost ) ;
132+ }
133+ }
69134 }
70135}
0 commit comments