/** * 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.50, 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.50, 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.50, 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.00, quantity: 2 }; window.groupOrderShop.cart['456'] = { name: 'Product 2', price: 5.50, 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.50, 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.00, 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.00, quantity: 2 }; window.groupOrderShop.cart['456'] = { name: 'Product B', price: 10.00, 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 {}; });