Skip to content

Commit b60748a

Browse files
committed
feat: Update tests for MixDatabaseRestPortalService and related services, enhance error handling and response structure
1 parent aa42576 commit b60748a

8 files changed

+47
-12
lines changed

packages/database/tests/mix-database-rest-portal-service.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ describe('MixDatabaseRestPortalService', () => {
1414
expect(dbService).toBeInstanceOf(MixDatabaseRestPortalService);
1515
});
1616

17-
it('should throw if no id provided', async () => {
18-
await expect(dbService.migrate({} as any)).rejects.toThrow('Missing id for migration');
17+
it('should return error if no id provided', async () => {
18+
const result = await dbService.migrate({} as any);
19+
expect(result).toEqual({
20+
errors: ['Missing id for migration'],
21+
isSucceed: false
22+
});
1923
});
2024

2125
it('should call migrate', async () => {

packages/database/tests/related-attribute-data-rest-portal-service.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RelatedAttributeDataRestPortalService } from './related-attribute-data-rest-portal-service';
1+
import { RelatedAttributeDataRestPortalService } from '../src/related-attribute-data-rest-portal-service';
2+
import { ApiService } from '@mixcore/api';
23

34
describe('RelatedAttributeDataRestPortalService', () => {
45
it('should instantiate with correct endpoint', () => {

packages/database/tests/related-attribute-set-rest-portal-service.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { RelatedAttributeSetRestPortalService } from './related-attribute-set-rest-portal-service';
1+
import { RelatedAttributeSetRestPortalService } from '../src/related-attribute-set-rest-portal-service';
2+
import { ApiService } from '@mixcore/api';
23

34
describe('RelatedAttributeSetRestPortalService', () => {
45
it('should instantiate with correct endpoint', () => {
5-
const service = new RelatedAttributeSetRestPortalService();
6+
const api = new ApiService({ apiBaseUrl: 'http://localhost/api/' });
7+
const service = new RelatedAttributeSetRestPortalService(api);
68
// @ts-expect-error endpoint is protected/private in base, this is a placeholder for real test
79
expect(service.endpoint).toBe('related-mix-database/portal');
810
});

packages/template/src/module-article-services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class ModuleArticleService {
2626
* @param params - Optional query params
2727
*/
2828
async fetchArticles(moduleId: string, params?: Record<string, any>): Promise<any[]> {
29-
const url = new URL('/api/v2/rest/mixcore/module-article/get-module-article', this.config.apiBaseUrl);
29+
const url = new URL('/api/v2/rest/mix-portal/module-article', this.config.apiBaseUrl);
3030
url.searchParams.append('moduleId', String(moduleId));
3131
if (params) {
3232
Object.entries(params).forEach(([k, v]) => url.searchParams.append(k, String(v)));

packages/template/src/module-gallery-services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class ModuleGalleryService {
2626
* @param params - Optional query params
2727
*/
2828
async fetchGalleryItems(moduleId: string, params?: Record<string, any>): Promise<any[]> {
29-
const url = new URL('/api/v2/rest/mixcore/module-gallery/get-module-gallery', this.config.apiBaseUrl);
29+
const url = new URL('/api/v2/rest/mix-portal/module-gallery', this.config.apiBaseUrl);
3030
url.searchParams.append('moduleId', String(moduleId));
3131
if (params) {
3232
Object.entries(params).forEach(([k, v]) => url.searchParams.append(k, String(v)));

packages/template/tests/module-article-services.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { ModuleArticleService } from './module-article-services';
1+
import { ModuleArticleService } from '../src/module-article-services';
2+
3+
// Mock fetch implementation
4+
const mockFetch = jest.fn();
5+
global.fetch = mockFetch;
26

37
const config = {
48
apiBaseUrl: 'https://mixcore.net',
@@ -9,8 +13,16 @@ describe('ModuleArticleService', () => {
913
const service = new ModuleArticleService(config);
1014

1115
it('fetchArticles should return an array', async () => {
16+
mockFetch.mockResolvedValueOnce({
17+
ok: true,
18+
json: async () => ([{ id: 1, title: 'test-article' }])
19+
});
1220
const articles = await service.fetchArticles('1');
13-
expect(Array.isArray(articles)).toBe(true);
21+
expect(articles).toEqual([{ id: 1, title: 'test-article' }]);
22+
expect(mockFetch).toHaveBeenCalledWith(
23+
'https://mixcore.net/api/v2/rest/mix-portal/module-article?moduleId=1',
24+
{ headers: undefined }
25+
);
1426
});
1527

1628
// Note: createArticle and deleteArticle require valid credentials and real data

packages/template/tests/module-gallery-services.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { ModuleGalleryService } from './module-gallery-services';
1+
import { ModuleGalleryService } from '../src/module-gallery-services';
2+
3+
// Mock fetch implementation
4+
const mockFetch = jest.fn();
5+
global.fetch = mockFetch;
26

37
const config = {
48
apiBaseUrl: 'https://mixcore.net',
@@ -9,8 +13,16 @@ describe('ModuleGalleryService', () => {
913
const service = new ModuleGalleryService(config);
1014

1115
it('fetchGalleryItems should return an array', async () => {
16+
mockFetch.mockResolvedValueOnce({
17+
ok: true,
18+
json: async () => ([{ id: 1, name: 'test-item' }])
19+
});
1220
const items = await service.fetchGalleryItems('1');
13-
expect(Array.isArray(items)).toBe(true);
21+
expect(items).toEqual([{ id: 1, name: 'test-item' }]);
22+
expect(mockFetch).toHaveBeenCalledWith(
23+
'https://mixcore.net/api/v2/rest/mix-portal/module-gallery?moduleId=1',
24+
{ headers: undefined }
25+
);
1426
});
1527

1628
// Note: uploadGalleryItem and deleteGalleryItem require valid credentials and real data

packages/template/tests/template-services.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ describe('TemplateService', () => {
1717
it('should call copy', async () => {
1818
globalThis.fetch = jest.fn().mockResolvedValue({ ok: true, json: async () => ({ id: 'copy' }) });
1919
const result = await templateService.copy('123');
20-
expect(result).toEqual({ id: 'copy' });
20+
expect(result).toEqual({
21+
data: { id: 'copy' },
22+
isSucceed: true,
23+
status: undefined
24+
});
2125
});
2226
});

0 commit comments

Comments
 (0)