Skip to content

Added API testcases for todo service#5

Open
abbas-dependable-naqvi wants to merge 2 commits intotodo_modulefrom
api_testcase
Open

Added API testcases for todo service#5
abbas-dependable-naqvi wants to merge 2 commits intotodo_modulefrom
api_testcase

Conversation

@abbas-dependable-naqvi
Copy link
Copy Markdown
Owner

Summary

Added API testcases for todo service

@abbas-dependable-naqvi abbas-dependable-naqvi changed the base branch from main to todo_module January 16, 2025 05:49
import { getRepositoryToken } from '@nestjs/typeorm';
import { Todo, TodoState } from './../entities/todo.entity';
import { User } from './../entities/user.entity';
import { Repository } from 'typeorm';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this import to top

userId: 1,
};

(todoRepository.findOne as jest.Mock).mockResolvedValue(mockTodo);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use mockResolvedValueOnce instead of this

{
provide: getRepositoryToken(Todo),
useValue: {
findOne: jest.fn(),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store them in a variable at the top and assign here, so that u can directly use it later

findOne: jest.fn(),
},
},
],
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear all mocks before each run

});
expect(result).toEqual(sanitizedTodo);

expect(todoRepository.findOne).toHaveBeenCalledWith({
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also check how many times its been called

(todoRepository.findOne as jest.Mock).mockResolvedValue(mockTodo);

await expect(service.find(todoID, userID)).rejects.toThrow(
UnauthorizedException,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should return forbidden error instead of unauthorized

(userRepository.findOne as jest.Mock).mockResolvedValue(mockUser);

(todoRepository.save as jest.Mock).mockRejectedValue(
new Error('Database error'),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use typeorm QueryFailedError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants