Skip to content

Commit

Permalink
Merge pull request #222 from a0uda/UnitTests
Browse files Browse the repository at this point in the history
A few unit tests for schedulings posts.
  • Loading branch information
FatemaKotb authored May 11, 2024
2 parents 4b805e2 + f7475d3 commit e5e3427
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
86 changes: 86 additions & 0 deletions __test__/community/scheduledPosts.test.js
Original file line number Diff line number Diff line change
@@ -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: [],
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions src/services/communityScheduledPostsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand All @@ -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 } };
}

Expand All @@ -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 } };
}

Expand All @@ -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}!` };
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e5e3427

Please sign in to comment.