Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5dc373f
chore: update dependencies for account module testing
jerrygirmaa Apr 7, 2025
61b2f2e
chore: update tsconfig for test configuration
jerrygirmaa Apr 7, 2025
383d239
feat(account): updated the relative import inaccount.service
jerrygirmaa Apr 7, 2025
db54331
test(account): add comprehensive test suite for account service
jerrygirmaa Apr 7, 2025
5fc5550
test(admin/account): update test cases for account.controller
jerrygirmaa Apr 11, 2025
1be713e
use relative path in the import
jerrygirmaa Apr 11, 2025
ebc8c88
test(admin/account): add service layer test coverage
jerrygirmaa Apr 11, 2025
42cfabb
test(admin/account): add unit tests for AccountIdPipe
jerrygirmaa Apr 11, 2025
fad650a
use relative path for the import
jerrygirmaa Apr 11, 2025
9ba4c96
test: update UserController for testing compatibility
jerrygirmaa Apr 15, 2025
3c152a8
test: update UserService imports
jerrygirmaa Apr 15, 2025
277b784
test: add unit tests for UserController
jerrygirmaa Apr 15, 2025
c936213
remove relative imports for AccountController
jerrygirmaa May 13, 2025
691dd70
remove relative imports
jerrygirmaa May 13, 2025
d4b3de5
remove relative imports
jerrygirmaa May 13, 2025
5ac2917
remove relative imports
jerrygirmaa May 13, 2025
f06e43a
remove relative imports
jerrygirmaa May 13, 2025
d27ffee
chore: add standalone Jest config file
jerrygirmaa May 13, 2025
9be850c
Merge remote-tracking branch 'origin/183-writing-test-case-for-the-ac…
jerrygirmaa May 13, 2025
5ef9a0d
remove relative path
jerrygirmaa May 13, 2025
92d35a6
remove relative path
jerrygirmaa May 13, 2025
3cce3e2
remove relative path
jerrygirmaa May 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// jest.config.ts
import { Config } from 'jest';

const config: Config = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '.',
testRegex: '.spec.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
},
testEnvironment: 'node',
};

export default config;
63 changes: 42 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@nestjs/testing": "^10.4.15",
"@types/bcrypt": "^5.0.2",
"@types/express": "^4.17.17",
"@types/faker": "^6.6.8",
"@types/jest": "^29.5.2",
"@types/jest": "^29.5.14",
"@types/jsonwebtoken": "^9.0.7",
"@types/node": "^20.3.1",
"@types/nodemailer": "^6.4.17",
Expand All @@ -77,11 +77,11 @@
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.5.0",
"jest": "^29.7.0",
"prettier": "^3.4.2",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-jest": "^29.3.1",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
Expand Down
122 changes: 116 additions & 6 deletions src/modules/admin/account/account.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,130 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AccountController } from './account.controller';
import { AccountService } from './account.service';
import { AccountController } from 'src/modules/admin/account/account.controller';
import { AccountService } from 'src/modules/admin/account/account.service';
import { CreateAccountDto } from 'src/modules/admin/account/dto/create-account.dto';
import { UpdateAccountDto } from 'src/modules/admin/account/dto/update-account.dto';
import { PaginationDto } from 'src/common/dto/pagination.dto';
import { AuthGuard } from 'src/modules/auth/guard/auth/auth.guard';
import { PrismaService } from 'src/modules/prisma/prisma.service';

describe('AccountController', () => {
let controller: AccountController;
let accountService: AccountService;

const mockAccountService = {
create: jest.fn(),
findAll: jest.fn(),
findOne: jest.fn(),
update: jest.fn(),
remove: jest.fn(),
};

const mockPrismaService = {
account: {
findFirst: jest.fn(),
},
};

const mockRequest = {
user: { id: 'test-user-id' },
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AccountController],
providers: [AccountService],
}).compile();
providers: [
{
provide: AccountService,
useValue: mockAccountService,
},
{
provide: PrismaService,
useValue: mockPrismaService,
},
{
provide: 'REQUEST',
useValue: mockRequest,
},
],
})
.overrideGuard(AuthGuard)
.useValue({ canActivate: () => true })
.compile();

controller = module.get<AccountController>(AccountController);
accountService = module.get<AccountService>(AccountService);
jest.clearAllMocks();
});

describe('POST /admin/account', () => {
it('should create an account', async () => {
const createDto: CreateAccountDto = {
name: 'Test Account',
domain: null,
};
const expectedResult = { id: 'acc-id', name: 'Test Account' };

mockAccountService.create.mockResolvedValue(expectedResult);

const result = await controller.create(createDto);
expect(result).toEqual(expectedResult);
expect(accountService.create).toHaveBeenCalledWith(createDto);
});
});

describe('GET /admin/account', () => {
it('should return paginated accounts', async () => {
const paginationDto: PaginationDto = { page: 1, limit: 10 };
const expectedResult = {
data: [{ id: 'acc1' }, { id: 'acc2' }],
meta: { total: 2, page: 1, limit: 10 },
};

mockAccountService.findAll.mockResolvedValue(expectedResult);

const result = await controller.findAll(paginationDto);
expect(result).toEqual(expectedResult);
expect(accountService.findAll).toHaveBeenCalledWith(paginationDto);
});
});

it('should be defined', () => {
expect(controller).toBeDefined();
describe('GET /admin/account/:id', () => {
it('should return a single account', async () => {
const accountId = 'acc-id';
const expectedResult = { id: accountId, name: 'Test Account' };

mockAccountService.findOne.mockResolvedValue(expectedResult);

const result = await controller.findOne(accountId);
expect(result).toEqual(expectedResult);
expect(accountService.findOne).toHaveBeenCalledWith(accountId);
});
});

describe('PATCH /admin/account/:id', () => {
it('should update an account', async () => {
const accountId = 'acc-id';
const updateDto: UpdateAccountDto = { name: 'Updated Name' };
const expectedResult = { id: accountId, ...updateDto };

mockAccountService.update.mockResolvedValue(expectedResult);

const result = await controller.update(accountId, updateDto);
expect(result).toEqual(expectedResult);
expect(accountService.update).toHaveBeenCalledWith(accountId, updateDto);
});
});

describe('DELETE /admin/account/:id', () => {
it('should delete an account', async () => {
const accountId = 'acc-id';
const expectedResult = { message: 'Account deleted successfully' };

mockAccountService.remove.mockResolvedValue(expectedResult);

const result = await controller.remove(accountId);
expect(result).toEqual(expectedResult);
expect(accountService.remove).toHaveBeenCalledWith(accountId);
});
});
});
8 changes: 4 additions & 4 deletions src/modules/admin/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
UsePipes,
Query,
} from '@nestjs/common';
import { AccountService } from './account.service';
import { CreateAccountDto } from './dto/create-account.dto';
import { UpdateAccountDto } from './dto/update-account.dto';
import { AccountIdPipe } from './pipe/account-id/account-id.pipe';
import { AccountService } from 'src/modules/admin/account/account.service';
import { CreateAccountDto } from 'src/modules/admin/account/dto/create-account.dto';
import { UpdateAccountDto } from 'src/modules/admin/account/dto/update-account.dto';
import { AccountIdPipe } from 'src/modules/admin/account/pipe/account-id/account-id.pipe';
import { AuthGuard } from 'src/modules/auth/guard/auth/auth.guard';
import { PaginationDto } from 'src/common/dto/pagination.dto';

Expand Down
Loading