1- using Egil . RazorComponents . Testing . Library . SampleApp . Components ;
1+ using AngleSharp . Dom ;
2+ using Egil . RazorComponents . Testing . Library . SampleApp . Components ;
23using Egil . RazorComponents . Testing . Library . SampleApp . Data ;
4+ using Egil . RazorComponents . Testing . Mocking . JSInterop ;
35using Microsoft . AspNetCore . Components ;
46using Microsoft . JSInterop ;
7+ using Shouldly ;
58using System ;
69using System . Collections . Generic ;
710using System . Linq ;
1114
1215namespace Egil . RazorComponents . Testing . Library . SampleApp . CodeOnlyTests
1316{
14- public class TodoListTest : ComponentFixtureBase
17+ public class TodoListTest : ComponentTestBase
1518 {
1619 private string GetExpectedHtml ( string label = "Task description" , string itemsHtml = "" ) =>
1720 $@ "<form>
@@ -28,43 +31,108 @@ private string GetExpectedHtml(string label = "Task description", string itemsHt
2831 [ Fact ( DisplayName = "Task list renders as empty with default html when no items is provided" ) ]
2932 public void Test001 ( )
3033 {
31- TestHost . AddMockJsRuntime ( ) ;
32- var cut = TestHost . AddComponent < TodoList > ( ) ;
34+ // arrange
35+ Services . AddMockJsRuntime ( ) ;
36+
37+ // act
38+ var cut = RenderComponent < TodoList > ( ) ;
3339
40+ // assert
3441 cut . ShouldBe ( GetExpectedHtml ( ) ) ;
3542 }
3643
3744 [ Fact ( DisplayName = "Task list with custom label renders correctly" ) ]
3845 public void Test002 ( )
3946 {
40- TestHost . AddMockJsRuntime ( ) ;
47+ // arrange
48+ Services . AddMockJsRuntime ( ) ;
4149 var label = "hello world" ;
42- var cut = TestHost . AddComponent < TodoList > ( ( nameof ( TodoList . Label ) , label ) ) ;
4350
51+ // act
52+ var cut = RenderComponent < TodoList > ( ( nameof ( TodoList . Label ) , label ) ) ;
53+
54+ // assert
4455 cut . ShouldBe ( GetExpectedHtml ( label : label ) ) ;
4556 }
4657
4758 [ Fact ( DisplayName = "Task list renders Items using ItemTemplate" ) ]
4859 public void Test003 ( )
4960 {
5061 // arrange
51- TestHost . AddMockJsRuntime ( ) ;
62+ Services . AddMockJsRuntime ( ) ;
63+ RenderFragment < Todo > itemTemplate = todo => builder => builder . AddMarkupContent ( 0 , $ "<li>{ todo . Id } </li>") ;
5264 var items = new [ ] { new Todo { Id = 42 } , new Todo { Id = 1337 } } ;
5365
5466 // act
55- var cut = TestHost . AddComponent < TodoList > (
67+ var cut = RenderComponent < TodoList > (
5668 ( nameof ( TodoList . Items ) , items ) ,
57- ( nameof ( TodoList . ItemsTemplate ) , GetItemTemplate ( ) )
69+ ( nameof ( TodoList . ItemsTemplate ) , itemTemplate )
5870 ) ;
5971
6072 // assert
6173 var expectedHtml = GetExpectedHtml ( itemsHtml : $ "<li>{ items [ 0 ] . Id } </li><li>{ items [ 1 ] . Id } </li>") ;
6274 cut . ShouldBe ( expectedHtml ) ;
75+ }
76+
77+ [ Fact ( DisplayName = "After first render, the new task input field has focus" ) ]
78+ public void Test004 ( )
79+ {
80+ // arrange
81+ var jsRtMock = Services . AddMockJsRuntime ( ) ;
82+
83+ // act
84+ var cut = RenderComponent < TodoList > ( ) ;
85+
86+ // assert that there is a call to document.body.focus.call with a single argument,
87+ // a reference to the input element.
88+ jsRtMock . VerifyInvoke ( "document.body.focus.call" )
89+ . Arguments . Single ( ) . ShouldBeElementReferenceTo ( cut . Find ( "input" ) ) ;
90+ }
91+
92+ [ Fact ( DisplayName = "The new task input field does not receive focus explicitly after second and later renders" ) ]
93+ public void Test0041 ( )
94+ {
95+ // arrange
96+ var jsRtMock = Services . AddMockJsRuntime ( ) ;
97+
98+ // act
99+ var cut = RenderComponent < TodoList > ( ) ; // first render
100+ cut . Render ( ) ; // second render
101+
102+ // assert
103+ jsRtMock . VerifyInvoke ( "document.body.focus.call" , calledTimes : 1 ) ;
104+ }
105+
106+
107+ [ Fact ( DisplayName = "When a text entered in the add task input field and the form is submitted, " +
108+ "the OnAddingTodo is raised with a new Todo containing the entered text" ) ]
109+ public void Test005 ( )
110+ {
111+ var jsRtMock = Services . AddMockJsRuntime ( ) ;
112+ var taskValue = "HELLO WORLD TASK" ;
113+ var createdTask = default ( Todo ) ;
114+ var cut = RenderComponent < TodoList > (
115+ ( nameof ( TodoList . OnAddingTodo ) , EventCallback . Factory . Create < Todo > ( this , task => createdTask = task ) )
116+ ) ;
117+
118+ cut . Find ( "input" ) . Change ( taskValue ) ;
119+ cut . Find ( "form" ) . Submit ( ) ;
120+
121+ createdTask . ShouldNotBeNull ( ) . Text . ShouldBe ( taskValue ) ;
122+ }
123+
124+ [ Fact ( DisplayName = "When add task form is submitted with no text OnAddingTodo is not called" ) ]
125+ public void Test006 ( )
126+ {
127+ var jsRtMock = Services . AddMockJsRuntime ( ) ;
128+ var createdTask = default ( Todo ) ;
129+ var cut = RenderComponent < TodoList > (
130+ ( nameof ( TodoList . OnAddingTodo ) , EventCallback . Factory . Create < Todo > ( this , task => createdTask = task ) )
131+ ) ;
132+
133+ cut . Find ( "form" ) . Submit ( ) ;
63134
64- RenderFragment < Todo > GetItemTemplate ( )
65- {
66- return todo => builder => builder . AddMarkupContent ( 0 , $ "<li>{ todo . Id } </li>") ;
67- }
135+ createdTask . ShouldBeNull ( ) ;
68136 }
69137 }
70138}
0 commit comments