-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XIVY-15526 adds test for badge-tooltip
- Loading branch information
Showing
4 changed files
with
132 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { describe } from 'vitest'; | ||
import type { ContentObject } from '@axonivy/form-editor-protocol'; | ||
import { findCmsEntries } from './CmsTooltip'; | ||
|
||
describe('test tooltips', () => { | ||
test('test cms entry search', async () => { | ||
const cmsTree: Array<ContentObject> = [ | ||
{ | ||
name: 'form-test-project', | ||
fullPath: '/', | ||
type: 'FOLDER', | ||
values: {}, | ||
children: [ | ||
{ | ||
name: 'greetings', | ||
fullPath: '/greetings', | ||
type: 'FOLDER', | ||
values: {}, | ||
children: [ | ||
{ | ||
name: 'hello', | ||
fullPath: '/greetings/hello', | ||
type: 'STRING', | ||
values: { en: 'Hello!' }, | ||
children: [] | ||
}, | ||
{ | ||
name: 'hello', | ||
fullPath: '/greetings/longEntry', | ||
type: 'STRING', | ||
values: { | ||
en: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent aliquam' | ||
}, | ||
children: [] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
]; | ||
|
||
const cmsValidPath = '/greetings/hello'; | ||
const cmsInvalidPath = '/notfound'; | ||
const cmsLongPath = '/greetings/longEntry'; | ||
const cmsValidEntries = findCmsEntries(cmsValidPath, cmsTree); | ||
const cmsInvalidEntries = findCmsEntries(cmsInvalidPath, cmsTree); | ||
const cmsLongEntries = findCmsEntries(cmsLongPath, cmsTree); | ||
|
||
expect(cmsValidEntries).toContain('en: Hello!'); | ||
expect(cmsInvalidEntries).toContain('no cms entry found'); | ||
expect(cmsLongEntries).toContain('en: Lorem ipsum dolor sit amet, consectetur ...'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { ContentObject } from '@axonivy/form-editor-protocol'; | ||
import { useAppContext } from '../context/AppContext'; | ||
import { useMeta } from '../context/useMeta'; | ||
|
||
export const CmsTooltip = ({ text }: { text: string }) => { | ||
const { context } = useAppContext(); | ||
const cmsItems = useMeta('meta/cms/cmsTree', { context, requiredProjects: false }, []).data; | ||
|
||
text = text.replaceAll(/#{ivy.cms.co\(|['"]|\)}/g, ''); | ||
const entries = findCmsEntries(text, cmsItems); | ||
entries.unshift(text); | ||
|
||
return entries.map((entry, i) => ( | ||
<span key={i}> | ||
{entry} | ||
<br /> | ||
</span> | ||
)); | ||
}; | ||
|
||
export const findCmsEntries = (targetPath: string, cmsTree: Array<ContentObject>) => { | ||
const notFound = ['no cms entry found']; | ||
if (cmsTree.length === 0) { | ||
return notFound; | ||
} | ||
const entries = findDeep(targetPath, cmsTree[0]); | ||
if (!entries) { | ||
return notFound; | ||
} | ||
return Object.entries(entries).map(entry => { | ||
let value = entry[1]; | ||
if (value.length > 40) { | ||
value = value.substring(0, 40) + '...'; | ||
} | ||
return `${entry[0]}: ${value}`; | ||
}); | ||
}; | ||
|
||
const findDeep = (targetPath: string, obj?: ContentObject) => { | ||
if (!obj) return null; | ||
if (obj.fullPath === targetPath) return obj.values; | ||
if (!obj.children || obj.children.length === 0) return null; | ||
return findDeep( | ||
targetPath, | ||
obj.children.find(e => targetPath.startsWith(e.fullPath)) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters