Skip to content

Commit ddd087c

Browse files
Merge pull request #733 from TransactionProcessing/copilot/improve-code-coverage-contracts-again
Add test coverage for Contracts New Page
2 parents e925759 + 5549ac1 commit ddd087c

1 file changed

Lines changed: 269 additions & 0 deletions

File tree

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
using AngleSharp.Dom;
2+
using Bunit;
3+
using EstateManagementUI.BusinessLogic.Models;
4+
using EstateManagementUI.BusinessLogic.Requests;
5+
using Moq;
6+
using Shouldly;
7+
using SimpleResults;
8+
using ContractsNew = EstateManagementUI.BlazorServer.Components.Pages.Contracts.New;
9+
10+
namespace EstateManagementUI.BlazorServer.Tests.Pages.Contracts;
11+
12+
public class ContractsNewPageTests : BaseTest
13+
{
14+
[Fact]
15+
public void ContractsNew_RendersCorrectly()
16+
{
17+
// Arrange
18+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
19+
.ReturnsAsync(Result.Success(new List<OperatorDropDownModel>()));
20+
21+
// Act
22+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
23+
24+
// Assert
25+
cut.Markup.ShouldContain("Create New Contract");
26+
}
27+
28+
[Fact]
29+
public void ContractsNew_HasCorrectPageTitle()
30+
{
31+
// Arrange
32+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
33+
.ReturnsAsync(Result.Success(new List<OperatorDropDownModel>()));
34+
35+
// Act
36+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
37+
38+
// Assert
39+
IRenderedComponent<Microsoft.AspNetCore.Components.Web.PageTitle> pageTitle = cut.FindComponent<Microsoft.AspNetCore.Components.Web.PageTitle>();
40+
pageTitle.Instance.ChildContent.ShouldNotBeNull();
41+
}
42+
43+
[Fact]
44+
public void ContractsNew_CancelButton_NavigatesToContractsIndex()
45+
{
46+
// Arrange
47+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
48+
.ReturnsAsync(Result.Success(new List<OperatorDropDownModel>()));
49+
50+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
51+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Create New Contract"), timeout: TimeSpan.FromSeconds(5));
52+
53+
// Act - Find and click the Cancel button
54+
IRefreshableElementCollection<IElement> buttons = cut.FindAll("button");
55+
IElement? cancelButton = buttons.FirstOrDefault(b => b.TextContent.Contains("Cancel"));
56+
cancelButton.ShouldNotBeNull();
57+
cancelButton.Click();
58+
59+
// Assert
60+
_fakeNavigationManager.Uri.ShouldContain("/contracts");
61+
}
62+
63+
[Fact]
64+
public void ContractsNew_CreateContractButton_IsPresent()
65+
{
66+
// Arrange
67+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
68+
.ReturnsAsync(Result.Success(new List<OperatorDropDownModel>()));
69+
70+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
71+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Create New Contract"), timeout: TimeSpan.FromSeconds(5));
72+
73+
// Act & Assert
74+
IElement createButton = cut.Find("#createContractButton");
75+
createButton.ShouldNotBeNull();
76+
}
77+
78+
[Fact]
79+
public void ContractsNew_LoadsOperatorsSuccessfully()
80+
{
81+
// Arrange
82+
List<OperatorDropDownModel> operators = new List<OperatorDropDownModel>
83+
{
84+
new OperatorDropDownModel
85+
{
86+
OperatorId = Guid.NewGuid(),
87+
OperatorName = "Test Operator 1"
88+
},
89+
new OperatorDropDownModel
90+
{
91+
OperatorId = Guid.NewGuid(),
92+
OperatorName = "Test Operator 2"
93+
}
94+
};
95+
96+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
97+
.ReturnsAsync(Result.Success(operators));
98+
99+
// Act
100+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
101+
102+
// Assert - Verify both operators are loaded and displayed in the dropdown
103+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Operator 1"), timeout: TimeSpan.FromSeconds(5));
104+
cut.Markup.ShouldContain("Test Operator 2");
105+
}
106+
107+
[Fact]
108+
public void ContractsNew_WithNoOperators_ShowsNoOperatorsMessage()
109+
{
110+
// Arrange
111+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
112+
.ReturnsAsync(Result.Success(new List<OperatorDropDownModel>()));
113+
114+
// Act
115+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
116+
117+
// Assert
118+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("No operators available"), timeout: TimeSpan.FromSeconds(5));
119+
}
120+
121+
[Fact]
122+
public void ContractsNew_SuccessfulCreation_NavigatesToContractsIndex()
123+
{
124+
// Arrange
125+
Guid operatorId = Guid.NewGuid();
126+
List<OperatorDropDownModel> operators = new List<OperatorDropDownModel>
127+
{
128+
new OperatorDropDownModel
129+
{
130+
OperatorId = operatorId,
131+
OperatorName = "Test Operator"
132+
}
133+
};
134+
135+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
136+
.ReturnsAsync(Result.Success(operators));
137+
_mockMediator.Setup(x => x.Send(It.IsAny<ContractCommands.CreateContractCommand>(), default))
138+
.ReturnsAsync(Result.Success());
139+
140+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
141+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Operator"), timeout: TimeSpan.FromSeconds(5));
142+
143+
// Act - Fill in the form
144+
IElement descriptionInput = cut.Find("input[placeholder='Enter contract description']");
145+
descriptionInput.Change("Test Contract Description");
146+
147+
IElement operatorSelect = cut.Find("select");
148+
operatorSelect.Change(operatorId.ToString());
149+
150+
// Submit the form
151+
IElement createButton = cut.Find("#createContractButton");
152+
createButton.Click();
153+
154+
// Assert
155+
cut.WaitForAssertion(() => {
156+
_mockMediator.Verify(x => x.Send(It.IsAny<ContractCommands.CreateContractCommand>(), default), Times.Once());
157+
_fakeNavigationManager.Uri.ShouldContain("/contracts");
158+
}, timeout: TimeSpan.FromSeconds(5));
159+
}
160+
161+
[Fact]
162+
public void ContractsNew_FailedCreation_ShowsErrorMessage()
163+
{
164+
// Arrange
165+
Guid operatorId = Guid.NewGuid();
166+
List<OperatorDropDownModel> operators = new List<OperatorDropDownModel>
167+
{
168+
new OperatorDropDownModel
169+
{
170+
OperatorId = operatorId,
171+
OperatorName = "Test Operator"
172+
}
173+
};
174+
175+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
176+
.ReturnsAsync(Result.Success(operators));
177+
_mockMediator.Setup(x => x.Send(It.IsAny<ContractCommands.CreateContractCommand>(), default))
178+
.ReturnsAsync(Result.Failure("Failed to create contract"));
179+
180+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
181+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Operator"), timeout: TimeSpan.FromSeconds(5));
182+
183+
// Act - Fill in the form
184+
IElement descriptionInput = cut.Find("input[placeholder='Enter contract description']");
185+
descriptionInput.Change("Test Contract Description");
186+
187+
IElement operatorSelect = cut.Find("select");
188+
operatorSelect.Change(operatorId.ToString());
189+
190+
// Submit the form
191+
IElement createButton = cut.Find("#createContractButton");
192+
createButton.Click();
193+
194+
// Assert
195+
cut.WaitForAssertion(() => {
196+
_mockMediator.Verify(x => x.Send(It.IsAny<ContractCommands.CreateContractCommand>(), default), Times.Once());
197+
cut.Markup.ShouldContain("Failed to create contract");
198+
}, timeout: TimeSpan.FromSeconds(5));
199+
}
200+
201+
[Fact]
202+
public void ContractsNew_FormValidation_RequiresDescription()
203+
{
204+
// Arrange
205+
Guid operatorId = Guid.NewGuid();
206+
List<OperatorDropDownModel> operators = new List<OperatorDropDownModel>
207+
{
208+
new OperatorDropDownModel
209+
{
210+
OperatorId = operatorId,
211+
OperatorName = "Test Operator"
212+
}
213+
};
214+
215+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
216+
.ReturnsAsync(Result.Success(operators));
217+
218+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
219+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Operator"), timeout: TimeSpan.FromSeconds(5));
220+
221+
// Act - Submit form without description
222+
IElement operatorSelect = cut.Find("select");
223+
operatorSelect.Change(operatorId.ToString());
224+
225+
IElement createButton = cut.Find("#createContractButton");
226+
createButton.Click();
227+
228+
// Assert - Validation message should appear
229+
cut.WaitForAssertion(() => {
230+
cut.Markup.ShouldContain("Description is required");
231+
// Should not call the mediator
232+
_mockMediator.Verify(x => x.Send(It.IsAny<ContractCommands.CreateContractCommand>(), default), Times.Never());
233+
}, timeout: TimeSpan.FromSeconds(5));
234+
}
235+
236+
[Fact]
237+
public void ContractsNew_FormValidation_RequiresOperator()
238+
{
239+
// Arrange
240+
List<OperatorDropDownModel> operators = new List<OperatorDropDownModel>
241+
{
242+
new OperatorDropDownModel
243+
{
244+
OperatorId = Guid.NewGuid(),
245+
OperatorName = "Test Operator"
246+
}
247+
};
248+
249+
_mockMediator.Setup(x => x.Send(It.IsAny<OperatorQueries.GetOperatorsForDropDownQuery>(), default))
250+
.ReturnsAsync(Result.Success(operators));
251+
252+
IRenderedComponent<ContractsNew> cut = RenderComponent<ContractsNew>();
253+
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Operator"), timeout: TimeSpan.FromSeconds(5));
254+
255+
// Act - Submit form without selecting operator
256+
IElement descriptionInput = cut.Find("input[placeholder='Enter contract description']");
257+
descriptionInput.Change("Test Contract Description");
258+
259+
IElement createButton = cut.Find("#createContractButton");
260+
createButton.Click();
261+
262+
// Assert - Validation message should appear
263+
cut.WaitForAssertion(() => {
264+
cut.Markup.ShouldContain("Operator is required");
265+
// Should not call the mediator
266+
_mockMediator.Verify(x => x.Send(It.IsAny<ContractCommands.CreateContractCommand>(), default), Times.Never());
267+
}, timeout: TimeSpan.FromSeconds(5));
268+
}
269+
}

0 commit comments

Comments
 (0)