# JavaScript Tests for website_sale_aplicoop

This directory contains QUnit tests for the JavaScript functionality of the website_sale_aplicoop module.

## Test Files

### 1. test_cart_functions.js
Tests for core cart functionality:
- Cart initialization
- Adding items to cart
- Removing items from cart
- Updating quantities
- Calculating totals
- localStorage persistence
- Decimal quantity handling
- Zero quantity handling

### 2. test_tooltips_labels.js
Tests for tooltip and label functionality:
- Tooltip initialization from labels
- Label loading and structure
- Missing label handling
- Label reinitialization
- JSON serialization of labels
- Empty labels handling

### 3. test_realtime_search.js
Tests for real-time product search:
- Search input functionality
- Category filtering
- Combined search and category filters
- Case-insensitive search
- Partial matching
- Whitespace trimming
- Product visibility toggling
- Result counting

### 4. test_suite.js
Main test suite that imports all test modules.

## Running the Tests

### Method 1: Via Odoo Test Runner (Recommended)

1. **Access the test interface:**
   ```
   http://localhost:8069/web/tests?mod=website_sale_aplicoop
   ```

2. **Run specific test modules:**
   ```
   http://localhost:8069/web/tests?mod=website_sale_aplicoop.test_cart_functions
   http://localhost:8069/web/tests?mod=website_sale_aplicoop.test_tooltips_labels
   http://localhost:8069/web/tests?mod=website_sale_aplicoop.test_realtime_search
   ```

3. **View results:**
   - Tests run in the browser
   - Results displayed in QUnit interface
   - Green = Pass, Red = Fail
   - Click failed tests to see details

### Method 2: Via Command Line

Run Odoo with test mode:

```bash
# Run all tests
docker-compose exec odoo odoo -d odoo --test-enable --stop-after-init

# Run with specific test tags
docker-compose exec odoo odoo -d odoo --test-enable --test-tags=website_sale_aplicoop --stop-after-init

# Run in verbose mode for more details
docker-compose exec odoo odoo -d odoo --test-enable --log-level=test --stop-after-init
```

### Method 3: Via Browser Console

1. Open the application page in browser
2. Open browser console (F12)
3. Run:
   ```javascript
   QUnit.start();
   ```

## Test Coverage

### Cart Functions (11 tests)
- ✅ Object initialization
- ✅ Empty cart verification
- ✅ Add item to cart
- ✅ Remove item from cart
- ✅ Update quantity
- ✅ Calculate total
- ✅ localStorage persistence
- ✅ Decimal quantities
- ✅ Zero quantity handling
- ✅ Same price products
- ✅ Label initialization

### Tooltips & Labels (10 tests)
- ✅ Tooltip initialization
- ✅ Missing label handling
- ✅ Label object structure
- ✅ Label data types
- ✅ Global label usage
- ✅ Reinitialization
- ✅ Elements without tooltips
- ✅ querySelectorAll functionality
- ✅ JSON serialization
- ✅ Empty labels handling

### Realtime Search (13 tests)
- ✅ Element existence
- ✅ Search by name
- ✅ Case insensitive search
- ✅ Empty search shows all
- ✅ Category filtering
- ✅ Combined filters
- ✅ Non-existent product
- ✅ Partial matching
- ✅ Whitespace trimming
- ✅ CSS class toggling
- ✅ Visibility restoration
- ✅ Result counting

**Total: 34 tests**

## Adding New Tests

1. Create a new test file in `/static/tests/`:
   ```javascript
   odoo.define('website_sale_aplicoop.test_my_feature', function (require) {
       'use strict';
       var QUnit = window.QUnit;

       QUnit.module('website_sale_aplicoop.my_feature', {
           beforeEach: function() {
               // Setup code
           },
           afterEach: function() {
               // Cleanup code
           }
       }, function() {
           QUnit.test('test description', function(assert) {
               assert.expect(1);
               assert.ok(true, 'test passes');
           });
       });
   });
   ```

2. Add to `test_suite.js`:
   ```javascript
   require('website_sale_aplicoop.test_my_feature');
   ```

3. Add to `__manifest__.py` assets:
   ```python
   'web.assets_tests': [
       # ... existing files ...
       'website_sale_aplicoop/static/tests/test_my_feature.js',
   ],
   ```

4. Reload module and run tests

## QUnit Assertions Reference

Common assertions used in tests:

- `assert.ok(value, message)` - Verify truthy value
- `assert.equal(actual, expected, message)` - Loose equality (==)
- `assert.strictEqual(actual, expected, message)` - Strict equality (===)
- `assert.deepEqual(actual, expected, message)` - Deep object comparison
- `assert.notOk(value, message)` - Verify falsy value
- `assert.notEqual(actual, expected, message)` - Verify not equal
- `assert.expect(count)` - Set expected assertion count

## Debugging Tests

### View Test Output
- Open browser console (F12)
- Check "Console" tab for test logs
- Check "Network" tab for failed requests

### Debug Individual Test
```javascript
QUnit.test('test name', function(assert) {
    debugger; // Browser will pause here
    // ... test code ...
});
```

### Run Single Test
```javascript
QUnit.only('test name', function(assert) {
    // Only this test will run
});
```

### Skip Test
```javascript
QUnit.skip('test name', function(assert) {
    // This test will be skipped
});
```

## Continuous Integration

Tests can be integrated into CI/CD pipelines:

```bash
# In CI script
docker-compose up -d
docker-compose exec -T odoo odoo -d odoo --test-enable --test-tags=website_sale_aplicoop --stop-after-init
exit_code=$?
docker-compose down
exit $exit_code
```

## Troubleshooting

### Tests not loading
- Verify module is installed and updated
- Check browser console for JavaScript errors
- Verify assets are properly declared in __manifest__.py
- Clear browser cache and restart Odoo

### Tests failing unexpectedly
- Check if labels are loaded (`window.groupOrderShop.labels`)
- Verify DOM elements exist before testing
- Check for timing issues (use beforeEach/afterEach)
- Verify localStorage is not blocked by browser

### Assets not found
- Update module: `docker-compose exec odoo odoo -d odoo -u website_sale_aplicoop`
- Clear assets cache: `docker-compose exec odoo rm -rf /var/lib/odoo/filestore/odoo/web/static/lib/minified_assets/`
- Restart Odoo

## Best Practices

1. **Use beforeEach/afterEach**: Clean up DOM and global state
2. **Expect assertions**: Always use `assert.expect(n)` to verify all assertions run
3. **Test isolation**: Each test should be independent
4. **Descriptive names**: Use clear, descriptive test names
5. **One concept per test**: Test one thing at a time
6. **Mock external dependencies**: Don't rely on real API calls
7. **Test edge cases**: Empty strings, null values, extreme numbers

## Resources

- [QUnit Documentation](https://qunitjs.com/)
- [Odoo JavaScript Testing](https://www.odoo.com/documentation/18.0/developer/reference/frontend/javascript_testing.html)
- [MDN Web Docs - Testing](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing)

---

**Maintainer**: Criptomart
**License**: AGPL-3.0
**Last Updated**: February 3, 2026
