Files
pos-system/services/iam-service/src/modules/feature/__tests__/feature.service.test.ts
Ho Ngoc Hai 8cc2f66df6 Update IAM Service with various enhancements and fixes
- Added `xmlchars` dependency to `pnpm-lock.yaml` for improved XML character handling.
- Updated IAM Service audit plan to streamline post-deployment monitoring tasks.
- Enhanced Dockerfile to prune development dependencies after build for a leaner production image.
- Introduced a new encryption key configuration in the environment example for better security practices.
- Refactored multiple service files to improve import organization and maintainability.
- Improved error handling in seed scripts to provide more detailed logging on failures.
- Updated various controllers and services to ensure consistent import statements and enhance readability.

These changes aim to improve the overall functionality, security, and maintainability of the IAM Service.
2026-01-02 16:13:36 +07:00

123 lines
3.8 KiB
TypeScript

import { logger } from '@goodgo/logger';
import { featureRepository } from '../feature.repository';
import { FeatureService } from '../feature.service';
// EN: Mock the logger to avoid console output during tests
// VI: Mock logger để tránh output console trong tests
jest.mock('@goodgo/logger');
// EN: Mock feature repository
// VI: Mock feature repository
jest.mock('../feature.repository', () => ({
featureRepository: {
create: jest.fn(),
findById: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
findAll: jest.fn(),
},
}));
describe('FeatureService', () => {
let featureService: FeatureService;
beforeEach(() => {
// EN: Clear all mocks before each test
// VI: Xóa tất cả mocks trước mỗi test
jest.clearAllMocks();
featureService = new FeatureService();
});
describe('create', () => {
it('should create a feature successfully', async () => {
// EN: Arrange
// VI: Chuẩn bị
const testData = { name: 'test-feature', title: 'Test Feature', description: 'A test feature' };
const mockFeature = {
id: 'test-id',
name: testData.name,
title: testData.title,
description: testData.description,
config: {},
enabled: true,
version: '1.0.0',
tags: [],
createdAt: new Date(),
updatedAt: new Date(),
};
(featureRepository.create as jest.Mock).mockResolvedValue(mockFeature);
// EN: Act
// VI: Thực hiện
const result = await featureService.create(testData);
// EN: Assert
// VI: Kiểm tra
expect(featureRepository.create).toHaveBeenCalledWith(testData);
expect(logger.info).toHaveBeenCalledWith('Creating feature / Tạo feature', { data: testData });
expect(logger.info).toHaveBeenCalledWith('Feature created successfully / Feature đã được tạo thành công', { featureId: mockFeature.id });
expect(result).toEqual(mockFeature);
});
it('should handle minimal data', async () => {
// EN: Arrange
// VI: Chuẩn bị
const minimalData = { name: 'minimal-feature' };
const mockFeature = {
id: 'minimal-id',
name: minimalData.name,
config: {},
enabled: true,
version: '1.0.0',
tags: [],
createdAt: new Date(),
updatedAt: new Date(),
};
(featureRepository.create as jest.Mock).mockResolvedValue(mockFeature);
// EN: Act
// VI: Thực hiện
const result = await featureService.create(minimalData);
// EN: Assert
// VI: Kiểm tra
expect(logger.info).toHaveBeenCalledWith('Creating feature / Tạo feature', { data: minimalData });
expect(result).toEqual(mockFeature);
});
it('should handle complex data structures', async () => {
// EN: Arrange
// VI: Chuẩn bị
const complexData = {
name: 'advanced-feature',
title: 'Advanced Feature',
description: 'Feature with complex data',
config: { enabled: true, priority: 1 },
tags: ['advanced', 'complex']
};
const mockFeature = {
id: 'complex-id',
name: complexData.name,
title: complexData.title,
description: complexData.description,
config: complexData.config,
enabled: true,
version: '1.0.0',
tags: complexData.tags,
createdAt: new Date(),
updatedAt: new Date(),
};
(featureRepository.create as jest.Mock).mockResolvedValue(mockFeature);
// EN: Act
// VI: Thực hiện
const result = await featureService.create(complexData);
// EN: Assert
// VI: Kiểm tra
expect(logger.info).toHaveBeenCalledWith('Creating feature / Tạo feature', { data: complexData });
expect(result).toEqual(mockFeature);
});
});
});