2025-01-16 20:18:47 +08:00
|
|
|
import { DashboardV2Spec, defaultDashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0';
|
2024-12-18 05:17:09 +08:00
|
|
|
import { backendSrv } from 'app/core/services/backend_srv';
|
|
|
|
import { AnnoKeyFolder, AnnoKeyFolderId, AnnoKeyFolderTitle, AnnoKeyFolderUrl } from 'app/features/apiserver/types';
|
|
|
|
|
|
|
|
import { DashboardWithAccessInfo } from './types';
|
2025-01-02 19:23:58 +08:00
|
|
|
import { K8sDashboardV2API } from './v2';
|
2024-12-18 05:17:09 +08:00
|
|
|
|
|
|
|
const mockDashboardDto: DashboardWithAccessInfo<DashboardV2Spec> = {
|
|
|
|
kind: 'DashboardWithAccessInfo',
|
|
|
|
apiVersion: 'v0alpha1',
|
|
|
|
|
|
|
|
metadata: {
|
|
|
|
name: 'dash-uid',
|
|
|
|
resourceVersion: '1',
|
|
|
|
creationTimestamp: '1',
|
|
|
|
annotations: {
|
|
|
|
[AnnoKeyFolder]: 'new-folder',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
spec: {
|
|
|
|
...defaultDashboardV2Spec(),
|
|
|
|
},
|
|
|
|
access: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
jest.mock('@grafana/runtime', () => ({
|
2025-01-09 17:51:53 +08:00
|
|
|
...jest.requireActual('@grafana/runtime'),
|
2024-12-18 05:17:09 +08:00
|
|
|
getBackendSrv: () => ({
|
|
|
|
get: () => mockDashboardDto,
|
|
|
|
}),
|
2025-01-09 17:51:53 +08:00
|
|
|
config: {
|
|
|
|
...jest.requireActual('@grafana/runtime').config,
|
|
|
|
},
|
2024-12-18 05:17:09 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('app/features/live/dashboard/dashboardWatcher', () => ({
|
|
|
|
ignoreNextSave: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('v2 dashboard API', () => {
|
|
|
|
it('should provide folder annotations', async () => {
|
|
|
|
jest.spyOn(backendSrv, 'getFolderByUid').mockResolvedValue({
|
|
|
|
id: 1,
|
|
|
|
uid: 'new-folder',
|
|
|
|
title: 'New Folder',
|
|
|
|
url: '/folder/url',
|
|
|
|
canAdmin: true,
|
|
|
|
canDelete: true,
|
|
|
|
canEdit: true,
|
|
|
|
canSave: true,
|
|
|
|
created: '',
|
|
|
|
createdBy: '',
|
|
|
|
hasAcl: false,
|
|
|
|
updated: '',
|
|
|
|
updatedBy: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
const convertToV1 = false;
|
2025-01-02 19:23:58 +08:00
|
|
|
const api = new K8sDashboardV2API(convertToV1);
|
2024-12-18 05:17:09 +08:00
|
|
|
// because the API can currently return both DashboardDTO and DashboardWithAccessInfo<DashboardV2Spec> based on the
|
|
|
|
// parameter convertToV1, we need to cast the result to DashboardWithAccessInfo<DashboardV2Spec> to be able to
|
|
|
|
// access
|
|
|
|
const result = (await api.getDashboardDTO('test')) as DashboardWithAccessInfo<DashboardV2Spec>;
|
|
|
|
expect(result.metadata.annotations![AnnoKeyFolderId]).toBe(1);
|
|
|
|
expect(result.metadata.annotations![AnnoKeyFolderTitle]).toBe('New Folder');
|
|
|
|
expect(result.metadata.annotations![AnnoKeyFolderUrl]).toBe('/folder/url');
|
|
|
|
expect(result.metadata.annotations![AnnoKeyFolder]).toBe('new-folder');
|
|
|
|
});
|
|
|
|
});
|