| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import { TestBed } from '@angular/core/testing';
- import { provideHttpClient } from '@angular/common/http';
- import { provideHttpClientTesting, HttpTestingController } from '@angular/common/http/testing';
- import { ConfigMetaService } from './config-meta.service';
- import { ConfigService } from 'base-core';
-
- // Mock ConfigService
- class MockConfigService {
- apiBaseUrl = 'http://localhost:8080';
- useMockData = true; // 使用模拟数据以便测试
- }
-
- describe('ConfigMetaService', () => {
- let service: ConfigMetaService;
- let httpTestingController: HttpTestingController;
- let mockConfigService: MockConfigService;
-
- beforeEach(() => {
- mockConfigService = new MockConfigService();
-
- TestBed.configureTestingModule({
- providers: [
- provideHttpClient(),
- provideHttpClientTesting(),
- ConfigMetaService,
- { provide: ConfigService, useValue: mockConfigService }
- ]
- });
-
- service = TestBed.inject(ConfigMetaService);
- httpTestingController = TestBed.inject(HttpTestingController);
- });
-
- afterEach(() => {
- httpTestingController.verify();
- });
-
- it('should be created', () => {
- expect(service).toBeTruthy();
- });
-
- describe('getListUrl', () => {
- it('should return correct URL when useMockData is true', () => {
- mockConfigService.useMockData = true;
- expect(service.getListUrl()).toBe('http://localhost:8080/tabulator/config/meta/list');
- });
-
- it('should return correct URL when useMockData is false', () => {
- mockConfigService.useMockData = false;
- expect(service.getListUrl()).toBe('http://localhost:8080/tabulator/config/meta/list');
- });
- });
-
- describe('listConfigMeta', () => {
- it('should return mock data when useMockData is true', (done) => {
- mockConfigService.useMockData = true;
-
- service.listConfigMeta().subscribe({
- next: (data) => {
- expect(data).toBeTruthy();
- expect(Array.isArray(data)).toBeTrue();
- // 模拟数据应该包含一些记录
- expect(data.length).toBeGreaterThan(0);
- done();
- },
- error: done.fail
- });
- });
-
- it('should make HTTP request when useMockData is false', () => {
- mockConfigService.useMockData = false;
- const mockResponse = {
- success: true,
- data: [
- {
- id: '1',
- configName: 'test',
- fieldName: 'testField',
- fieldType: 'string',
- yamlName: 'testYaml',
- fieldDesc: 'test description',
- creator: 'admin',
- createdAt: '2024-01-01T00:00:00Z'
- }
- ]
- };
-
- service.listConfigMeta().subscribe(response => {
- expect(response).toEqual(mockResponse.data);
- });
-
- const req = httpTestingController.expectOne('http://localhost:8080/tabulator/config/meta/list');
- expect(req.request.method).toBe('POST');
- req.flush(mockResponse);
- });
- });
-
- describe('listForTabulator', () => {
- it('should convert Tabulator params and return expected format', (done) => {
- mockConfigService.useMockData = true;
- const tabulatorParams = {
- page: 1,
- size: 20,
- sort: [{ field: 'configName', dir: 'asc' }],
- filter: [{ field: 'fieldType', type: '=', value: 'string' }]
- };
-
- service.listForTabulator(tabulatorParams).subscribe({
- next: (response) => {
- expect(response).toBeTruthy();
- expect(response.last_page).toBeDefined();
- expect(response.data).toBeDefined();
- expect(Array.isArray(response.data)).toBeTrue();
- done();
- },
- error: done.fail
- });
- });
-
- it('should handle empty params', (done) => {
- mockConfigService.useMockData = true;
- const emptyParams = {};
-
- service.listForTabulator(emptyParams).subscribe({
- next: (response) => {
- expect(response).toBeTruthy();
- expect(response.last_page).toBeDefined();
- expect(response.data).toBeDefined();
- expect(Array.isArray(response.data)).toBeTrue();
- done();
- },
- error: done.fail
- });
- });
- });
-
- describe('initConfigMeta', () => {
- it('should return mock success when useMockData is true', (done) => {
- mockConfigService.useMockData = true;
-
- service.initConfigMeta().subscribe({
- next: (result) => {
- expect(result).toBeTruthy();
- expect(result.success).toBeTrue();
- expect(result.data).toBe('模拟初始化成功');
- done();
- },
- error: done.fail
- });
- });
-
- it('should make HTTP POST request when useMockData is false', () => {
- mockConfigService.useMockData = false;
- const mockResponse = { success: true, data: '初始化成功' };
-
- service.initConfigMeta().subscribe(response => {
- expect(response).toEqual(mockResponse);
- });
-
- const req = httpTestingController.expectOne('http://localhost:8080/init/config/meta');
- expect(req.request.method).toBe('POST');
- req.flush(mockResponse);
- });
- });
- });
|