Files
adopt-a-street/backend/__tests__/models/PointTransaction.test.js
William Valentin c17019360c fix: resolve CouchDB test infrastructure issues
- Fix Badge.js model to use correct couchdbService.createDocument() method
- Update Badge.test.js with proper static method testing patterns
- Add missing Post model import in Post.test.js
- Update jest.setup.js with missing mock methods (get, destroy)
- Fix PointTransaction.test.js mock service definition
- Ensure consistent mock patterns across model tests

User and Badge model tests now pass with 40/40 tests working.
Post test import fixed, remaining test issues identified for next iteration.

🤖 Generated with AI Assistant

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
2025-11-03 00:07:20 -08:00

542 lines
16 KiB
JavaScript

// Mock CouchDB service for testing
const mockCouchdbService = {
create: jest.fn(),
insert: jest.fn(),
get: jest.fn(),
getById: jest.fn(),
find: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
findUserById: jest.fn(),
initialize: jest.fn().mockResolvedValue(true),
isReady: jest.fn().mockReturnValue(true),
isConnected: true,
isConnecting: false,
shutdown: jest.fn().mockResolvedValue(true),
};
// Mock the service module
jest.mock('../../services/couchdbService', () => mockCouchdbService);
const couchdbService = require('../../services/couchdbService');
const PointTransaction = require('../../models/PointTransaction');
describe('PointTransaction Model', () => {
beforeEach(() => {
mockCouchdbService.create.mockReset();
mockCouchdbService.insert.mockReset();
mockCouchdbService.get.mockReset();
mockCouchdbService.getById.mockReset();
mockCouchdbService.find.mockReset();
mockCouchdbService.update.mockReset();
mockCouchdbService.delete.mockReset();
mockCouchdbService.findUserById.mockReset();
});
describe('Schema Validation', () => {
it('should create a valid point transaction', async () => {
const transactionData = {
user: 'user_123',
amount: 50,
transactionType: 'earned',
description: 'Completed street cleaning task',
relatedEntity: {
type: 'task_completion',
referenceId: 'task_123'
},
balanceAfter: 150
};
const mockInsertResult = {
ok: true,
id: 'point_transaction_123',
rev: '1-abc'
};
mockCouchdbService.insert.mockResolvedValue(mockInsertResult);
const transaction = await PointTransaction.create(transactionData);
expect(transaction._id).toBeDefined();
expect(transaction.user).toBe(transactionData.user);
expect(transaction.amount).toBe(transactionData.amount);
expect(transaction.transactionType).toBe(transactionData.transactionType);
expect(transaction.description).toBe(transactionData.description);
expect(transaction.relatedEntity.type).toBe(transactionData.relatedEntity.type);
expect(transaction.relatedEntity.referenceId).toBe(transactionData.relatedEntity.referenceId);
expect(transaction.balanceAfter).toBe(transactionData.balanceAfter);
});
it('should require user field', async () => {
const transactionData = {
points: 50,
type: 'earned',
description: 'Transaction without user',
};
expect(() => new PointTransaction(transactionData)).toThrow();
});
it('should require points field', async () => {
const transactionData = {
user: 'user_123',
type: 'earned',
description: 'Transaction without points',
};
expect(() => new PointTransaction(transactionData)).toThrow();
});
it('should require type field', async () => {
const transactionData = {
user: 'user_123',
points: 50,
description: 'Transaction without type',
};
expect(() => new PointTransaction(transactionData)).toThrow();
});
it('should require description field', async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'earned',
};
expect(() => new PointTransaction(transactionData)).toThrow();
});
});
describe('Transaction Types', () => {
const validTypes = ['earned', 'spent', 'bonus', 'penalty', 'refund'];
validTypes.forEach(type => {
it(`should accept "${type}" as valid transaction type`, async () => {
const transactionData = {
user: 'user_123',
points: 50,
type,
description: `Testing ${type} transaction`,
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.type).toBe(type);
});
});
it('should reject invalid transaction type', async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'invalid_type',
description: 'Invalid type transaction',
};
expect(() => new PointTransaction(transactionData)).toThrow();
});
});
describe('Points Validation', () => {
it('should accept positive points for earned transactions', async () => {
const transactionData = {
user: 'user_123',
points: 100,
type: 'earned',
description: 'Earned points transaction',
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.points).toBe(100);
});
it('should accept negative points for spent transactions', async () => {
const transactionData = {
user: 'user_123',
points: -50,
type: 'spent',
description: 'Spent points transaction',
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.points).toBe(-50);
});
it('should accept positive points for bonus transactions', async () => {
const transactionData = {
user: 'user_123',
points: 25,
type: 'bonus',
description: 'Bonus points transaction',
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.points).toBe(25);
});
it('should accept negative points for penalty transactions', async () => {
const transactionData = {
user: 'user_123',
points: -10,
type: 'penalty',
description: 'Penalty points transaction',
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.points).toBe(-10);
});
});
describe('Source Information', () => {
it('should allow source information', async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'earned',
description: 'Transaction with source',
source: {
type: 'task_completion',
referenceId: 'task_123',
additionalInfo: 'Street cleaning task completed'
}
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.source.type).toBe('task_completion');
expect(transaction.source.referenceId).toBe('task_123');
expect(transaction.source.additionalInfo).toBe('Street cleaning task completed');
});
it('should not require source information', async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'earned',
description: 'Transaction without source',
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.source).toBeUndefined();
});
});
describe('Source Types', () => {
const validSourceTypes = [
'task_completion',
'street_adoption',
'event_participation',
'reward_redemption',
'badge_earned',
'manual_adjustment',
'system_bonus'
];
validSourceTypes.forEach(sourceType => {
it(`should accept "${sourceType}" as valid source type`, async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'earned',
description: `Testing ${sourceType} source`,
source: {
type: sourceType,
referenceId: 'ref_123'
}
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.source.type).toBe(sourceType);
});
});
});
describe('Relationships', () => {
it('should reference user ID', async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'earned',
description: 'User transaction',
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.user).toBe('user_123');
});
});
describe('Timestamps', () => {
it('should automatically set createdAt and updatedAt', async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'earned',
description: 'Timestamp test transaction',
};
const mockCreated = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
const transaction = await PointTransaction.create(transactionData);
expect(transaction.createdAt).toBeDefined();
expect(transaction.updatedAt).toBeDefined();
expect(typeof transaction.createdAt).toBe('string');
expect(typeof transaction.updatedAt).toBe('string');
});
it('should update updatedAt on modification', async () => {
const transactionData = {
user: 'user_123',
points: 50,
type: 'earned',
description: 'Update test transaction',
};
const mockTransaction = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
...transactionData,
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.findDocumentById.mockResolvedValue(mockTransaction);
mockCouchdbService.updateDocument.mockResolvedValue({
...mockTransaction,
description: 'Updated transaction description',
_rev: '2-def',
updatedAt: '2023-01-01T00:00:01.000Z'
});
const transaction = await PointTransaction.findById('point_transaction_123');
const originalUpdatedAt = transaction.updatedAt;
transaction.description = 'Updated transaction description';
await transaction.save();
expect(transaction.updatedAt).not.toBe(originalUpdatedAt);
});
});
describe('Static Methods', () => {
it('should find transaction by ID', async () => {
const mockTransaction = {
_id: 'point_transaction_123',
_rev: '1-abc',
type: 'point_transaction',
user: 'user_123',
points: 50,
type: 'earned',
description: 'Test transaction',
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
};
mockCouchdbService.findDocumentById.mockResolvedValue(mockTransaction);
const transaction = await PointTransaction.findById('point_transaction_123');
expect(transaction).toBeDefined();
expect(transaction._id).toBe('point_transaction_123');
expect(transaction.user).toBe('user_123');
});
it('should return null when transaction not found', async () => {
mockCouchdbService.findDocumentById.mockResolvedValue(null);
const transaction = await PointTransaction.findById('nonexistent');
expect(transaction).toBeNull();
});
it('should find transactions by user ID', async () => {
const mockTransactions = [
{
_id: 'point_transaction_1',
_rev: '1-abc',
type: 'point_transaction',
user: 'user_123',
points: 50,
type: 'earned',
description: 'Transaction 1',
createdAt: '2023-01-01T00:00:00.000Z',
updatedAt: '2023-01-01T00:00:00.000Z'
},
{
_id: 'point_transaction_2',
_rev: '1-abc',
type: 'point_transaction',
user: 'user_123',
points: -25,
type: 'spent',
description: 'Transaction 2',
createdAt: '2023-01-02T00:00:00.000Z',
updatedAt: '2023-01-02T00:00:00.000Z'
}
];
mockCouchdbService.findByType.mockResolvedValue(mockTransactions);
const transactions = await PointTransaction.findByUser('user_123');
expect(transactions).toHaveLength(2);
expect(transactions[0].user).toBe('user_123');
expect(transactions[1].user).toBe('user_123');
});
});
describe('Helper Methods', () => {
it('should calculate user balance', async () => {
const mockTransactions = [
{
_id: 'point_transaction_1',
type: 'point_transaction',
user: 'user_123',
points: 100,
type: 'earned'
},
{
_id: 'point_transaction_2',
type: 'point_transaction',
user: 'user_123',
points: -25,
type: 'spent'
},
{
_id: 'point_transaction_3',
type: 'point_transaction',
user: 'user_123',
points: 50,
type: 'earned'
}
];
mockCouchdbService.findByType.mockResolvedValue(mockTransactions);
const balance = await PointTransaction.getUserBalance('user_123');
expect(balance).toBe(125); // 100 - 25 + 50
});
it('should return 0 for user with no transactions', async () => {
mockCouchdbService.findByType.mockResolvedValue([]);
const balance = await PointTransaction.getUserBalance('user_456');
expect(balance).toBe(0);
});
});
});