Folders: Refactor Browse/Dashboards list tests (#109421)

This commit is contained in:
Tom Ratcliffe
2025-08-21 15:46:12 +01:00
committed by GitHub
parent 91748fe115
commit 82d36a259e
9 changed files with 133 additions and 133 deletions
@@ -1,14 +1,19 @@
import { HttpHandler } from 'msw';
import folderHandlers from './api/folders/handlers';
import searchHandlers from './api/search/handlers';
import teamsHandlers from './api/teams/handlers';
import appPlatformDashboardv0alpha1Handlers from './apis/dashboard.grafana.app/v0alpha1/handlers';
import appPlatformFolderv1beta1Handlers from './apis/folder.grafana.app/v1beta1/handlers';
import appPlatformIamv0alpha1Handlers from './apis/iam.grafana.app/v0alpha1/handlers';
const allHandlers: HttpHandler[] = [
// Legacy handlers
...teamsHandlers,
...folderHandlers,
...searchHandlers,
// App platform handlers
...appPlatformDashboardv0alpha1Handlers,
...appPlatformFolderv1beta1Handlers,
...appPlatformIamv0alpha1Handlers,
@@ -1,3 +1,4 @@
import { Chance } from 'chance';
import { HttpResponse, http } from 'msw';
import { treeViewersCanEdit, wellFormedTree } from '../../../fixtures/folders';
@@ -19,7 +20,6 @@ const additionalProperties = {
created: '2025-07-14T12:07:36+02:00',
createdBy: 'Anonymous',
hasAcl: false,
id: 1,
orgId: 1,
updated: '2025-07-15T18:01:36+02:00',
updatedBy: 'Anonymous',
@@ -42,10 +42,11 @@ const listFoldersHandler = () =>
const folders = tree
.filter((v) => v.item.kind === 'folder' && v.item.parentUID === parentUid)
.map((folder) => {
const random = Chance(folder.item.uid);
return {
id: random.integer({ min: 1, max: 1000 }),
uid: folder.item.uid,
title: folder.item.kind === 'folder' ? folder.item.title : "invalid - this shouldn't happen",
...additionalProperties,
};
})
.sort((a, b) => collator.compare(a.title, b.title)) // API always sorts by title
@@ -66,7 +67,10 @@ const getFolderHandler = () =>
return HttpResponse.json({ message: 'folder not found', status: 'not-found' }, { status: 404 });
}
const random = Chance(folder.item.uid);
return HttpResponse.json({
id: random.integer({ min: 1, max: 1000 }),
title: folder?.item.title,
uid: folder?.item.uid,
...additionalProperties,
@@ -0,0 +1,73 @@
import { Chance } from 'chance';
import { HttpResponse, http } from 'msw';
import { wellFormedTree } from '../../../fixtures/folders';
const [mockTree] = wellFormedTree();
type FilterArray = Array<(v: (typeof mockTree)[number]) => boolean>;
const slugify = (str: string) => {
return str
.toLowerCase()
.replace(/[^\w ]+/g, '')
.replace(/ +/g, '-');
};
const getLegacySearchHandler = () =>
http.get('/api/search', ({ request }) => {
const folderFilter = new URL(request.url).searchParams.get('folderUIDs') || null;
const typeFilter = new URL(request.url).searchParams.get('type') || null;
// Workaround for the fixture kind being 'dashboard' instead of 'dash-db'
const mappedTypeFilter = typeFilter === 'dash-db' ? 'dashboard' : typeFilter;
const response = mockTree
.filter((filterItem) => {
const filters: FilterArray = [];
if (folderFilter && folderFilter !== 'general') {
filters.push(
({ item }) => (item.kind === 'folder' || item.kind === 'dashboard') && item.parentUID === folderFilter
);
}
if (folderFilter === 'general') {
filters.push(
({ item }) => (item.kind === 'folder' || item.kind === 'dashboard') && item.parentUID === undefined
);
}
if (mappedTypeFilter) {
filters.push(({ item }) => item.kind === mappedTypeFilter);
}
return filters.every((filterPredicate) => filterPredicate(filterItem));
})
.map(({ item }) => {
const random = Chance(item.uid);
const slugified = slugify(item.title || '');
const parentFolder =
item.kind === 'dashboard'
? mockTree.find((t) => t.item.kind === 'folder' && t.item.uid === item.parentUID)
: undefined;
return {
id: random.integer({ min: 1, max: 1000 }),
uid: item.uid,
orgId: 1,
title: item.title,
uri: `db/${slugified}`,
url: `/d/${item.uid}/${slugified}`,
folderUid: parentFolder?.item.uid,
folderTitle: parentFolder?.item.title,
slug: '',
type: item.kind,
tags: [],
isStarred: false,
sortMeta: 0,
isDeleted: false,
};
});
return HttpResponse.json(response);
});
export default [getLegacySearchHandler()];