Skip to content

Commit d54cf84

Browse files
Add more tests for PrepareCustomerListModel and PrepareCustomerList
Cover group/tag/settings mapping and the missing-registered-group case for the list model, plus sales-employee filtering, paging, guest mapping and the empty-result case for the customer list. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 62956be commit d54cf84

1 file changed

Lines changed: 107 additions & 12 deletions

File tree

src/Tests/Grand.Web.Admin.Tests/Services/CustomerViewModelServiceTests.cs

Lines changed: 107 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class CustomerViewModelServiceTests
5050
private Mock<IDateTimeService> _dateTimeServiceMock;
5151
private Mock<IDownloadService> _downloadServiceMock;
5252
private Mock<INewsLetterSubscriptionService> _newsLetterSubscriptionServiceMock;
53+
private Mock<ITranslationService> _translationServiceMock;
54+
private Customer _currentCustomer;
5355
private CustomerViewModelService _customerViewModelService;
5456

5557
[TestInitialize]
@@ -68,12 +70,15 @@ public void Setup()
6870
_dateTimeServiceMock = new Mock<IDateTimeService>();
6971
_downloadServiceMock = new Mock<IDownloadService>();
7072
_newsLetterSubscriptionServiceMock = new Mock<INewsLetterSubscriptionService>();
73+
_translationServiceMock = new Mock<ITranslationService>();
74+
_translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns<string>(k => k);
7175

7276
_dateTimeServiceMock.Setup(d => d.ConvertToUserTime(It.IsAny<DateTime>(), It.IsAny<DateTimeKind>()))
7377
.Returns<DateTime, DateTimeKind>((dt, _) => dt);
7478

79+
_currentCustomer = new Customer { Id = CurrentCustomerId };
7580
var workContextMock = new Mock<IWorkContext>();
76-
workContextMock.Setup(w => w.CurrentCustomer).Returns(new Customer { Id = CurrentCustomerId });
81+
workContextMock.Setup(w => w.CurrentCustomer).Returns(_currentCustomer);
7782
var storeContextMock = new Mock<IStoreContext>();
7883
storeContextMock.Setup(s => s.CurrentStore).Returns(new Grand.Domain.Stores.Store { Id = CurrentStoreId });
7984
var contextAccessorMock = new Mock<IContextAccessor>();
@@ -129,7 +134,7 @@ public void Setup()
129134
_customerProductServiceMock.Object,
130135
_newsLetterSubscriptionServiceMock.Object,
131136
_dateTimeServiceMock.Object,
132-
new Mock<ITranslationService>().Object,
137+
_translationServiceMock.Object,
133138
_loyaltyPointsServiceMock.Object,
134139
new Mock<ICountryService>().Object,
135140
contextAccessorMock.Object,
@@ -516,19 +521,53 @@ public async Task DeleteCustomer_RemovesNewsletterSubscriptions()
516521
n => n.DeleteNewsLetterSubscription(subscription, It.IsAny<bool>()), Times.Once);
517522
}
518523

524+
[TestMethod]
525+
public async Task PrepareCustomerListModel_MapsSettingsGroupsAndTags()
526+
{
527+
var registered = new CustomerGroup { Id = "reg", Name = "Registered" };
528+
var guests = new CustomerGroup { Id = "gst", Name = "Guests" };
529+
_groupServiceMock.Setup(g => g.GetCustomerGroupBySystemName(It.IsAny<string>()))
530+
.ReturnsAsync(registered);
531+
_groupServiceMock
532+
.Setup(g => g.GetAllCustomerGroups(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(),
533+
It.IsAny<bool>()))
534+
.ReturnsAsync(new PagedList<CustomerGroup> { registered, guests });
535+
_customerTagServiceMock.Setup(t => t.GetAllCustomerTags())
536+
.ReturnsAsync(new List<CustomerTag> { new() { Id = "t1", Name = "VIP" } });
537+
538+
var model = await _customerViewModelService.PrepareCustomerListModel();
539+
540+
Assert.AreEqual(2, model.AvailableCustomerGroups.Count);
541+
Assert.IsTrue(model.AvailableCustomerGroups.Any(x => x.Value == "gst" && !x.Selected));
542+
Assert.IsTrue(model.AvailableCustomerTags.Any(x => x.Value == "t1" && x.Text == "VIP"));
543+
Assert.AreEqual(new CustomerSettings().UsernamesEnabled, model.UsernamesEnabled);
544+
Assert.AreEqual(new CustomerSettings().CompanyEnabled, model.CompanyEnabled);
545+
}
546+
547+
[TestMethod]
548+
public async Task PrepareCustomerListModel_RegisteredGroupMissing_NullSearchGroupId()
549+
{
550+
_groupServiceMock.Setup(g => g.GetCustomerGroupBySystemName(It.IsAny<string>()))
551+
.ReturnsAsync(new CustomerGroup { Id = "reg" });
552+
_groupServiceMock
553+
.Setup(g => g.GetAllCustomerGroups(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(),
554+
It.IsAny<bool>()))
555+
.ReturnsAsync(new PagedList<CustomerGroup> { new() { Id = "other" } });
556+
_customerTagServiceMock.Setup(t => t.GetAllCustomerTags()).ReturnsAsync(new List<CustomerTag>());
557+
558+
var model = await _customerViewModelService.PrepareCustomerListModel();
559+
560+
//registered group is not among the available groups -> the search id resolves to null
561+
Assert.AreEqual(1, model.SearchCustomerGroupIds.Count);
562+
Assert.IsNull(model.SearchCustomerGroupIds.First());
563+
Assert.IsFalse(model.AvailableCustomerGroups.Any(x => x.Selected));
564+
}
565+
519566
[TestMethod]
520567
public async Task PrepareCustomerList_MapsCustomers()
521568
{
522-
_customerServiceMock
523-
.Setup(c => c.GetAllCustomers(
524-
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
525-
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string[]>(),
526-
It.IsAny<string[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
527-
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
528-
It.IsAny<bool>(), It.IsAny<ShoppingCartType?>(), It.IsAny<int>(), It.IsAny<int>(),
529-
It.IsAny<System.Linq.Expressions.Expression<Func<Customer, object>>>()))
530-
.ReturnsAsync(new PagedList<Customer>
531-
{ new() { Id = "c1", Email = "customer@example.com", Active = true } });
569+
SetupGetAllCustomers(new PagedList<Customer>
570+
{ new() { Id = "c1", Email = "customer@example.com", Active = true } });
532571

533572
var (list, _) = await _customerViewModelService.PrepareCustomerList(
534573
new CustomerListModel(), new[] { "grp" }, new[] { "tag" }, 1, 10);
@@ -538,4 +577,60 @@ public async Task PrepareCustomerList_MapsCustomers()
538577
Assert.AreEqual("c1", items[0].Id);
539578
Assert.AreEqual("customer@example.com", items[0].Email);
540579
}
580+
581+
[TestMethod]
582+
public async Task PrepareCustomerList_FiltersBySalesEmployeeAndPaging()
583+
{
584+
_currentCustomer.SeId = "se-1";
585+
SetupGetAllCustomers(new PagedList<Customer>());
586+
587+
await _customerViewModelService.PrepareCustomerList(
588+
new CustomerListModel(), new[] { "grp" }, new[] { "tag" }, 2, 15);
589+
590+
//salesEmployeeId comes from the current customer, pageIndex is zero-based
591+
_customerServiceMock.Verify(c => c.GetAllCustomers(
592+
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
593+
It.IsAny<string>(), It.IsAny<string>(), "se-1",
594+
It.Is<string[]>(g => g.SequenceEqual(new[] { "grp" })),
595+
It.Is<string[]>(t => t.SequenceEqual(new[] { "tag" })),
596+
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
597+
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(),
598+
It.IsAny<ShoppingCartType?>(), 1, 15,
599+
It.IsAny<System.Linq.Expressions.Expression<Func<Customer, object>>>()), Times.Once);
600+
}
601+
602+
[TestMethod]
603+
public async Task PrepareCustomerList_GuestCustomer_UsesGuestResource()
604+
{
605+
SetupGetAllCustomers(new PagedList<Customer> { new() { Id = "guest", Email = "", Active = true } });
606+
607+
var (list, _) = await _customerViewModelService.PrepareCustomerList(
608+
new CustomerListModel(), Array.Empty<string>(), Array.Empty<string>(), 1, 10);
609+
610+
Assert.AreEqual("Admin.Customers.Guest", list.First().Email);
611+
}
612+
613+
[TestMethod]
614+
public async Task PrepareCustomerList_NoCustomers_ReturnsEmpty()
615+
{
616+
SetupGetAllCustomers(new PagedList<Customer>());
617+
618+
var (list, _) = await _customerViewModelService.PrepareCustomerList(
619+
new CustomerListModel(), Array.Empty<string>(), Array.Empty<string>(), 1, 10);
620+
621+
Assert.AreEqual(0, list.Count());
622+
}
623+
624+
private void SetupGetAllCustomers(IPagedList<Customer> result)
625+
{
626+
_customerServiceMock
627+
.Setup(c => c.GetAllCustomers(
628+
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
629+
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string[]>(),
630+
It.IsAny<string[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
631+
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
632+
It.IsAny<bool>(), It.IsAny<ShoppingCartType?>(), It.IsAny<int>(), It.IsAny<int>(),
633+
It.IsAny<System.Linq.Expressions.Expression<Func<Customer, object>>>()))
634+
.ReturnsAsync(result);
635+
}
541636
}

0 commit comments

Comments
 (0)