diff --git a/public/app/features/alerting/unified/hooks/useFolder.test.ts b/public/app/features/alerting/unified/hooks/useFolder.test.ts new file mode 100644 index 00000000000..3c480cb464a --- /dev/null +++ b/public/app/features/alerting/unified/hooks/useFolder.test.ts @@ -0,0 +1,27 @@ +import { mockFolder } from '../mocks'; + +import { stringifyFolder } from './useFolder'; + +describe('with slashes', () => { + it('should correctly stringify a folder', () => { + const folder = mockFolder({ title: 'my/folder' }); + expect(stringifyFolder(folder)).toEqual('my\\/folder'); + }); + + it('should correctly stringify a nested folder', () => { + const folder = mockFolder({ title: 'my/folder', parents: [mockFolder({ title: 'parent/slash' })] }); + expect(stringifyFolder(folder)).toEqual('parent\\/slash/my\\/folder'); + }); +}); + +describe('without slashes', () => { + it('should correctly stringify a folder', () => { + const folder = mockFolder({ title: 'my folder' }); + expect(stringifyFolder(folder)).toEqual('my folder'); + }); + + it('should correctly stringify a nested folder', () => { + const folder = mockFolder({ title: 'my folder', parents: [mockFolder({ title: 'my parent' })] }); + expect(stringifyFolder(folder)).toEqual('my parent/my folder'); + }); +}); diff --git a/public/app/features/alerting/unified/hooks/useFolder.ts b/public/app/features/alerting/unified/hooks/useFolder.ts index e4fcd3882f8..8939ae8ad4c 100644 --- a/public/app/features/alerting/unified/hooks/useFolder.ts +++ b/public/app/features/alerting/unified/hooks/useFolder.ts @@ -34,5 +34,11 @@ export function useFolder(uid?: string): ReturnBag { } export function stringifyFolder({ title, parents }: FolderDTO) { - return parents && parents?.length ? [...parents.map((p) => p.title), title].join('/') : title; + return parents && parents?.length + ? [...parents.map((p) => p.title), title].map(encodeTitle).join('/') + : encodeTitle(title); +} + +export function encodeTitle(title: string): string { + return title.replaceAll('/', '\\/'); }