2021-09-07 23:40:58 +10:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using System;
|
2021-09-08 15:02:07 +10:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-09-08 15:18:46 +10:00
|
|
|
|
using System.Web.Mvc;
|
2021-09-08 15:02:07 +10:00
|
|
|
|
using Moq;
|
|
|
|
|
using SportsStore.Domain.Abstract;
|
|
|
|
|
using SportsStore.Domain.Entities;
|
|
|
|
|
using SportsStore.WebUI.Controllers;
|
2021-09-08 15:18:46 +10:00
|
|
|
|
using SportsStore.WebUI.HtmlHelpers;
|
|
|
|
|
using SportsStore.WebUI.Models;
|
2021-09-07 23:40:58 +10:00
|
|
|
|
|
|
|
|
|
namespace SportsStore.UnitTests
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class UnitTest1
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
2021-09-08 15:02:07 +10:00
|
|
|
|
public void Can_Paginate()
|
2021-09-07 23:40:58 +10:00
|
|
|
|
{
|
2021-09-08 15:02:07 +10:00
|
|
|
|
// Arrange
|
|
|
|
|
Mock<IProductRepository> mock = new Mock<IProductRepository>();
|
2021-09-08 15:18:46 +10:00
|
|
|
|
mock.Setup(m => m.Products).Returns(new Product[]
|
|
|
|
|
{
|
|
|
|
|
new Product { ProductID = 1, Name = "P1" },
|
|
|
|
|
new Product { ProductID = 2, Name = "P2" },
|
|
|
|
|
new Product { ProductID = 3, Name = "P3" },
|
|
|
|
|
new Product { ProductID = 4, Name = "P4" },
|
|
|
|
|
new Product { ProductID = 5, Name = "P5" }
|
2021-09-08 15:02:07 +10:00
|
|
|
|
});
|
|
|
|
|
ProductController controller = new ProductController(mock.Object);
|
|
|
|
|
controller.PageSize = 3;
|
|
|
|
|
// Act
|
2021-09-08 21:27:53 +10:00
|
|
|
|
ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model;
|
2021-09-08 15:02:07 +10:00
|
|
|
|
// Assert
|
2021-09-08 15:40:07 +10:00
|
|
|
|
Product[] prodArray = result.Products.ToArray();
|
2021-09-08 15:02:07 +10:00
|
|
|
|
Assert.IsTrue(prodArray.Length == 2);
|
|
|
|
|
Assert.AreEqual(prodArray[0].Name, "P4");
|
|
|
|
|
Assert.AreEqual(prodArray[1].Name, "P5");
|
2021-09-07 23:40:58 +10:00
|
|
|
|
}
|
2021-09-08 15:18:46 +10:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Can_Generate_Page_Links()
|
|
|
|
|
{
|
|
|
|
|
// Arrange - define an HTML helper - we need to do this
|
|
|
|
|
// in order to apply the extension method
|
|
|
|
|
HtmlHelper myHelper = null;
|
|
|
|
|
// Arrange - create PagingInfo data
|
|
|
|
|
PagingInfo pagingInfo = new PagingInfo
|
|
|
|
|
{
|
|
|
|
|
CurrentPage = 2,
|
|
|
|
|
TotalItems = 28,
|
|
|
|
|
ItemsPerPage = 10
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Arrange - set up the delegate using a lambda expression
|
|
|
|
|
Func<int, string> pageUrlDelegate = i => "Page" + i;
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual(@"<a class=""btn btn-default"" href=""Page1"">1</a>"
|
|
|
|
|
+ @"<a class=""btn btn-default btn-primary selected"" href=""Page2"">2</a>"
|
|
|
|
|
+ @"<a class=""btn btn-default"" href=""Page3"">3</a>",
|
|
|
|
|
result.ToString());
|
|
|
|
|
}
|
2021-09-08 15:40:07 +10:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Can_Send_Pagination_View_Model()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
|
Mock<IProductRepository> mock = new Mock<IProductRepository>();
|
2021-09-08 23:00:27 +10:00
|
|
|
|
mock.Setup(m => m.Products).Returns(new Product[]
|
|
|
|
|
{
|
|
|
|
|
new Product { ProductID = 1, Name = "P1" },
|
|
|
|
|
new Product { ProductID = 2, Name = "P2" },
|
|
|
|
|
new Product { ProductID = 3, Name = "P3" },
|
|
|
|
|
new Product { ProductID = 4, Name = "P4" },
|
|
|
|
|
new Product { ProductID = 5, Name = "P5" }
|
2021-09-08 15:40:07 +10:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
|
ProductController controller = new ProductController(mock.Object);
|
|
|
|
|
controller.PageSize = 3;
|
|
|
|
|
|
|
|
|
|
// Act
|
2021-09-08 21:27:53 +10:00
|
|
|
|
ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model;
|
2021-09-08 15:40:07 +10:00
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
PagingInfo pageInfo = result.PagingInfo;
|
|
|
|
|
Assert.AreEqual(pageInfo.CurrentPage, 2);
|
|
|
|
|
Assert.AreEqual(pageInfo.ItemsPerPage, 3);
|
|
|
|
|
Assert.AreEqual(pageInfo.TotalItems, 5);
|
|
|
|
|
Assert.AreEqual(pageInfo.TotalPages, 2);
|
|
|
|
|
}
|
2021-09-08 21:37:23 +10:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Can_Filter_Products()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
// - create the mock repository
|
|
|
|
|
Mock<IProductRepository> mock = new Mock<IProductRepository>();
|
|
|
|
|
mock.Setup(m => m.Products).Returns(new Product[]
|
|
|
|
|
{
|
|
|
|
|
new Product { ProductID = 1, Name = "P1", Category = "Cat1" },
|
|
|
|
|
new Product { ProductID = 2, Name = "P2", Category = "Cat2" },
|
|
|
|
|
new Product { ProductID = 3, Name = "P3", Category = "Cat1" },
|
|
|
|
|
new Product { ProductID = 4, Name = "P4", Category = "Cat2" },
|
|
|
|
|
new Product { ProductID = 5, Name = "P5", Category = "Cat3" }
|
|
|
|
|
});
|
|
|
|
|
// Arrange - create a controller and make the page size 3 items
|
|
|
|
|
ProductController controller = new ProductController(mock.Object);
|
|
|
|
|
controller.PageSize = 3;
|
|
|
|
|
// Action
|
|
|
|
|
Product[] result = ((ProductsListViewModel)controller.List("Cat2", 1).Model)
|
|
|
|
|
.Products.ToArray();
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual(result.Length, 2);
|
|
|
|
|
Assert.IsTrue(result[0].Name == "P2" && result[0].Category == "Cat2");
|
|
|
|
|
Assert.IsTrue(result[1].Name == "P4" && result[1].Category == "Cat2");
|
|
|
|
|
}
|
2021-09-08 23:00:27 +10:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Can_Create_Categories()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
// - create the mock repository
|
|
|
|
|
Mock<IProductRepository> mock = new Mock<IProductRepository>();
|
|
|
|
|
mock.Setup(m => m.Products).Returns(new Product[]
|
|
|
|
|
{
|
|
|
|
|
new Product { ProductID = 1, Name = "P1", Category = "Apples" },
|
|
|
|
|
new Product { ProductID = 2, Name = "P2", Category = "Apples" },
|
|
|
|
|
new Product { ProductID = 3, Name = "P3", Category = "Plums" },
|
|
|
|
|
new Product { ProductID = 4, Name = "P4", Category = "Oranges" },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Arrange - create the controller
|
|
|
|
|
NavController target = new NavController(mock.Object);
|
|
|
|
|
|
|
|
|
|
// Act = get the set of categories
|
|
|
|
|
string[] results = ((IEnumerable<string>)target.Menu().Model).ToArray();
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual(results.Length, 3);
|
|
|
|
|
Assert.AreEqual(results[0], "Apples");
|
|
|
|
|
Assert.AreEqual(results[1], "Oranges");
|
|
|
|
|
Assert.AreEqual(results[2], "Plums");
|
|
|
|
|
}
|
2021-09-08 23:24:09 +10:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Indicates_Selected_Category()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
// - create the mock repository
|
|
|
|
|
Mock<IProductRepository> mock = new Mock<IProductRepository>();
|
|
|
|
|
mock.Setup(m => m.Products).Returns(new Product[]
|
|
|
|
|
{
|
|
|
|
|
new Product { ProductID = 1, Name = "P1", Category = "Apples" },
|
|
|
|
|
new Product { ProductID = 4, Name = "P2", Category = "Oranges" },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Arrange - create the controller
|
|
|
|
|
NavController target = new NavController(mock.Object);
|
|
|
|
|
|
|
|
|
|
// Arrange - define the category to selected
|
|
|
|
|
string categoryToSelected = "Apples";
|
|
|
|
|
|
|
|
|
|
// Action
|
|
|
|
|
string result = target.Menu(categoryToSelected).ViewBag.SelectedCategory;
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual(categoryToSelected, result);
|
|
|
|
|
}
|
2021-09-08 23:00:27 +10:00
|
|
|
|
}
|
2021-09-07 23:40:58 +10:00
|
|
|
|
}
|