-
-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ test: add tests for blog controller #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Adityavanjre
wants to merge
3
commits into
main
Choose a base branch
from
test-blog-controller-656758565419885959
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| const { getBlogs, getBlogById, createBlog, updateBlog, deleteBlog } = require('../controllers/blogController'); | ||
| const Blog = require('../models/Blog'); | ||
|
|
||
| jest.mock('../models/Blog'); | ||
|
|
||
| describe('Blog Controller', () => { | ||
| let req, res; | ||
|
|
||
| beforeEach(() => { | ||
| req = { | ||
| params: {}, | ||
| body: {}, | ||
| user: undefined | ||
| }; | ||
| res = { | ||
| json: jest.fn(), | ||
| status: jest.fn().mockReturnThis() | ||
| }; | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getBlogs', () => { | ||
| it('should fetch published blogs for public users', async () => { | ||
| const mockBlogs = [{ title: 'Blog 1' }, { title: 'Blog 2' }]; | ||
| const sortMock = jest.fn().mockResolvedValue(mockBlogs); | ||
| Blog.find.mockReturnValue({ sort: sortMock }); | ||
|
|
||
| await getBlogs(req, res); | ||
|
|
||
| expect(Blog.find).toHaveBeenCalledWith({ status: 'published' }); | ||
| expect(sortMock).toHaveBeenCalledWith({ createdAt: -1 }); | ||
| expect(res.json).toHaveBeenCalledWith(mockBlogs); | ||
| }); | ||
|
|
||
| it('should fetch all blogs for admin users', async () => { | ||
| req.user = { isAdmin: true }; | ||
| const mockBlogs = [{ title: 'Blog 1' }, { title: 'Draft' }]; | ||
| const sortMock = jest.fn().mockResolvedValue(mockBlogs); | ||
| Blog.find.mockReturnValue({ sort: sortMock }); | ||
|
|
||
| await getBlogs(req, res); | ||
|
|
||
| expect(Blog.find).toHaveBeenCalledWith({}); | ||
| expect(sortMock).toHaveBeenCalledWith({ createdAt: -1 }); | ||
| expect(res.json).toHaveBeenCalledWith(mockBlogs); | ||
| }); | ||
|
|
||
| it('should handle errors', async () => { | ||
| const errorMessage = 'Database error'; | ||
| Blog.find.mockImplementation(() => { | ||
| throw new Error(errorMessage); | ||
| }); | ||
|
|
||
| await getBlogs(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(500); | ||
| expect(res.json).toHaveBeenCalledWith({ message: errorMessage }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getBlogById', () => { | ||
| it('should find blog by slug first', async () => { | ||
| req.params.id = 'my-blog-post'; | ||
| const mockBlog = { title: 'My Blog Post', status: 'published' }; | ||
| Blog.findOne.mockResolvedValue(mockBlog); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(Blog.findOne).toHaveBeenCalledWith({ slug: 'my-blog-post' }); | ||
| expect(Blog.findById).not.toHaveBeenCalled(); | ||
| expect(res.json).toHaveBeenCalledWith(mockBlog); | ||
| }); | ||
|
|
||
| it('should find blog by ID if slug not found and ID is valid', async () => { | ||
| req.params.id = '507f1f77bcf86cd799439011'; | ||
| const mockBlog = { title: 'My Blog Post', status: 'published' }; | ||
| Blog.findOne.mockResolvedValue(null); | ||
| Blog.findById.mockResolvedValue(mockBlog); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(Blog.findOne).toHaveBeenCalledWith({ slug: '507f1f77bcf86cd799439011' }); | ||
| expect(Blog.findById).toHaveBeenCalledWith('507f1f77bcf86cd799439011'); | ||
| expect(res.json).toHaveBeenCalledWith(mockBlog); | ||
| }); | ||
|
|
||
| it('should not search by ID if slug not found and ID is invalid format', async () => { | ||
| req.params.id = 'invalid-id-format'; | ||
| Blog.findOne.mockResolvedValue(null); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(Blog.findOne).toHaveBeenCalledWith({ slug: 'invalid-id-format' }); | ||
| expect(Blog.findById).not.toHaveBeenCalled(); | ||
| expect(res.status).toHaveBeenCalledWith(404); | ||
| expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' }); | ||
| }); | ||
|
|
||
| it('should return 404 if not found', async () => { | ||
| req.params.id = 'non-existent-slug'; | ||
| Blog.findOne.mockResolvedValue(null); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(404); | ||
| expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' }); | ||
| }); | ||
|
|
||
| it('should handle errors', async () => { | ||
| req.params.id = 'my-blog-post'; | ||
| const errorMessage = 'Database error'; | ||
| Blog.findOne.mockRejectedValue(new Error(errorMessage)); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(500); | ||
| expect(res.json).toHaveBeenCalledWith({ message: errorMessage }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createBlog', () => { | ||
| it('should create a new blog post', async () => { | ||
| req.body = { | ||
| title: 'New Blog', | ||
| author: 'Test Author', | ||
| category: 'Tech', | ||
| image: 'image.jpg', | ||
| excerpt: 'Excerpt', | ||
| content: 'Content', | ||
| readTime: '5 min', | ||
| tags: ['test'], | ||
| slug: 'new-blog', | ||
| status: 'draft' | ||
| }; | ||
|
|
||
| const mockSavedBlog = { _id: '123', ...req.body }; | ||
| const saveMock = jest.fn().mockResolvedValue(mockSavedBlog); | ||
| Blog.mockImplementation(() => ({ | ||
| save: saveMock | ||
| })); | ||
|
|
||
| await createBlog(req, res); | ||
|
|
||
| expect(Blog).toHaveBeenCalledWith(req.body); | ||
| expect(saveMock).toHaveBeenCalled(); | ||
| expect(res.status).toHaveBeenCalledWith(201); | ||
| expect(res.json).toHaveBeenCalledWith(mockSavedBlog); | ||
| }); | ||
|
|
||
| it('should handle validation errors', async () => { | ||
| req.body = { title: 'Missing Fields' }; | ||
| const errorMessage = 'Validation Error'; | ||
| Blog.mockImplementation(() => ({ | ||
| save: jest.fn().mockRejectedValue(new Error(errorMessage)) | ||
| })); | ||
|
|
||
| await createBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(400); | ||
| expect(res.json).toHaveBeenCalledWith({ message: errorMessage }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateBlog', () => { | ||
| it('should update a blog post', async () => { | ||
| req.params.id = '123'; | ||
| req.body = { title: 'Updated Title', content: 'Updated content' }; | ||
|
|
||
| const saveMock = jest.fn().mockResolvedValue({ _id: '123', ...req.body }); | ||
| const mockBlog = { _id: '123', title: 'Old Title', save: saveMock }; | ||
| Blog.findById.mockResolvedValue(mockBlog); | ||
|
|
||
| await updateBlog(req, res); | ||
|
|
||
| expect(Blog.findById).toHaveBeenCalledWith('123'); | ||
| expect(mockBlog.title).toBe('Updated Title'); | ||
| expect(mockBlog.content).toBe('Updated content'); | ||
| expect(saveMock).toHaveBeenCalled(); | ||
| expect(res.json).toHaveBeenCalledWith({ _id: '123', ...req.body }); | ||
| }); | ||
|
|
||
| it('should return 404 if blog to update not found', async () => { | ||
| req.params.id = 'non-existent-id'; | ||
| Blog.findById.mockResolvedValue(null); | ||
|
|
||
| await updateBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(404); | ||
| expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' }); | ||
| }); | ||
|
|
||
| it('should handle update errors', async () => { | ||
| req.params.id = '123'; | ||
| const errorMessage = 'Update Error'; | ||
| Blog.findById.mockRejectedValue(new Error(errorMessage)); | ||
|
|
||
| await updateBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(400); | ||
| expect(res.json).toHaveBeenCalledWith({ message: errorMessage }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('deleteBlog', () => { | ||
| it('should delete a blog post', async () => { | ||
| req.params.id = '123'; | ||
| const deleteOneMock = jest.fn().mockResolvedValue(); | ||
| const mockBlog = { _id: '123', title: 'Blog to Delete', deleteOne: deleteOneMock }; | ||
| Blog.findById.mockResolvedValue(mockBlog); | ||
|
|
||
| await deleteBlog(req, res); | ||
|
|
||
| expect(Blog.findById).toHaveBeenCalledWith('123'); | ||
| expect(deleteOneMock).toHaveBeenCalled(); | ||
| expect(res.json).toHaveBeenCalledWith({ message: 'Blog post removed' }); | ||
| }); | ||
|
|
||
| it('should return 404 if blog to delete not found', async () => { | ||
| req.params.id = 'non-existent-id'; | ||
| Blog.findById.mockResolvedValue(null); | ||
|
|
||
| await deleteBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(404); | ||
| expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' }); | ||
| }); | ||
|
|
||
| it('should handle deletion errors', async () => { | ||
| req.params.id = '123'; | ||
| const errorMessage = 'Delete Error'; | ||
| Blog.findById.mockRejectedValue(new Error(errorMessage)); | ||
|
|
||
| await deleteBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(500); | ||
| expect(res.json).toHaveBeenCalledWith({ message: errorMessage }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
nexus/frontend/src/app/(dashboard)/manufacturing/machines/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding
jest/supertesthere without regenerating the npm lockfiles leaves the clean-install metadata stale:agency/server/package-lock.jsonstill lists onlynodemonfor this package, and the root lock entry foragency/serveralso lacks these new devDependencies. In CI or any workflow that usesnpm ci(the npm help describes it as a clean install from the lockfile), this can fail or install from dependency data that does not matchpackage.json, so the newly addednpm testscript may not be reproducible until the lockfiles are updated with these dependencies.Useful? React with πΒ / π.