You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

  1. package test.utils;
  2. import sys.io.File.getContent;
  3. import haxe.Json.parse;
  4. import utest.Assert;
  5. import utils.CartUtils;
  6. import Common;
  7. class TestCartUtils
  8. {
  9. static var products:Array<ProductInfo>;
  10. public function new() {
  11. var http = new haxe.Http("localhost/js/test/mocks.json?format=json");
  12. products = parse(getContent("js/test/mocks.json"));
  13. }
  14. public function testAddToCart()
  15. {
  16. var order = {
  17. products: [{
  18. product: products[0],
  19. quantity: 1
  20. }],
  21. total: products[0].price
  22. };
  23. var newProduct = products[1];
  24. order = CartUtils.addToCart(order, newProduct, 1);
  25. Assert.equals(order.products.length, 2);
  26. Assert.equals(order.total, products[0].price + products[1].price);
  27. order = CartUtils.addToCart(order, newProduct, 3);
  28. Assert.equals(order.products[1].quantity, 4);
  29. Assert.equals(order.total, products[0].price + 4 * products[1].price);
  30. }
  31. public function testRemoveFromCart()
  32. {
  33. var order = {
  34. products: [{
  35. product: products[0],
  36. quantity: 5
  37. }, {
  38. product: products[1],
  39. quantity: 3
  40. }],
  41. total: 5 * products[0].price + 3 * products[1].price
  42. };
  43. order = CartUtils.removeFromCart(order, products[0], 2);
  44. Assert.equals(order.products[0].quantity, 3);
  45. Assert.equals(order.total, 3 * products[0].price + 3 * products[1].price);
  46. order = CartUtils.removeFromCart(order, products[1]);
  47. Assert.equals(order.products.length, 1);
  48. Assert.equals(order.total, 3 * products[0].price);
  49. }
  50. }