1
+ using System ;
2
+ using System . Linq ;
3
+ using System . Linq . Expressions ;
4
+ using System . Threading . Tasks ;
5
+ using Bunit ;
6
+ using LinkDotNet . Blog . Domain ;
7
+ using LinkDotNet . Blog . Infrastructure . Persistence ;
8
+ using LinkDotNet . Blog . TestUtilities ;
9
+ using LinkDotNet . Blog . Web . Features . Archive ;
10
+ using LinkDotNet . Blog . Web . Features . Components ;
11
+ using Microsoft . Extensions . DependencyInjection ;
12
+ using X . PagedList ;
13
+
14
+ namespace LinkDotNet . Blog . UnitTests . Web . Features . Archive ;
15
+
16
+ public class ArchiveTests : TestContext
17
+ {
18
+ [ Fact ]
19
+ public void ShouldDisplayAllBlogPosts ( )
20
+ {
21
+ var repository = new Mock < IRepository < BlogPost > > ( ) ;
22
+ Services . AddScoped ( _ => repository . Object ) ;
23
+ var allBlogPosts = new [ ]
24
+ {
25
+ CreateBlogPost ( new DateTime ( 2021 , 1 , 1 ) , "Blog Post 1" ) ,
26
+ CreateBlogPost ( new DateTime ( 2021 , 2 , 1 ) , "Blog Post 2" ) ,
27
+ CreateBlogPost ( new DateTime ( 2022 , 1 , 1 ) , "Blog Post 3" ) ,
28
+ } . OrderByDescending ( a => a . UpdatedDate ) ;
29
+ repository . Setup ( r => r . GetAllAsync (
30
+ It . IsAny < Expression < Func < BlogPost , bool > > > ( ) ,
31
+ It . IsAny < Expression < Func < BlogPost , object > > > ( ) ,
32
+ It . IsAny < bool > ( ) ,
33
+ It . IsAny < int > ( ) ,
34
+ It . IsAny < int > ( ) ) )
35
+ . ReturnsAsync ( new PagedList < BlogPost > ( allBlogPosts , 1 , 10 ) ) ;
36
+
37
+ var cut = RenderComponent < ArchivePage > ( ) ;
38
+
39
+ cut . WaitForElements ( "h2" ) ;
40
+ var yearHeader = cut . FindAll ( "h2" ) ;
41
+ yearHeader . Should ( ) . HaveCount ( 2 ) ;
42
+ yearHeader [ 0 ] . TextContent . Should ( ) . Be ( "2022" ) ;
43
+ yearHeader [ 1 ] . TextContent . Should ( ) . Be ( "2021" ) ;
44
+ var entries = cut . FindAll ( "li" ) ;
45
+ entries . Should ( ) . HaveCount ( 3 ) ;
46
+ entries [ 0 ] . TextContent . Should ( ) . Be ( "Blog Post 3" ) ;
47
+ entries [ 1 ] . TextContent . Should ( ) . Be ( "Blog Post 2" ) ;
48
+ entries [ 2 ] . TextContent . Should ( ) . Be ( "Blog Post 1" ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public void ShouldShowLoading ( )
53
+ {
54
+ var repository = new Mock < IRepository < BlogPost > > ( ) ;
55
+ Services . AddScoped ( _ => repository . Object ) ;
56
+ repository . Setup ( r => r . GetAllAsync (
57
+ It . IsAny < Expression < Func < BlogPost , bool > > > ( ) ,
58
+ It . IsAny < Expression < Func < BlogPost , object > > > ( ) ,
59
+ It . IsAny < bool > ( ) ,
60
+ It . IsAny < int > ( ) ,
61
+ It . IsAny < int > ( ) ) )
62
+ . Returns ( async ( ) =>
63
+ {
64
+ await Task . Delay ( 250 ) ;
65
+ return new PagedList < BlogPost > ( Array . Empty < BlogPost > ( ) , 1 , 1 ) ;
66
+ } ) ;
67
+
68
+ var cut = RenderComponent < ArchivePage > ( ) ;
69
+
70
+ cut . FindComponents < Loading > ( ) . Count . Should ( ) . Be ( 1 ) ;
71
+ }
72
+
73
+ private static BlogPost CreateBlogPost ( DateTime date , string title )
74
+ {
75
+ return new BlogPostBuilder ( )
76
+ . WithTitle ( title )
77
+ . WithUpdatedDate ( date )
78
+ . Build ( ) ;
79
+ }
80
+ }
0 commit comments