diff --git a/__test__/community/scheduledPosts.test.js b/__test__/community/scheduledPosts.test.js new file mode 100644 index 0000000..65efaa4 --- /dev/null +++ b/__test__/community/scheduledPosts.test.js @@ -0,0 +1,86 @@ +import { Post } from '../../src/db/models/Post.js' +import { scheduledPost } from '../../src/db/models/scheduledPosts.js' +import { postScheduledPost, getScheduledPosts, editScheduledPost, submitScheduledPost } from '../../src/services/communityScheduledPostsService.js' + +jest.mock('../../src/db/models/Post.js') +jest.mock('../../src/db/models/scheduledPosts.js') + +describe('postScheduledPost', () => { + it('should return an error if finding the scheduled post fails', async () => { + scheduledPost.findById.mockRejectedValue(new Error('Find error')); + const result = await postScheduledPost('post1'); + expect(result).toEqual({ err: { status: 500, message: 'Find error' } }); + }); + + it('should return an error if saving the post fails', async () => { + scheduledPost.findById.mockResolvedValue({ _doc: { scheduling_details: { repetition_option: 'none' } } }); + Post.mockImplementation(() => { + return { save: jest.fn().mockRejectedValue(new Error('Save error')) }; + }); + const result = await postScheduledPost('post1'); + expect(result).toEqual({ err: { status: 500, message: 'Save error' } }); + }); + + it('should return an error if deleting the scheduled post fails', async () => { + scheduledPost.findById.mockResolvedValue({ _doc: { scheduling_details: { repetition_option: 'none' } } }); + Post.mockImplementation(() => { + return { save: jest.fn().mockResolvedValue({ title: 'Post1' }) }; + }); + scheduledPost.deleteOne.mockRejectedValue(new Error('Delete error')); + const result = await postScheduledPost('post1'); + expect(result).toEqual({ err: { status: 500, message: 'Delete error' } }); + }); + + it('should return a success message if the post is posted successfully', async () => { + scheduledPost.findById.mockResolvedValue({ _doc: { scheduling_details: { repetition_option: 'none' } } }); + Post.mockImplementation(() => { + return { save: jest.fn().mockResolvedValue({ title: 'Post1', created_at: Date.now() }) }; + }); + scheduledPost.deleteOne.mockResolvedValue({}); + const result = await postScheduledPost('post1'); + expect(result.successMessage).toContain('Post with title Post1 posted successfully on'); + }); +}); + +describe('getScheduledPosts', () => { + const mockPosts = [ + { scheduling_details: { repetition_option: 'none' } }, + { scheduling_details: { repetition_option: 'daily' } }, + { scheduling_details: { repetition_option: 'weekly' } }, + { scheduling_details: { repetition_option: 'none' } }, + ]; + + beforeEach(() => { + scheduledPost.find.mockImplementation(() => ({ + select: jest.fn().mockReturnThis(), + sort: jest.fn().mockImplementation(() => mockPosts), + })); + }); + + it('should return recurring and non-recurring posts', async () => { + const result = await getScheduledPosts('community1'); + expect(result).toEqual({ + recurring_posts: [ + { scheduling_details: { repetition_option: 'daily' } }, + { scheduling_details: { repetition_option: 'weekly' } }, + ], + non_recurring_posts: [ + { scheduling_details: { repetition_option: 'none' } }, + { scheduling_details: { repetition_option: 'none' } }, + ], + }); + }); + + it('should return an empty array for recurring and non-recurring posts if no posts are found', async () => { + scheduledPost.find.mockReset(); + scheduledPost.find.mockImplementation(() => ({ + select: jest.fn().mockReturnThis(), + sort: jest.fn().mockImplementation(() => []), + })); + const result = await getScheduledPosts('community1'); + expect(result).toEqual({ + recurring_posts: [], + non_recurring_posts: [], + }); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index d8a0bdc..cbd5ca1 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node src/index.js", "dev": "nodemon src/index.js", - "test": "jest ./__test__/community/chat.test.js", + "test": "jest ./__test__/community/scheduledPosts.test.js", "docs": "jsdoc -c src/config/jsdocsconfig.json", "seed": "node ./seeds/seed.js", "coverage": "jest --coverage" diff --git a/src/services/communityScheduledPostsService.js b/src/services/communityScheduledPostsService.js index 20c0124..d795365 100644 --- a/src/services/communityScheduledPostsService.js +++ b/src/services/communityScheduledPostsService.js @@ -107,7 +107,7 @@ const savePostForScheduling = async (scheduling_details, postInput, user) => { } // Return the saved post. - console.log(`Post with id ${savedPost._id} and title ${savedPost.title} saved successfully to be posted in the future!`) + // console.log(`Post with id ${savedPost._id} and title ${savedPost.title} saved successfully to be posted in the future!`) return { saved_post_id: savedPost._id }; } @@ -128,7 +128,7 @@ const postScheduledPost = async (post_id) => { try { scheduled_post = await scheduledPost.findById(post_id); } catch (error) { - console.log(error) + // console.log(error) return { err: { status: 500, message: error.message } }; } @@ -140,7 +140,7 @@ const postScheduledPost = async (post_id) => { try { post = await post.save(); } catch (error) { - console.log(error) + // console.log(error) return { err: { status: 500, message: error.message } }; } @@ -150,15 +150,15 @@ const postScheduledPost = async (post_id) => { await scheduledPost.deleteOne({ _id: scheduled_post._id }); } catch (error) { - console.log(error) + // console.log(error) return { err: { status: 500, message: error.message } }; } - console.log(`Scheduled post with id ${scheduled_post._id} and title ${scheduledPost.title} removed successfully!`); + // console.log(`Scheduled post with id ${scheduled_post._id} and title ${scheduledPost.title} removed successfully!`); } // Return a success message. - console.log(`Post with id ${post._id} and title ${post.title} posted successfully on ${post.created_at}!`) + // console.log(`Post with id ${post._id} and title ${post.title} posted successfully on ${post.created_at}!`) return { successMessage: `Post with title ${post.title} posted successfully on ${post.created_at}!` }; } @@ -208,7 +208,7 @@ const editScheduledPost = async (post_id, new_description) => { post.description = new_description; post.edited_at = Date.now(); - console.log(post) + // console.log(post) try { await post.save();