Skip to content

Commit

Permalink
Fix and DRY ShoppingCartControllerTests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Nov 6, 2023
1 parent 478e9c6 commit fce2334
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions test/OrchardCore.Commerce.Tests/ShoppingCartControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ public class ShoppingCartControllerTests
public async Task AddExistingItemToCart()
{
var cartId = Guid.NewGuid().ToString();
await StoreCartAsync(cartId: null);
await StoreCartAsync(cartId);
using var controller = GetController();
await controller.AddItem(new ShoppingCartLineUpdateModel
{
Quantity = 7,
ProductSku = "foo",
});
await AddItemAsync(controller, cartId, 7, "foo");
var cart = await _cartStorage.RetrieveAsync(cartId);

Assert.Equal(
Expand All @@ -77,11 +73,7 @@ public async Task AddNewItemToCart()
var cartId = Guid.NewGuid().ToString();
await StoreCartAsync(cartId);
using var controller = GetController();
await controller.AddItem(new ShoppingCartLineUpdateModel
{
Quantity = 7,
ProductSku = "bar",
});
await AddItemAsync(controller, cartId, 7, "bar");
var cart = await _cartStorage.RetrieveAsync(cartId);

Assert.Equal(
Expand All @@ -102,13 +94,15 @@ await _cartStorage.StoreAsync(new ShoppingCart(
new ShoppingCartItem(4, "foo", _attrSet2Parsed),
new ShoppingCartItem(5, "foo", _attrSet3Parsed),
new ShoppingCartItem(6, "bar", _attrSet3Parsed)));

using var controller = GetController();
await controller.AddItem(new ShoppingCartLineUpdateModel { Quantity = 7, ProductSku = "foo" });
await controller.AddItem(new ShoppingCartLineUpdateModel { Quantity = 8, ProductSku = "foo", Attributes = _attrSet1 });
await controller.AddItem(new ShoppingCartLineUpdateModel { Quantity = 9, ProductSku = "foo", Attributes = _attrSet2 });
await controller.AddItem(new ShoppingCartLineUpdateModel { Quantity = 10, ProductSku = "foo", Attributes = _attrSet3 });
await controller.AddItem(new ShoppingCartLineUpdateModel { Quantity = 11, ProductSku = "bar", Attributes = _attrSet3 });
await controller.AddItem(new ShoppingCartLineUpdateModel { Quantity = 13, ProductSku = "baz", Attributes = _attrSet3 });
await AddItemAsync(controller, cartId: null, 7, "foo");
await AddItemAsync(controller, cartId: null, 8, "foo", _attrSet1);
await AddItemAsync(controller, cartId: null, 9, "foo", _attrSet2);
await AddItemAsync(controller, cartId: null, 10, "foo", _attrSet3);
await AddItemAsync(controller, cartId: null, 11, "bar", _attrSet3);
await AddItemAsync(controller, cartId: null, 13, "baz", _attrSet3);

var cart = await controller.Get();

Assert.Equal(
Expand Down Expand Up @@ -226,4 +220,19 @@ private ShoppingCartController GetController()

private Task StoreCartAsync(string cartId) =>
_cartStorage.StoreAsync(new ShoppingCart(new ShoppingCartItem(3, "foo")), cartId);

private static Task AddItemAsync(
ShoppingCartController controller,
string cartId,
int quantity,
string sku,
IDictionary<string, string[]> attributes = null) =>
controller.AddItem(
new ShoppingCartLineUpdateModel
{
Quantity = quantity,
ProductSku = sku,
Attributes = attributes,
},
cartId);
}

0 comments on commit fce2334

Please sign in to comment.