fix: update Comment and Reward tests to use local mocks and proper validation
- Fixed Comment.test.js to use local mocks instead of global mocks - Updated Comment tests to match new model structure (user.userId, post.postId) - Fixed Reward.test.js to use local mocks instead of global mocks - Updated Reward validation tests to expect ValidationError exceptions - All Phase 2 models now have 100% passing tests: - Event.js: 19/19 tests passing - Reward.js: 22/22 tests passing - Comment.js: 16/16 tests passing 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -1,20 +1,41 @@
|
||||
// Mock CouchDB service for testing
|
||||
const mockCouchdbService = {
|
||||
createDocument: jest.fn(),
|
||||
updateDocument: jest.fn(),
|
||||
deleteDocument: jest.fn(),
|
||||
getById: jest.fn(),
|
||||
find: jest.fn(),
|
||||
findUserById: jest.fn(),
|
||||
updateUserPoints: jest.fn(),
|
||||
bulkDocs: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
initialize: jest.fn(),
|
||||
isReady: jest.fn().mockReturnValue(true),
|
||||
shutdown: jest.fn()
|
||||
};
|
||||
|
||||
// Mock the service module
|
||||
jest.mock('../../services/couchdbService', () => mockCouchdbService);
|
||||
|
||||
const Reward = require('../../models/Reward');
|
||||
|
||||
describe('Reward Model', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
// Reset all mocks to ensure clean state
|
||||
global.mockCouchdbService.createDocument.mockReset();
|
||||
global.mockCouchdbService.updateDocument.mockReset();
|
||||
global.mockCouchdbService.deleteDocument.mockReset();
|
||||
global.mockCouchdbService.getById.mockReset();
|
||||
global.mockCouchdbService.find.mockReset();
|
||||
global.mockCouchdbService.findUserById.mockReset();
|
||||
global.mockCouchdbService.updateUserPoints.mockReset();
|
||||
global.mockCouchdbService.bulkDocs.mockReset();
|
||||
global.mockCouchdbService.create.mockReset();
|
||||
global.mockCouchdbService.update.mockReset();
|
||||
global.mockCouchdbService.delete.mockReset();
|
||||
mockCouchdbService.createDocument.mockReset();
|
||||
mockCouchdbService.updateDocument.mockReset();
|
||||
mockCouchdbService.deleteDocument.mockReset();
|
||||
mockCouchdbService.getById.mockReset();
|
||||
mockCouchdbService.find.mockReset();
|
||||
mockCouchdbService.findUserById.mockReset();
|
||||
mockCouchdbService.updateUserPoints.mockReset();
|
||||
mockCouchdbService.bulkDocs.mockReset();
|
||||
mockCouchdbService.create.mockReset();
|
||||
mockCouchdbService.update.mockReset();
|
||||
mockCouchdbService.delete.mockReset();
|
||||
});
|
||||
|
||||
describe('Schema Validation', () => {
|
||||
@@ -37,7 +58,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -55,67 +76,25 @@ describe('Reward Model', () => {
|
||||
cost: 50,
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'reward_123',
|
||||
_rev: '1-abc',
|
||||
type: 'reward',
|
||||
...rewardData,
|
||||
isActive: true,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
// The Reward model doesn't validate, so we test the behavior
|
||||
const reward = await Reward.create(rewardData);
|
||||
expect(reward.name).toBeUndefined();
|
||||
await expect(Reward.create(rewardData)).rejects.toThrow('Name is required');
|
||||
});
|
||||
|
||||
it('should require description field', async () => {
|
||||
it('should require description field', async () => {
|
||||
const rewardData = {
|
||||
name: 'Reward without description',
|
||||
cost: 50,
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'reward_123',
|
||||
_rev: '1-abc',
|
||||
type: 'reward',
|
||||
...rewardData,
|
||||
isActive: true,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
// The Reward model doesn't validate, so we test the behavior
|
||||
const reward = await Reward.create(rewardData);
|
||||
expect(reward.description).toBeUndefined();
|
||||
await expect(Reward.create(rewardData)).rejects.toThrow('Description is required');
|
||||
});
|
||||
|
||||
it('should require cost field', async () => {
|
||||
it('should require cost field', async () => {
|
||||
const rewardData = {
|
||||
name: 'Reward without cost',
|
||||
description: 'This reward has no cost',
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'reward_123',
|
||||
_rev: '1-abc',
|
||||
type: 'reward',
|
||||
...rewardData,
|
||||
isActive: true,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
// The Reward model doesn't validate, so we test the behavior
|
||||
const reward = await Reward.create(rewardData);
|
||||
expect(reward.cost).toBeUndefined();
|
||||
await expect(Reward.create(rewardData)).rejects.toThrow('Cost is required and must be a number');
|
||||
});
|
||||
|
||||
it('should validate cost is a positive number', async () => {
|
||||
@@ -125,21 +104,7 @@ describe('Reward Model', () => {
|
||||
cost: -10,
|
||||
};
|
||||
|
||||
const mockCreated = {
|
||||
_id: 'reward_123',
|
||||
_rev: '1-abc',
|
||||
type: 'reward',
|
||||
...rewardData,
|
||||
isActive: true,
|
||||
createdAt: '2023-01-01T00:00:00.000Z',
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
// The Reward model doesn't validate, so we test the behavior
|
||||
const reward = await Reward.create(rewardData);
|
||||
expect(reward.cost).toBe(-10);
|
||||
await expect(Reward.create(rewardData)).rejects.toThrow('Cost must be non-negative');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -162,7 +127,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -187,7 +152,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -219,7 +184,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -250,7 +215,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -275,7 +240,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
// The Reward model doesn't validate, so we test behavior
|
||||
const reward = await Reward.create(rewardData);
|
||||
@@ -302,7 +267,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -327,8 +292,8 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
global.mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
...mockReward,
|
||||
isActive: false,
|
||||
_rev: '2-def'
|
||||
@@ -370,7 +335,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -397,8 +362,8 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
global.mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
...mockReward,
|
||||
redeemedBy: [
|
||||
{
|
||||
@@ -444,7 +409,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
mockCouchdbService.createDocument.mockResolvedValue(mockCreated);
|
||||
|
||||
const reward = await Reward.create(rewardData);
|
||||
|
||||
@@ -472,8 +437,8 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
global.mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
mockCouchdbService.updateDocument.mockResolvedValue({
|
||||
...mockReward,
|
||||
isActive: false,
|
||||
_rev: '2-def',
|
||||
@@ -502,7 +467,7 @@ describe('Reward Model', () => {
|
||||
updatedAt: '2023-01-01T00:00:00.000Z'
|
||||
};
|
||||
|
||||
global.mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
mockCouchdbService.getById.mockResolvedValue(mockReward);
|
||||
|
||||
const reward = await Reward.findById('reward_123');
|
||||
expect(reward).toBeDefined();
|
||||
@@ -511,7 +476,7 @@ describe('Reward Model', () => {
|
||||
});
|
||||
|
||||
it('should return null when reward not found', async () => {
|
||||
global.mockCouchdbService.getById.mockResolvedValue(null);
|
||||
mockCouchdbService.getById.mockResolvedValue(null);
|
||||
|
||||
const reward = await Reward.findById('nonexistent');
|
||||
expect(reward).toBeNull();
|
||||
|
||||
Reference in New Issue
Block a user