Skip to content

Commit cd0a807

Browse files
Add comprehensive test coverage for Merchants Deposit page
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
1 parent f15e834 commit cd0a807

1 file changed

Lines changed: 224 additions & 0 deletions

File tree

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
using AngleSharp.Dom;
2+
using Bunit;
3+
using EstateManagementUI.BlazorServer.Common;
4+
using EstateManagementUI.BlazorServer.Models;
5+
using EstateManagementUI.BusinessLogic.Requests;
6+
using Moq;
7+
using Shouldly;
8+
using SimpleResults;
9+
using MerchantsDeposit = EstateManagementUI.BlazorServer.Components.Pages.Merchants.Deposit;
10+
11+
namespace EstateManagementUI.BlazorServer.Tests.Pages.Merchants;
12+
13+
public class MerchantsDepositPageTests : BaseTest
14+
{
15+
private readonly Guid _testMerchantId = Guid.NewGuid();
16+
17+
[Fact]
18+
public void Deposit_RendersCorrectly()
19+
{
20+
// Arrange
21+
SetupSuccessfulMerchantLoad();
22+
23+
// Act
24+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
25+
parameters.Add(p => p.MerchantId, _testMerchantId));
26+
27+
// Assert
28+
cut.Markup.ShouldContain("Make Merchant Deposit");
29+
}
30+
31+
[Fact]
32+
public void Deposit_HasCorrectPageTitle()
33+
{
34+
// Arrange
35+
SetupSuccessfulMerchantLoad();
36+
37+
// Act
38+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
39+
parameters.Add(p => p.MerchantId, _testMerchantId));
40+
41+
// Assert
42+
IRenderedComponent<Microsoft.AspNetCore.Components.Web.PageTitle> pageTitle =
43+
cut.FindComponent<Microsoft.AspNetCore.Components.Web.PageTitle>();
44+
pageTitle.Instance.ChildContent.ShouldNotBeNull();
45+
}
46+
47+
[Fact]
48+
public void Deposit_DisplaysMerchantName()
49+
{
50+
// Arrange
51+
SetupSuccessfulMerchantLoad("Test Merchant Name");
52+
53+
// Act
54+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
55+
parameters.Add(p => p.MerchantId, _testMerchantId));
56+
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5));
57+
58+
// Assert
59+
cut.Markup.ShouldContain("For merchant: Test Merchant Name");
60+
}
61+
62+
[Fact]
63+
public void Deposit_DisplaysFormFields()
64+
{
65+
// Arrange
66+
SetupSuccessfulMerchantLoad();
67+
68+
// Act
69+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
70+
parameters.Add(p => p.MerchantId, _testMerchantId));
71+
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5));
72+
73+
// Assert
74+
cut.Markup.ShouldContain("Deposit Amount");
75+
cut.Markup.ShouldContain("Date of Deposit");
76+
cut.Markup.ShouldContain("Reference");
77+
78+
IElement depositAmountInput = cut.Find("#depositAmount");
79+
depositAmountInput.ShouldNotBeNull();
80+
81+
IElement depositDateInput = cut.Find("#depositDate");
82+
depositDateInput.ShouldNotBeNull();
83+
84+
IElement depositReferenceInput = cut.Find("#depositReference");
85+
depositReferenceInput.ShouldNotBeNull();
86+
}
87+
88+
[Fact]
89+
public void Deposit_MakeDepositButton_SubmitsForm()
90+
{
91+
// Arrange
92+
SetupSuccessfulMerchantLoad();
93+
this.MerchantUIService.Setup(m => m.MakeMerchantDeposit(
94+
It.IsAny<CorrelationId>(),
95+
It.IsAny<Guid>(),
96+
It.IsAny<Guid>(),
97+
It.IsAny<MerchantModels.DepositModel>()))
98+
.ReturnsAsync(Result.Success);
99+
100+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
101+
parameters.Add(p => p.MerchantId, _testMerchantId));
102+
cut.Instance.SetDelayOverride(0);
103+
cut.Render(); // required to trigger re-render
104+
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5));
105+
106+
// Act - Fill form fields
107+
IElement depositAmountInput = cut.Find("#depositAmount");
108+
depositAmountInput.Change(100);
109+
110+
IElement depositDateInput = cut.Find("#depositDate");
111+
depositDateInput.Change(DateTime.Today.ToString("yyyy-MM-dd"));
112+
113+
IElement depositReferenceInput = cut.Find("#depositReference");
114+
depositReferenceInput.Change("TEST-REF-001");
115+
116+
// Find and click the Make Deposit button
117+
IRefreshableElementCollection<IElement> buttons = cut.FindAll("button");
118+
IElement? makeDepositButton = buttons.FirstOrDefault(b =>
119+
b.TextContent.Contains("Make Deposit") &&
120+
(b.GetAttribute("id") ?? "") == "makeDepositButton");
121+
makeDepositButton?.Click();
122+
123+
// Assert
124+
cut.WaitForAssertion(() =>
125+
cut.Markup.ShouldContain("Deposit recorded successfully"),
126+
timeout: TimeSpan.FromSeconds(5));
127+
128+
// Verify navigation to merchants index
129+
_fakeNavigationManager.Uri.ShouldContain("/merchants");
130+
}
131+
132+
[Fact]
133+
public void Deposit_MakeDepositButton_ShowsErrorOnFailure()
134+
{
135+
// Arrange
136+
SetupSuccessfulMerchantLoad();
137+
this.MerchantUIService.Setup(m => m.MakeMerchantDeposit(
138+
It.IsAny<CorrelationId>(),
139+
It.IsAny<Guid>(),
140+
It.IsAny<Guid>(),
141+
It.IsAny<MerchantModels.DepositModel>()))
142+
.ReturnsAsync(Result.Failure());
143+
144+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
145+
parameters.Add(p => p.MerchantId, _testMerchantId));
146+
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5));
147+
148+
// Act - Fill form fields
149+
IElement depositAmountInput = cut.Find("#depositAmount");
150+
depositAmountInput.Change(100);
151+
152+
IElement depositDateInput = cut.Find("#depositDate");
153+
depositDateInput.Change(DateTime.Today.ToString("yyyy-MM-dd"));
154+
155+
IElement depositReferenceInput = cut.Find("#depositReference");
156+
depositReferenceInput.Change("TEST-REF-001");
157+
158+
// Find and click the Make Deposit button
159+
IRefreshableElementCollection<IElement> buttons = cut.FindAll("button");
160+
IElement? makeDepositButton = buttons.FirstOrDefault(b =>
161+
b.TextContent.Contains("Make Deposit") &&
162+
(b.GetAttribute("id") ?? "") == "makeDepositButton");
163+
makeDepositButton?.Click();
164+
165+
// Assert
166+
cut.WaitForAssertion(() =>
167+
cut.Markup.ShouldContain("Failed to make deposit"),
168+
timeout: TimeSpan.FromSeconds(5));
169+
}
170+
171+
[Fact]
172+
public void Deposit_CancelButton_NavigatesToMerchantsIndex()
173+
{
174+
// Arrange
175+
SetupSuccessfulMerchantLoad();
176+
177+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
178+
parameters.Add(p => p.MerchantId, _testMerchantId));
179+
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5));
180+
181+
// Act - Find and click the Cancel button
182+
IRefreshableElementCollection<IElement> buttons = cut.FindAll("button");
183+
IElement? cancelButton = buttons.FirstOrDefault(b => b.TextContent.Contains("Cancel"));
184+
cancelButton?.Click();
185+
186+
// Assert
187+
_fakeNavigationManager.Uri.ShouldContain("/merchants");
188+
}
189+
190+
[Fact]
191+
public void Deposit_LoadMerchant_LoadFails_NavigatesToError()
192+
{
193+
// Arrange
194+
this.MerchantUIService.Setup(m => m.GetMerchant(
195+
It.IsAny<CorrelationId>(),
196+
It.IsAny<Guid>(),
197+
It.IsAny<Guid>()))
198+
.ReturnsAsync(Result.Failure());
199+
200+
// Act
201+
IRenderedComponent<MerchantsDeposit> cut = RenderComponent<MerchantsDeposit>(parameters =>
202+
parameters.Add(p => p.MerchantId, _testMerchantId));
203+
204+
// Assert
205+
_fakeNavigationManager.Uri.ShouldContain("error");
206+
}
207+
208+
// Helper methods
209+
private void SetupSuccessfulMerchantLoad(string merchantName = "Test Merchant")
210+
{
211+
MerchantModels.MerchantModel merchant = new()
212+
{
213+
MerchantId = _testMerchantId,
214+
MerchantName = merchantName,
215+
MerchantReference = "TEST-REF"
216+
};
217+
218+
this.MerchantUIService.Setup(m => m.GetMerchant(
219+
It.IsAny<CorrelationId>(),
220+
It.IsAny<Guid>(),
221+
It.IsAny<Guid>()))
222+
.ReturnsAsync(Result.Success(merchant));
223+
}
224+
}

0 commit comments

Comments
 (0)