-
-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ add unit tests for agency/server blog controller #15
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
5
commits into
main
Choose a base branch
from
test/agency-server-blog-controller-6964327158673213702
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
5 commits
Select commit
Hold shift + click to select a range
14836b8
test: add unit tests for agency/server blog controller
google-labs-jules[bot] ca754ac
test: add unit tests for agency/server blog controller
google-labs-jules[bot] 3a25a08
test: add unit tests for agency/server blog controller
google-labs-jules[bot] 352086d
test: add unit tests for agency/server blog controller
google-labs-jules[bot] 344ad08
test: add unit tests for agency/server blog controller
google-labs-jules[bot] 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
243 changes: 243 additions & 0 deletions
243
agency/server/controllers/__tests__/blogController.test.js
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,243 @@ | ||
| const { | ||
| getBlogs, | ||
| getBlogById, | ||
| createBlog, | ||
| updateBlog, | ||
| deleteBlog, | ||
| } = require('../blogController'); | ||
|
|
||
| const Blog = require('../../models/Blog'); | ||
|
|
||
| jest.mock('../../models/Blog'); | ||
|
|
||
| describe('Blog Controller Tests', () => { | ||
| let req, res; | ||
|
|
||
| beforeEach(() => { | ||
| req = { | ||
| body: {}, | ||
| params: {}, | ||
| user: {} | ||
| }; | ||
|
|
||
| res = { | ||
| json: jest.fn(), | ||
| status: jest.fn().mockReturnThis(), | ||
| }; | ||
|
|
||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getBlogs', () => { | ||
| it('should retrieve published blogs for a regular user', async () => { | ||
| const blogs = [{ title: 'Published Blog', status: 'published' }]; | ||
| const mockSort = jest.fn().mockResolvedValue(blogs); | ||
| Blog.find.mockReturnValue({ sort: mockSort }); | ||
|
|
||
| await getBlogs(req, res); | ||
|
|
||
| expect(Blog.find).toHaveBeenCalledWith({ status: 'published' }); | ||
| expect(mockSort).toHaveBeenCalledWith({ createdAt: -1 }); | ||
| expect(res.json).toHaveBeenCalledWith(blogs); | ||
| }); | ||
|
|
||
| it('should retrieve all blogs for an admin', async () => { | ||
| req.user = { isAdmin: true }; | ||
| const blogs = [{ title: 'Published Blog' }, { title: 'Draft Blog' }]; | ||
| const mockSort = jest.fn().mockResolvedValue(blogs); | ||
| Blog.find.mockReturnValue({ sort: mockSort }); | ||
|
|
||
| await getBlogs(req, res); | ||
|
|
||
| expect(Blog.find).toHaveBeenCalledWith({}); | ||
| expect(mockSort).toHaveBeenCalledWith({ createdAt: -1 }); | ||
| expect(res.json).toHaveBeenCalledWith(blogs); | ||
| }); | ||
|
|
||
| it('should handle 500 error', async () => { | ||
| const error = new Error('Database error'); | ||
| Blog.find.mockImplementation(() => { throw error; }); | ||
|
|
||
| await getBlogs(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(500); | ||
| expect(res.json).toHaveBeenCalledWith({ message: error.message }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getBlogById', () => { | ||
| it('should find blog by slug', async () => { | ||
| req.params.id = 'my-slug'; | ||
| const blog = { title: 'Slug Blog', slug: 'my-slug', status: 'published' }; | ||
| Blog.findOne.mockResolvedValue(blog); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(Blog.findOne).toHaveBeenCalledWith({ slug: 'my-slug' }); | ||
| expect(res.json).toHaveBeenCalledWith(blog); | ||
| }); | ||
|
|
||
| it('should find blog by ID if slug not found and ID matches regex', async () => { | ||
| req.params.id = '507f1f77bcf86cd799439011'; | ||
| const blog = { title: 'ID Blog', _id: '507f1f77bcf86cd799439011', status: 'published' }; | ||
| Blog.findOne.mockResolvedValue(null); | ||
| Blog.findById.mockResolvedValue(blog); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(Blog.findOne).toHaveBeenCalledWith({ slug: req.params.id }); | ||
| expect(Blog.findById).toHaveBeenCalledWith(req.params.id); | ||
| expect(res.json).toHaveBeenCalledWith(blog); | ||
| }); | ||
|
|
||
| it('should return 404 when blog 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 500 error', async () => { | ||
| req.params.id = 'some-id'; | ||
| const error = new Error('Find error'); | ||
| Blog.findOne.mockRejectedValue(error); | ||
|
|
||
| await getBlogById(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(500); | ||
| expect(res.json).toHaveBeenCalledWith({ message: error.message }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createBlog', () => { | ||
| it('should create a blog successfully', async () => { | ||
| req.body = { | ||
| title: 'New Blog', | ||
| author: 'John Doe', | ||
| category: 'Tech', | ||
| status: 'published' | ||
| }; | ||
|
|
||
| const createdBlog = { ...req.body, _id: 'some-id' }; | ||
|
|
||
| // Mock the constructor and save | ||
| Blog.mockImplementation(() => ({ | ||
| save: jest.fn().mockResolvedValue(createdBlog) | ||
| })); | ||
|
|
||
| await createBlog(req, res); | ||
|
|
||
| expect(Blog).toHaveBeenCalledWith({ | ||
| title: 'New Blog', | ||
| author: 'John Doe', | ||
| category: 'Tech', | ||
| image: undefined, | ||
| excerpt: undefined, | ||
| content: undefined, | ||
| readTime: undefined, | ||
| tags: undefined, | ||
| slug: undefined, | ||
| status: 'published' | ||
| }); | ||
| expect(res.status).toHaveBeenCalledWith(201); | ||
| expect(res.json).toHaveBeenCalledWith(createdBlog); | ||
| }); | ||
|
|
||
| it('should handle 400 error on save', async () => { | ||
| const error = new Error('Validation failed'); | ||
| Blog.mockImplementation(() => ({ | ||
| save: jest.fn().mockRejectedValue(error) | ||
| })); | ||
|
|
||
| await createBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(400); | ||
| expect(res.json).toHaveBeenCalledWith({ message: error.message }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateBlog', () => { | ||
| it('should update a blog successfully', async () => { | ||
| req.params.id = 'some-id'; | ||
| req.body = { title: 'Updated Title' }; | ||
|
|
||
| const mockBlog = { | ||
| title: 'Old Title', | ||
| save: jest.fn().mockImplementation(function() { | ||
| return Promise.resolve(this); | ||
| }) | ||
| }; | ||
|
|
||
| Blog.findById.mockResolvedValue(mockBlog); | ||
|
|
||
| await updateBlog(req, res); | ||
|
|
||
| expect(Blog.findById).toHaveBeenCalledWith('some-id'); | ||
| expect(mockBlog.title).toBe('Updated Title'); | ||
| expect(mockBlog.save).toHaveBeenCalled(); | ||
| expect(res.json).toHaveBeenCalledWith(mockBlog); | ||
| }); | ||
|
|
||
| it('should return 404 when blog 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 400 error', async () => { | ||
| req.params.id = 'some-id'; | ||
| const error = new Error('Update error'); | ||
| Blog.findById.mockRejectedValue(error); | ||
|
|
||
| await updateBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(400); | ||
| expect(res.json).toHaveBeenCalledWith({ message: error.message }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('deleteBlog', () => { | ||
| it('should delete a blog successfully', async () => { | ||
| req.params.id = 'some-id'; | ||
| const mockBlog = { | ||
| deleteOne: jest.fn().mockResolvedValue({}) | ||
| }; | ||
|
|
||
| Blog.findById.mockResolvedValue(mockBlog); | ||
|
|
||
| await deleteBlog(req, res); | ||
|
|
||
| expect(Blog.findById).toHaveBeenCalledWith('some-id'); | ||
| expect(mockBlog.deleteOne).toHaveBeenCalled(); | ||
| expect(res.json).toHaveBeenCalledWith({ message: 'Blog post removed' }); | ||
| }); | ||
|
|
||
| it('should return 404 when blog 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 500 error', async () => { | ||
| req.params.id = 'some-id'; | ||
| const error = new Error('Delete error'); | ||
| Blog.findById.mockRejectedValue(error); | ||
|
|
||
| await deleteBlog(req, res); | ||
|
|
||
| expect(res.status).toHaveBeenCalledWith(500); | ||
| expect(res.json).toHaveBeenCalledWith({ message: error.message }); | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.