addons-cm/website_sale_aplicoop/static/tests/test_cart_functions.js
snt 6fbc7b9456 [FIX] website_sale_aplicoop: Remove redundant string= attributes and fix OCA linting warnings
- Remove redundant string= from 17 field definitions where name matches string value (W8113)
- Convert @staticmethod to instance methods in selection methods for proper self.env._() access
- Fix W8161 (prefer-env-translation) by using self.env._() instead of standalone _()
- Fix W8301/W8115 (translation-not-lazy) by proper placement of % interpolation outside self.env._()
- Remove unused imports of odoo._ from group_order.py and sale_order_extension.py
- All OCA linting warnings in website_sale_aplicoop main models are now resolved

Changes:
- website_sale_aplicoop/models/group_order.py: 21 field definitions cleaned
- website_sale_aplicoop/models/sale_order_extension.py: 5 field definitions cleaned + @staticmethod conversion
- Consistent with OCA standards for addon submission
2026-02-18 17:54:43 +01:00

275 lines
9.4 KiB
JavaScript

/**
* QUnit Tests for Cart Functions
* Tests core cart functionality (add, remove, update, calculate)
*/
odoo.define("website_sale_aplicoop.test_cart_functions", function (require) {
"use strict";
var QUnit = window.QUnit;
QUnit.module(
"website_sale_aplicoop",
{
beforeEach: function () {
// Setup: Initialize groupOrderShop object
window.groupOrderShop = {
orderId: "1",
cart: {},
labels: {
save_cart: "Save Cart",
reload_cart: "Reload Cart",
checkout: "Checkout",
confirm_order: "Confirm Order",
back_to_cart: "Back to Cart",
},
};
// Clear localStorage
localStorage.clear();
},
afterEach: function () {
// Cleanup
localStorage.clear();
delete window.groupOrderShop;
},
},
function () {
QUnit.test("groupOrderShop object initializes correctly", function (assert) {
assert.expect(3);
assert.ok(window.groupOrderShop, "groupOrderShop object exists");
assert.equal(window.groupOrderShop.orderId, "1", "orderId is set");
assert.ok(typeof window.groupOrderShop.cart === "object", "cart is an object");
});
QUnit.test("cart starts empty", function (assert) {
assert.expect(1);
var cartKeys = Object.keys(window.groupOrderShop.cart);
assert.equal(cartKeys.length, 0, "cart has no items initially");
});
QUnit.test("can add item to cart", function (assert) {
assert.expect(4);
// Add a product to cart
var productId = "123";
var productData = {
name: "Test Product",
price: 10.5,
quantity: 2,
};
window.groupOrderShop.cart[productId] = productData;
assert.equal(Object.keys(window.groupOrderShop.cart).length, 1, "cart has 1 item");
assert.ok(window.groupOrderShop.cart[productId], "product exists in cart");
assert.equal(
window.groupOrderShop.cart[productId].name,
"Test Product",
"product name is correct"
);
assert.equal(
window.groupOrderShop.cart[productId].quantity,
2,
"product quantity is correct"
);
});
QUnit.test("can remove item from cart", function (assert) {
assert.expect(2);
// Add then remove
var productId = "123";
window.groupOrderShop.cart[productId] = {
name: "Test Product",
price: 10.5,
quantity: 2,
};
assert.equal(
Object.keys(window.groupOrderShop.cart).length,
1,
"cart has 1 item after add"
);
delete window.groupOrderShop.cart[productId];
assert.equal(
Object.keys(window.groupOrderShop.cart).length,
0,
"cart is empty after remove"
);
});
QUnit.test("can update item quantity", function (assert) {
assert.expect(3);
var productId = "123";
window.groupOrderShop.cart[productId] = {
name: "Test Product",
price: 10.5,
quantity: 2,
};
assert.equal(
window.groupOrderShop.cart[productId].quantity,
2,
"initial quantity is 2"
);
// Update quantity
window.groupOrderShop.cart[productId].quantity = 5;
assert.equal(
window.groupOrderShop.cart[productId].quantity,
5,
"quantity updated to 5"
);
assert.equal(
Object.keys(window.groupOrderShop.cart).length,
1,
"still only 1 item in cart"
);
});
QUnit.test("cart total calculates correctly", function (assert) {
assert.expect(1);
// Add multiple products
window.groupOrderShop.cart["123"] = {
name: "Product 1",
price: 10.0,
quantity: 2,
};
window.groupOrderShop.cart["456"] = {
name: "Product 2",
price: 5.5,
quantity: 3,
};
// Calculate total manually
var total = 0;
Object.keys(window.groupOrderShop.cart).forEach(function (productId) {
var item = window.groupOrderShop.cart[productId];
total += item.price * item.quantity;
});
// Expected: (10.00 * 2) + (5.50 * 3) = 20.00 + 16.50 = 36.50
assert.equal(total.toFixed(2), "36.50", "cart total is correct");
});
QUnit.test("localStorage saves cart correctly", function (assert) {
assert.expect(2);
var cartKey = "eskaera_1_cart";
var testCart = {
123: {
name: "Test Product",
price: 10.5,
quantity: 2,
},
};
// Save to localStorage
localStorage.setItem(cartKey, JSON.stringify(testCart));
// Retrieve and verify
var savedCart = JSON.parse(localStorage.getItem(cartKey));
assert.ok(savedCart, "cart was saved to localStorage");
assert.equal(savedCart["123"].name, "Test Product", "cart data is correct");
});
QUnit.test("labels object is initialized", function (assert) {
assert.expect(5);
assert.ok(window.groupOrderShop.labels, "labels object exists");
assert.equal(
window.groupOrderShop.labels["save_cart"],
"Save Cart",
"save_cart label exists"
);
assert.equal(
window.groupOrderShop.labels["reload_cart"],
"Reload Cart",
"reload_cart label exists"
);
assert.equal(
window.groupOrderShop.labels["checkout"],
"Checkout",
"checkout label exists"
);
assert.equal(
window.groupOrderShop.labels["confirm_order"],
"Confirm Order",
"confirm_order label exists"
);
});
QUnit.test("cart handles decimal quantities correctly", function (assert) {
assert.expect(2);
window.groupOrderShop.cart["123"] = {
name: "Weight Product",
price: 8.99,
quantity: 1.5,
};
var item = window.groupOrderShop.cart["123"];
var subtotal = item.price * item.quantity;
assert.equal(item.quantity, 1.5, "decimal quantity stored correctly");
assert.equal(
subtotal.toFixed(2),
"13.49",
"subtotal with decimal quantity is correct"
);
});
QUnit.test("cart handles zero quantity", function (assert) {
assert.expect(1);
window.groupOrderShop.cart["123"] = {
name: "Test Product",
price: 10.0,
quantity: 0,
};
var item = window.groupOrderShop.cart["123"];
var subtotal = item.price * item.quantity;
assert.equal(subtotal, 0, "zero quantity results in zero subtotal");
});
QUnit.test("cart handles multiple items with same price", function (assert) {
assert.expect(2);
window.groupOrderShop.cart["123"] = {
name: "Product A",
price: 10.0,
quantity: 2,
};
window.groupOrderShop.cart["456"] = {
name: "Product B",
price: 10.0,
quantity: 3,
};
var total = 0;
Object.keys(window.groupOrderShop.cart).forEach(function (productId) {
var item = window.groupOrderShop.cart[productId];
total += item.price * item.quantity;
});
assert.equal(Object.keys(window.groupOrderShop.cart).length, 2, "cart has 2 items");
assert.equal(total.toFixed(2), "50.00", "total is correct with same prices");
});
}
);
return {};
});