# Jest Testing Suite for POS Backend

This document explains the Jest testing setup for the POS Backend application.

## Installation

Jest is already installed as a dev dependency. Run tests with:

```bash
npm test
```

## Test Structure

Tests are organized in the `__tests__` directory with the following structure:

```
__tests__/
├── utils/                      # Utility function tests
│   ├── AppError.test.js        # Error class tests
│   └── CatchAsync.test.js      # Async wrapper tests
├── controllers/                # Controller tests
│   ├── productController.test.js
│   ├── ordersController.test.js
│   ├── authController.test.js
│   ├── categoryController.test.js
│   ├── CompanyController.test.js
│   └── cashincashoutController.test.js
├── integration/                # Integration tests
│   └── errorHandling.test.js
├── product.schema.test.js      # Product validation schema tests
├── ordervalidation.schema.test.js
└── uservalidation.schema.test.js
```

## Test Coverage

Run tests with coverage report:

```bash
npm test -- --coverage
```

Current coverage includes:

- **AppError utility**: 100% coverage
- **CatchAsync utility**: 100% coverage
- **Product schema validation**: 100% coverage
- **Order schema validation**: 100% coverage
- **User schema validation**: 100% coverage
- **Orders controller**: 100% coverage
- **Product controller**: Validation and creation tests
- **Auth controller**: Logout functionality tests

## Test Types

### Unit Tests
- **Validation Schemas**: Test data validation and coercion
- **Utilities**: Test error handling and async wrappers
- **Controllers**: Test individual controller methods with mocked dependencies

### Integration Tests
- **Error Handling**: Test error class behavior across the app

## Mocking Strategy

Controllers use Jest mocks for:
- Database models (e.g., `Product`, `Order`, `User`)
- External dependencies (bcryptjs, jsonwebtoken)
- Validation schemas

Example:
```javascript
jest.mock('../../Modles/ProductModle');
jest.mock('../../Valadation/product.schema');
```

## Running Specific Tests

Run tests for a specific file:
```bash
npm test -- product.schema.test.js
```

Run tests matching a pattern:
```bash
npm test -- --testNamePattern="validation"
```

Run tests in watch mode:
```bash
npm test -- --watch
```

## Configuration

Jest configuration is in `jest.config.js`:

- **Test Environment**: Node.js
- **Coverage Collection**: Enabled
- **Coverage Directory**: `./coverage`
- **Test Pattern**: `**/__tests__/**/*.test.js`

## Adding New Tests

1. Create a test file in the appropriate `__tests__` subdirectory
2. Use the naming convention: `<module>.test.js`
3. Structure tests with `describe()` and `test()` blocks
4. Mock external dependencies as needed
5. Run `npm test` to verify

Example test template:
```javascript
jest.mock('../../path/to/dependency');

const module = require('../../path/to/module');

describe('Module Name', () => {
  let mockReq, mockRes, mockNext;

  beforeEach(() => {
    // Setup mocks
  });

  test('should do something', async () => {
    // Arrange
    // Act
    // Assert
  });
});
```

## Continuous Integration

To integrate with CI/CD pipelines, tests can be run in CI mode:

```bash
npm test -- --ci --coverage --maxWorkers=2
```

## Troubleshooting

### Tests failing due to missing mocks
Ensure all external dependencies are properly mocked with `jest.mock()`.

### Coverage not meeting expectations
Check that the module files are properly imported and all code paths are exercised.

### Timeout errors
Increase Jest timeout for slower tests:
```javascript
jest.setTimeout(10000);
```

## Future Improvements

- Add integration tests with test database (MongoDB)
- Add E2E tests with supertest
- Add snapshot tests for responses
- Increase controller test coverage
- Add middleware tests
- Add model validation tests
