- Replace jest.mock with proper hoisted mocks for Jest compatibility - Add missing CouchDB service methods to mocks (findUserById, create, getById, update) - Update Post model tests to work with static class methods instead of constructor validation - Fix mock service references throughout all model test files 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
608 lines
16 KiB
JavaScript
608 lines
16 KiB
JavaScript
// Mock CouchDB service for testing
|
|
jest.mock('../../services/couchdbService', () => ({
|
|
createDocument: jest.fn(),
|
|
findDocumentById: jest.fn(),
|
|
updateDocument: jest.fn(),
|
|
findByType: jest.fn(),
|
|
findUserById: jest.fn(),
|
|
create: jest.fn(),
|
|
getById: jest.fn(),
|
|
update: jest.fn(),
|
|
}));
|
|
|
|
const Post = require('../../models/Post');
|
|
const User = require('../../models/User');
|
|
const couchdbService = require('../../services/couchdbService');
|
|
|
|
describe('Post Model', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
// Reset all mocks to ensure clean state
|
|
couchdbService.createDocument.mockReset();
|
|
couchdbService.findDocumentById.mockReset();
|
|
couchdbService.updateDocument.mockReset();
|
|
couchdbService.findByType.mockReset();
|
|
couchdbService.findUserById.mockReset();
|
|
couchdbService.create.mockReset();
|
|
couchdbService.getById.mockReset();
|
|
couchdbService.update.mockReset();
|
|
});
|
|
|
|
describe('Schema Validation', () => {
|
|
it('should create a valid text post', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'This is a test post',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockUser = {
|
|
_id: 'user_123',
|
|
name: 'Test User',
|
|
profilePicture: '',
|
|
posts: [],
|
|
stats: { postsCreated: 0 }
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
user: {
|
|
userId: 'user_123',
|
|
name: 'Test User',
|
|
profilePicture: ''
|
|
},
|
|
content: postData.content,
|
|
likes: [],
|
|
likesCount: 0,
|
|
commentsCount: 0,
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.findUserById.mockResolvedValue(mockUser);
|
|
couchdbService.create.mockResolvedValue(mockCreated);
|
|
couchdbService.update.mockResolvedValue({});
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post._id).toBeDefined();
|
|
expect(post.content).toBe(postData.content);
|
|
expect(post.user.name).toBe('Test User');
|
|
expect(post.likes).toEqual([]);
|
|
expect(post.likesCount).toBe(0);
|
|
});
|
|
|
|
it('should require user field', async () => {
|
|
const postData = {
|
|
content: 'Post without user',
|
|
type: 'text',
|
|
};
|
|
|
|
await expect(Post.create(postData)).rejects.toThrow('User is required');
|
|
});
|
|
|
|
it('should require content field', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
type: 'text',
|
|
};
|
|
|
|
// Test that empty/undefined content is handled
|
|
const mockUser = {
|
|
_id: 'user_123',
|
|
name: 'Test User',
|
|
profilePicture: '',
|
|
posts: [],
|
|
stats: { postsCreated: 0 }
|
|
};
|
|
|
|
couchdbService.findUserById.mockResolvedValue(mockUser);
|
|
|
|
const post = await Post.create(postData);
|
|
expect(post.content).toBeUndefined();
|
|
});
|
|
|
|
it('should require type field', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Post without type',
|
|
};
|
|
|
|
const mockUser = {
|
|
_id: 'user_123',
|
|
name: 'Test User',
|
|
profilePicture: '',
|
|
posts: [],
|
|
stats: { postsCreated: 0 }
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
type: 'post',
|
|
user: { userId: 'user_123', name: 'Test User', profilePicture: '' },
|
|
content: postData.content,
|
|
likes: [],
|
|
likesCount: 0,
|
|
commentsCount: 0,
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.findUserById.mockResolvedValue(mockUser);
|
|
couchdbService.create.mockResolvedValue(mockCreated);
|
|
couchdbService.update.mockResolvedValue({});
|
|
|
|
const post = await Post.create(postData);
|
|
expect(post.type).toBe('post'); // Default type
|
|
});
|
|
});
|
|
|
|
describe('Post Types', () => {
|
|
const validTypes = ['text', 'image', 'achievement'];
|
|
|
|
validTypes.forEach(type => {
|
|
it(`should accept "${type}" as valid type`, async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: `This is a ${type} post`,
|
|
type,
|
|
};
|
|
|
|
const mockUser = {
|
|
_id: 'user_123',
|
|
name: 'Test User',
|
|
profilePicture: '',
|
|
posts: [],
|
|
stats: { postsCreated: 0 }
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
user: { userId: 'user_123', name: 'Test User', profilePicture: '' },
|
|
content: postData.content,
|
|
likes: [],
|
|
likesCount: 0,
|
|
commentsCount: 0,
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.findUserById.mockResolvedValue(mockUser);
|
|
couchdbService.create.mockResolvedValue(mockCreated);
|
|
couchdbService.update.mockResolvedValue({});
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.type).toBe('post'); // All posts have type 'post' in CouchDB
|
|
});
|
|
});
|
|
|
|
it('should reject invalid post type', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Invalid type post',
|
|
type: 'invalid_type',
|
|
};
|
|
|
|
expect(() => new Post(postData)).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('Image Posts', () => {
|
|
it('should allow image URL for image posts', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Check out this photo',
|
|
type: 'image',
|
|
imageUrl: 'https://example.com/image.jpg',
|
|
cloudinaryPublicId: 'post_123',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.imageUrl).toBe('https://example.com/image.jpg');
|
|
expect(post.cloudinaryPublicId).toBe('post_123');
|
|
});
|
|
|
|
it('should allow text post without image URL', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Just a text post',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.imageUrl).toBeUndefined();
|
|
expect(post.cloudinaryPublicId).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('Likes', () => {
|
|
it('should allow adding likes', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Post to be liked',
|
|
type: 'text',
|
|
likes: ['user_456']
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.likes).toHaveLength(1);
|
|
expect(post.likes[0]).toBe('user_456');
|
|
});
|
|
|
|
it('should allow multiple likes', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Popular post',
|
|
type: 'text',
|
|
likes: ['user_456', 'user_789']
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.likes).toHaveLength(2);
|
|
});
|
|
|
|
it('should start with empty likes array', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'New post',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.likes).toEqual([]);
|
|
expect(post.likes).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('Comments', () => {
|
|
it('should allow adding comments', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Post with comments',
|
|
type: 'text',
|
|
comments: ['comment_123']
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.comments).toHaveLength(1);
|
|
expect(post.comments[0]).toBe('comment_123');
|
|
});
|
|
|
|
it('should start with empty comments array', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'New post',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.comments).toEqual([]);
|
|
expect(post.comments).toHaveLength(0);
|
|
});
|
|
|
|
it('should allow multiple comments', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Post with multiple comments',
|
|
type: 'text',
|
|
comments: ['comment_123', 'comment_456', 'comment_789']
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.comments).toHaveLength(3);
|
|
});
|
|
});
|
|
|
|
describe('Timestamps', () => {
|
|
it('should automatically set createdAt and updatedAt', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Timestamp post',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.createdAt).toBeDefined();
|
|
expect(post.updatedAt).toBeDefined();
|
|
expect(typeof post.createdAt).toBe('string');
|
|
expect(typeof post.updatedAt).toBe('string');
|
|
});
|
|
|
|
it('should update updatedAt on modification', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Update test post',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockPost = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.findDocumentById.mockResolvedValue(mockPost);
|
|
couchdbService.updateDocument.mockResolvedValue({
|
|
...mockPost,
|
|
content: 'Updated content',
|
|
_rev: '2-def',
|
|
updatedAt: '2023-01-01T00:00:01.000Z'
|
|
});
|
|
|
|
const post = await Post.findById('post_123');
|
|
post.content = 'Updated content';
|
|
await post.save();
|
|
|
|
expect(post.updatedAt).toBe('2023-01-01T00:00:01.000Z');
|
|
});
|
|
});
|
|
|
|
describe('Relationships', () => {
|
|
it('should reference User model', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'User relationship post',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.user).toBe('user_123');
|
|
});
|
|
|
|
it('should store likes as user IDs', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Post with likes',
|
|
type: 'text',
|
|
likes: ['user_456']
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.likes).toHaveLength(1);
|
|
expect(post.likes[0]).toBe('user_456');
|
|
});
|
|
});
|
|
|
|
describe('Content Validation', () => {
|
|
it('should trim content', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: ' Content with spaces ',
|
|
type: 'text',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.content).toBe('Content with spaces');
|
|
});
|
|
|
|
it('should enforce maximum content length', async () => {
|
|
const longContent = 'a'.repeat(5001); // Assuming 5000 char limit
|
|
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: longContent,
|
|
type: 'text',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
// If no max length is enforced, the post should still save
|
|
expect(post.content).toBe(longContent);
|
|
});
|
|
});
|
|
|
|
describe('Achievement Posts', () => {
|
|
it('should create achievement type posts', async () => {
|
|
const postData = {
|
|
user: 'user_123',
|
|
content: 'Completed 10 tasks!',
|
|
type: 'achievement',
|
|
};
|
|
|
|
const mockCreated = {
|
|
_id: 'post_123',
|
|
_rev: '1-abc',
|
|
type: 'post',
|
|
...postData,
|
|
likes: [],
|
|
comments: [],
|
|
createdAt: '2023-01-01T00:00:00.000Z',
|
|
updatedAt: '2023-01-01T00:00:00.000Z'
|
|
};
|
|
|
|
couchdbService.createDocument.mockResolvedValue(mockCreated);
|
|
|
|
const post = await Post.create(postData);
|
|
|
|
expect(post.type).toBe('achievement');
|
|
expect(post.content).toBe('Completed 10 tasks!');
|
|
});
|
|
});
|
|
});
|