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>
This commit is contained in:
@@ -1,59 +1,70 @@
|
||||
// Mock CouchDB service for testing
|
||||
const mockCouchdbService = {
|
||||
createDocument: jest.fn(),
|
||||
findDocumentById: jest.fn(),
|
||||
updateDocument: jest.fn(),
|
||||
findByType: jest.fn(),
|
||||
initialize: jest.fn(),
|
||||
getDocument: jest.fn(),
|
||||
findUserById: jest.fn(),
|
||||
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.createDocument.mockReset();
|
||||
mockCouchdbService.findDocumentById.mockReset();
|
||||
mockCouchdbService.updateDocument.mockReset();
|
||||
mockCouchdbService.findByType.mockReset();
|
||||
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',
|
||||
points: 50,
|
||||
type: 'earned',
|
||||
amount: 50,
|
||||
transactionType: 'earned',
|
||||
description: 'Completed street cleaning task',
|
||||
source: {
|
||||
relatedEntity: {
|
||||
type: 'task_completion',
|
||||
referenceId: 'task_123'
|
||||
}
|
||||
},
|
||||
balanceAfter: 150
|
||||
};
|
||||
|
||||
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'
|
||||
const mockInsertResult = {
|
||||
ok: true,
|
||||
id: 'point_transaction_123',
|
||||
rev: '1-abc'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.insert.mockResolvedValue(mockInsertResult);
|
||||
|
||||
const transaction = await PointTransaction.create(transactionData);
|
||||
|
||||
expect(transaction._id).toBeDefined();
|
||||
expect(transaction.user).toBe(transactionData.user);
|
||||
expect(transaction.points).toBe(transactionData.points);
|
||||
expect(transaction.type).toBe(transactionData.type);
|
||||
expect(transaction.amount).toBe(transactionData.amount);
|
||||
expect(transaction.transactionType).toBe(transactionData.transactionType);
|
||||
expect(transaction.description).toBe(transactionData.description);
|
||||
expect(transaction.source.type).toBe(transactionData.source.type);
|
||||
expect(transaction.source.referenceId).toBe(transactionData.source.referenceId);
|
||||
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 () => {
|
||||
|
||||
Reference in New Issue
Block a user