feat: complete Event model standardized error handling
- Update Event.js with class-based structure and standardized error handling - Add constructor validation for required fields (title, description, date, location) - Implement withErrorHandling wrapper for all static methods - Add toJSON() and save() instance methods - Fix test infrastructure to use correct mock methods (createDocument vs create) - All 19 Event tests now passing with proper error handling 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -1,29 +1,24 @@
|
||||
// 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(),
|
||||
update: jest.fn(),
|
||||
};
|
||||
|
||||
// Mock the service module
|
||||
jest.mock('../../services/couchdbService', () => mockCouchdbService);
|
||||
const Event = require('../../models/Event');
|
||||
|
||||
describe('Event Model', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
// Reset all mocks to ensure clean state
|
||||
mockCouchdbService.createDocument.mockReset();
|
||||
mockCouchdbService.findDocumentById.mockReset();
|
||||
mockCouchdbService.updateDocument.mockReset();
|
||||
mockCouchdbService.findByType.mockReset();
|
||||
mockCouchdbService.create.mockReset();
|
||||
mockCouchdbService.getById.mockReset();
|
||||
mockCouchdbService.find.mockReset();
|
||||
global.mockCouchdbService.createDocument.mockReset();
|
||||
global.mockCouchdbService.findDocumentById.mockReset();
|
||||
global.mockCouchdbService.updateDocument.mockReset();
|
||||
global.mockCouchdbService.findByType.mockReset();
|
||||
global.mockCouchdbService.createDocument.mockReset();
|
||||
global.mockCouchdbService.getById.mockReset();
|
||||
global.mockCouchdbService.find.mockReset();
|
||||
|
||||
// Set up default implementations for tests that don't override them
|
||||
global.mockCouchdbService.createDocument.mockImplementation((doc) => Promise.resolve({
|
||||
_id: `test_${Date.now()}`,
|
||||
_rev: '1-test',
|
||||
...doc
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Schema Validation', () => {
|
||||
@@ -47,7 +42,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.create.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -122,7 +117,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -151,7 +146,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -180,7 +175,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -214,7 +209,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -255,7 +250,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -288,7 +283,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -319,7 +314,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -329,12 +324,16 @@ describe('Event Model', () => {
|
||||
expect(typeof event.updatedAt).toBe('string');
|
||||
});
|
||||
|
||||
it('should update updatedAt on modification', async () => {
|
||||
it('should update updatedAt on modification', async () => {
|
||||
const eventData = {
|
||||
title: 'Update Test Event',
|
||||
description: 'Testing update timestamp',
|
||||
title: 'Timestamp Event',
|
||||
description: 'Testing timestamps',
|
||||
date: '2023-12-01T10:00:00.000Z',
|
||||
location: 'Test Location',
|
||||
participants: [],
|
||||
status: 'upcoming',
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
const mockEvent = {
|
||||
@@ -348,19 +347,24 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.findDocumentById.mockResolvedValue(mockEvent);
|
||||
mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
global.mockCouchdbService.getById.mockResolvedValue(mockEvent);
|
||||
global.mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
...mockEvent,
|
||||
status: 'completed',
|
||||
_rev: '2-def',
|
||||
updatedAt: '2023-01-01T00:00:01.000Z'
|
||||
_rev: '2-def'
|
||||
});
|
||||
|
||||
const event = await Event.findById('event_123');
|
||||
const originalUpdatedAt = event.updatedAt;
|
||||
|
||||
// Wait a bit to ensure different timestamp
|
||||
await new Promise(resolve => setTimeout(resolve, 1));
|
||||
|
||||
event.status = 'completed';
|
||||
await event.save();
|
||||
|
||||
expect(event.updatedAt).toBe('2023-01-01T00:00:01.000Z');
|
||||
expect(event.updatedAt).not.toBe(originalUpdatedAt);
|
||||
expect(event.updatedAt).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -391,7 +395,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const event = await Event.create(eventData);
|
||||
|
||||
@@ -416,7 +420,7 @@ describe('Event Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
mockCouchdbService.findDocumentById.mockResolvedValue(mockEvent);
|
||||
global.mockCouchdbService.getById.mockResolvedValue(mockEvent);
|
||||
|
||||
const event = await Event.findById('event_123');
|
||||
expect(event).toBeDefined();
|
||||
@@ -425,7 +429,7 @@ describe('Event Model', () => {
|
||||
});
|
||||
|
||||
it('should return null when event not found', async () => {
|
||||
mockCouchdbService.findDocumentById.mockResolvedValue(null);
|
||||
global.mockCouchdbService.getById.mockResolvedValue(null);
|
||||
|
||||
const event = await Event.findById('nonexistent');
|
||||
expect(event).toBeNull();
|
||||
|
||||
Reference in New Issue
Block a user