|
2 | 2 | @using Xunit |
3 | 3 |
|
4 | 4 | <Fixture Setup="ctx => ctx.Services.AddMockJsRuntime()" |
5 | | - Tests="new Test[]{ EmptyTodoList, SettingLabel, TaskListRendersItemsUsingItemTemplate }"> |
| 5 | + Test="EmptyTodoList" |
| 6 | + Tests="new Test[]{ SettingLabel, TaskListRendersItemsUsingItemTemplate }"> |
6 | 7 | <ComponentUnderTest> |
7 | 8 | <TodoList> |
8 | 9 | <ItemsTemplate> |
|
31 | 32 |
|
32 | 33 | void EmptyTodoList(IRazorTestContext context) |
33 | 34 | { |
| 35 | + // Act - get the CUT |
34 | 36 | var cut = context.GetComponentUnderTest<TodoList>(); |
35 | 37 |
|
36 | | - cut.ShouldBe(context.GetFragment("EmptyTodoListRender")); |
| 38 | + // Assert - get the expected initial rendered HTML from the fragment |
| 39 | + // and use it to verify the initial rendered HTML |
| 40 | + var expectedInitialRender = context.GetFragment("EmptyTodoListRender"); |
| 41 | + cut.ShouldBe(expectedInitialRender); |
37 | 42 | } |
38 | 43 |
|
39 | 44 | void SettingLabel(IRazorTestContext context) |
40 | 45 | { |
| 46 | + // Arrange - get the CUT |
41 | 47 | var cut = context.GetComponentUnderTest<TodoList>(); |
42 | 48 |
|
| 49 | + // Act - update label |
43 | 50 | cut.SetParametersAndRender((nameof(TodoList.Label), "LABEL")); |
44 | 51 |
|
| 52 | + // Assert - verifiy that the placeholder and aria-label has changed |
45 | 53 | cut.GetChangesSinceFirstRender().ShouldAllBe( |
46 | 54 | diff => diff.ShouldBeAttributeChange("placeholder", "LABEL"), |
47 | 55 | diff => diff.ShouldBeAttributeChange("aria-label", "LABEL") |
|
50 | 58 |
|
51 | 59 | void TaskListRendersItemsUsingItemTemplate(IRazorTestContext context) |
52 | 60 | { |
| 61 | + // Arrange - get the cut and take a snapshot of the current render tree output |
53 | 62 | var cut = context.GetComponentUnderTest<TodoList>(); |
54 | 63 | cut.TakeSnapshot(); |
55 | 64 |
|
| 65 | + // Act - assign test todo items to the CUT |
56 | 66 | cut.SetParametersAndRender((nameof(TodoList.Items), TestItems)); |
57 | 67 |
|
| 68 | + // Assert - get the diffs since the snapshot and compare to the expected. |
58 | 69 | var diffs = cut.GetChangesSinceSnapshot(); |
59 | | - diffs.ShouldHaveSingleChange() |
60 | | - .ShouldBeAddition(context.GetFragment("TodoItemRender")); |
| 70 | + var expected = context.GetFragment("TodoItemRender"); |
| 71 | + diffs.ShouldHaveSingleChange().ShouldBeAddition(expected); |
61 | 72 | } |
62 | 73 | } |
63 | 74 |
|
|
86 | 97 |
|
87 | 98 | void OnFirstRenderInputFieldGetsFocus(IRazorTestContext context) |
88 | 99 | { |
| 100 | + // Act |
89 | 101 | var cut = context.GetComponentUnderTest<TodoList>(); |
90 | 102 |
|
91 | | - // assert that there is a call to document.body.focus.call with a single argument, |
| 103 | + // Assert that there is a call to document.body.focus.call with a single argument, |
92 | 104 | // a reference to the input element. |
93 | 105 | jsRtMock.VerifyInvoke("document.body.focus.call") |
94 | 106 | .Arguments.Single().ShouldBeElementReferenceTo(cut.Find("input")); |
95 | 107 | } |
96 | 108 |
|
97 | 109 | void AfterFirstRenderInputFieldDoesntGetFocusAfterRerenders(IRazorTestContext context) |
98 | 110 | { |
| 111 | + // Arrange |
99 | 112 | var cut = context.GetComponentUnderTest<TodoList>(); |
100 | 113 |
|
| 114 | + // Act |
101 | 115 | cut.Render(); // second render |
102 | 116 | cut.Render(); // thrid render |
103 | 117 | cut.Render(); // ... |
104 | 118 | cut.Render(); |
105 | 119 |
|
| 120 | + // Assert that focus logic only runs on first render (only called 1 time). |
106 | 121 | jsRtMock.VerifyInvoke("document.body.focus.call", calledTimes: 1); |
107 | 122 | } |
108 | 123 |
|
109 | 124 | void WhenAddTaskFormIsSubmittedWithNoTextOnAddingTodoIsNotCalled(IRazorTestContext context) |
110 | 125 | { |
| 126 | + // Arrange |
111 | 127 | var cut = context.GetComponentUnderTest<TodoList>(); |
112 | 128 |
|
| 129 | + // Act - submit the empty form |
113 | 130 | cut.Find("form").Submit(); |
114 | 131 |
|
| 132 | + // Assert - verify that no task was created |
115 | 133 | Assert.Null(createdTodo); |
116 | 134 | } |
117 | 135 |
|
118 | 136 | void WhenAddTaskFormIsSubmittedWithTextOnAddingTodoIsCalled(IRazorTestContext context) |
119 | 137 | { |
| 138 | + // Arrange - ensure createdTask is null |
120 | 139 | createdTodo = null; |
121 | 140 | var cut = context.GetComponentUnderTest<TodoList>(); |
122 | 141 | var taskValue = "HELLO WORLD TASK"; |
123 | 142 |
|
| 143 | + // Act - find input field and change its value, then submit the form |
124 | 144 | cut.Find("input").Change(taskValue); |
125 | 145 | cut.Find("form").Submit(); |
126 | 146 |
|
| 147 | + // Assert that a new task has been passed to the EventCallback listener and that the |
| 148 | + // new task has the expected value |
127 | 149 | Assert.NotNull(createdTodo); |
128 | 150 | Assert.Equal(taskValue, createdTodo?.Text); |
129 | 151 | } |
|
0 commit comments