1
+ using System . Linq ;
2
+ using System . Threading . Tasks ;
3
+ using Bunit ;
4
+ using FluentAssertions ;
5
+ using LinkDotNet . Blog . TestUtilities ;
6
+ using LinkDotNet . Blog . Web . Pages ;
7
+ using LinkDotNet . Blog . Web . Shared ;
8
+ using LinkDotNet . Infrastructure . Persistence ;
9
+ using Microsoft . Extensions . DependencyInjection ;
10
+ using Xunit ;
11
+
12
+ namespace LinkDotNet . Blog . IntegrationTests . Web . Pages
13
+ {
14
+ public class SearchTests : SqlDatabaseTestBase
15
+ {
16
+ [ Fact ]
17
+ public async Task ShouldFindBlogPostWhenTitleMatches ( )
18
+ {
19
+ var blogPost1 = new BlogPostBuilder ( ) . WithTitle ( "Title 1" ) . Build ( ) ;
20
+ var blogPost2 = new BlogPostBuilder ( ) . WithTitle ( "Title 2" ) . Build ( ) ;
21
+ await BlogPostRepository . StoreAsync ( blogPost1 ) ;
22
+ await BlogPostRepository . StoreAsync ( blogPost2 ) ;
23
+ using var ctx = new TestContext ( ) ;
24
+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
25
+
26
+ var cut = ctx . RenderComponent < Search > ( p => p . Add ( s => s . SearchTerm , "Title 1" ) ) ;
27
+
28
+ cut . WaitForState ( ( ) => cut . FindComponents < ShortBlogPost > ( ) . Any ( ) ) ;
29
+ var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
30
+ blogPosts . Should ( ) . HaveCount ( 1 ) ;
31
+ blogPosts . Single ( ) . Find ( ".description h1" ) . TextContent . Should ( ) . Be ( "Title 1" ) ;
32
+ }
33
+
34
+ [ Fact ]
35
+ public async Task ShouldFindBlogPostWhenTagMatches ( )
36
+ {
37
+ var blogPost1 = new BlogPostBuilder ( ) . WithTitle ( "Title 1" ) . WithTags ( "Cat" ) . Build ( ) ;
38
+ var blogPost2 = new BlogPostBuilder ( ) . WithTitle ( "Title 2" ) . WithTags ( "Dog" ) . Build ( ) ;
39
+ await BlogPostRepository . StoreAsync ( blogPost1 ) ;
40
+ await BlogPostRepository . StoreAsync ( blogPost2 ) ;
41
+ using var ctx = new TestContext ( ) ;
42
+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
43
+
44
+ var cut = ctx . RenderComponent < Search > ( p => p . Add ( s => s . SearchTerm , "Cat" ) ) ;
45
+
46
+ cut . WaitForState ( ( ) => cut . FindComponents < ShortBlogPost > ( ) . Any ( ) ) ;
47
+ var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
48
+ blogPosts . Should ( ) . HaveCount ( 1 ) ;
49
+ blogPosts . Single ( ) . Find ( ".description h1" ) . TextContent . Should ( ) . Be ( "Title 1" ) ;
50
+ }
51
+
52
+ [ Fact ]
53
+ public async Task ShouldUnescapeQuery ( )
54
+ {
55
+ var blogPost1 = new BlogPostBuilder ( ) . WithTitle ( "Title 1" ) . Build ( ) ;
56
+ await BlogPostRepository . StoreAsync ( blogPost1 ) ;
57
+ using var ctx = new TestContext ( ) ;
58
+ ctx . Services . AddScoped < IRepository > ( _ => BlogPostRepository ) ;
59
+
60
+ var cut = ctx . RenderComponent < Search > ( p => p . Add ( s => s . SearchTerm , "Title%201" ) ) ;
61
+
62
+ cut . WaitForState ( ( ) => cut . FindComponents < ShortBlogPost > ( ) . Any ( ) ) ;
63
+ var blogPosts = cut . FindComponents < ShortBlogPost > ( ) ;
64
+ blogPosts . Should ( ) . HaveCount ( 1 ) ;
65
+ blogPosts . Single ( ) . Find ( ".description h1" ) . TextContent . Should ( ) . Be ( "Title 1" ) ;
66
+ }
67
+ }
68
+ }
0 commit comments