2025-08-01 20:34:33 +08:00
|
|
|
import { ReactNode } from 'react';
|
|
|
|
import { act, getWrapper, renderHook, waitFor } from 'test/test-utils';
|
2025-07-03 22:15:23 +08:00
|
|
|
|
|
|
|
import * as runtime from '@grafana/runtime';
|
2025-08-01 20:34:33 +08:00
|
|
|
import { setupMockServer } from '@grafana/test-utils/server';
|
|
|
|
import { getFolderFixtures } from '@grafana/test-utils/unstable';
|
|
|
|
import { backendSrv } from 'app/core/services/backend_srv';
|
2025-08-29 22:43:48 +08:00
|
|
|
import { ManagerKind } from 'app/features/apiserver/types';
|
2025-07-03 22:15:23 +08:00
|
|
|
|
|
|
|
import { DashboardViewItem } from '../../../features/search/types';
|
|
|
|
|
|
|
|
import { useFoldersQuery } from './useFoldersQuery';
|
2025-08-29 22:43:48 +08:00
|
|
|
import { getCustomRootFolderItem, getRootFolderItem } from './utils';
|
2025-07-03 22:15:23 +08:00
|
|
|
|
2025-08-01 20:34:33 +08:00
|
|
|
const [_, { folderA, folderB, folderC }] = getFolderFixtures();
|
2025-07-03 22:15:23 +08:00
|
|
|
|
2025-08-01 20:34:33 +08:00
|
|
|
runtime.setBackendSrv(backendSrv);
|
|
|
|
setupMockServer();
|
2025-07-03 22:15:23 +08:00
|
|
|
|
2025-08-01 20:34:33 +08:00
|
|
|
const wrapper = ({ children }: { children: ReactNode }) => {
|
|
|
|
const ProviderWrapper = getWrapper({ renderWithRouter: true });
|
|
|
|
return <ProviderWrapper>{children}</ProviderWrapper>;
|
2025-07-03 22:15:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('useFoldersQuery', () => {
|
2025-08-14 20:57:58 +08:00
|
|
|
let configBackup: runtime.GrafanaBootConfig;
|
2025-07-03 22:15:23 +08:00
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
configBackup = { ...runtime.config };
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
runtime.config.featureToggles = configBackup.featureToggles;
|
|
|
|
});
|
|
|
|
|
2025-08-01 20:34:33 +08:00
|
|
|
describe.each([
|
|
|
|
// foldersAppPlatformAPI enabled
|
|
|
|
true,
|
|
|
|
// foldersAppPlatformAPI disabled
|
|
|
|
false,
|
|
|
|
])('foldersAppPlatformAPI feature toggle set to %s', (featureToggleState) => {
|
|
|
|
it('returns data using legacy api', async () => {
|
|
|
|
runtime.config.featureToggles.foldersAppPlatformAPI = featureToggleState;
|
|
|
|
const [_dashboardsContainer, ...items] = await testFn();
|
|
|
|
|
|
|
|
const sortedItemTitles = items.map((item) => (item.item as DashboardViewItem).title).sort();
|
|
|
|
const expectedTitles = [folderA.item.title, folderB.item.title, folderC.item.title].sort();
|
|
|
|
|
|
|
|
expect(sortedItemTitles).toEqual(expectedTitles);
|
|
|
|
});
|
2025-08-29 22:43:48 +08:00
|
|
|
|
|
|
|
it('uses custom root folder display name when rootFolderItem is provided', async () => {
|
|
|
|
runtime.config.featureToggles.foldersAppPlatformAPI = featureToggleState;
|
|
|
|
const { result } = renderHook(
|
|
|
|
() =>
|
|
|
|
useFoldersQuery({
|
|
|
|
isBrowsing: true,
|
|
|
|
openFolders: {},
|
|
|
|
rootFolderItem: getCustomRootFolderItem({
|
|
|
|
title: 'Test Repo',
|
|
|
|
managedBy: ManagerKind.Repo,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
{ wrapper }
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test that root folder item uses the custom display name
|
|
|
|
expect(result.current.items[0]).toEqual(
|
|
|
|
getCustomRootFolderItem({
|
|
|
|
title: 'Test Repo',
|
|
|
|
managedBy: ManagerKind.Repo,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2025-07-03 22:15:23 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2025-08-01 20:34:33 +08:00
|
|
|
async function testFn() {
|
2025-08-29 22:43:48 +08:00
|
|
|
const { result } = renderHook(
|
|
|
|
() =>
|
|
|
|
useFoldersQuery({
|
|
|
|
isBrowsing: true,
|
|
|
|
openFolders: {},
|
|
|
|
}),
|
|
|
|
{ wrapper }
|
|
|
|
);
|
2025-07-03 22:15:23 +08:00
|
|
|
|
2025-08-01 20:34:33 +08:00
|
|
|
expect(result.current.items[0]).toEqual(getRootFolderItem());
|
2025-07-03 22:15:23 +08:00
|
|
|
expect(result.current.isLoading).toBe(false);
|
2025-08-01 20:34:33 +08:00
|
|
|
|
2025-07-03 22:15:23 +08:00
|
|
|
act(() => {
|
|
|
|
result.current.requestNextPage(undefined);
|
|
|
|
});
|
|
|
|
|
2025-08-01 20:34:33 +08:00
|
|
|
expect(result.current.isLoading).toBe(true);
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
const withoutPaginationPlaceholders = result.current.items.filter((item) => item.item.kind !== 'ui');
|
|
|
|
return expect(withoutPaginationPlaceholders.length).toBeGreaterThan(1);
|
|
|
|
});
|
|
|
|
|
2025-07-03 22:15:23 +08:00
|
|
|
return result.current.items;
|
|
|
|
}
|