Skip to content

Commit 9c1c8b6

Browse files
committed
unit Test for helper
1 parent f5de259 commit 9c1c8b6

File tree

2 files changed

+106
-24
lines changed

2 files changed

+106
-24
lines changed

packages/sn-pickers-react/src/components/picker/picker-helper.tsx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@ export const PickerHelper = ({
4444

4545
let isContextPathInTree = false
4646

47-
console.log({
48-
selectionRoots,
49-
contextPath,
50-
})
51-
5247
for (const root of selectionRoots || []) {
5348
if (PathHelper.isInSubTree(root, contextPath!)) {
5449
isContextPathInTree = true
5550
}
5651

52+
if (!repository.load) {
53+
continue
54+
}
55+
5756
const promise = repository?.load<GenericContent>({
5857
idOrPath: root,
5958
oDataOptions: {
@@ -64,20 +63,24 @@ export const PickerHelper = ({
6463
SelectionRootQueries.push(promise)
6564
}
6665

67-
const promiseResult = await Promise.allSettled(SelectionRootQueries)
68-
69-
const fulfilledResults: TReferemceSelectionHelperPath[] = promiseResult
70-
.filter((result) => result.status === 'fulfilled' && result.value?.d.Path !== contextPath)
71-
.map(
72-
(result) =>
73-
(result as PromiseFulfilledResult<ODataResponse<GenericContent>>).value.d as TReferemceSelectionHelperPath,
74-
)
75-
76-
console.log(fulfilledResults)
77-
78-
setHelperPaths(fulfilledResults)
79-
setIsLoading(false)
80-
setIsAncestorOfRoot(isContextPathInTree)
66+
try {
67+
const promiseResult = await Promise.allSettled(SelectionRootQueries)
68+
69+
const fulfilledResults: TReferemceSelectionHelperPath[] = promiseResult
70+
.filter((result) => result.status === 'fulfilled' && result.value?.d.Path !== contextPath)
71+
.map(
72+
(result) =>
73+
(result as PromiseFulfilledResult<ODataResponse<GenericContent>>).value
74+
.d as TReferemceSelectionHelperPath,
75+
)
76+
77+
setHelperPaths(fulfilledResults)
78+
console.log(fulfilledResults)
79+
setIsLoading(false)
80+
setIsAncestorOfRoot(isContextPathInTree)
81+
} catch (e) {
82+
console.error(e)
83+
}
8184
}
8285

8386
getReferencePickerHelperData()
@@ -113,6 +116,7 @@ export const PickerHelper = ({
113116
return (
114117
<Link
115118
key={path.Path}
119+
data-test={`path-helper-${path.Path}`}
116120
variant="body2"
117121
onClick={() => handleJumpToCurrentPath(path.Path)}
118122
className={styles}>

packages/sn-pickers-react/tests/picker.test.tsx

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,14 @@ describe('Picker component', () => {
459459
})
460460
})
461461

462-
it('should render helper Items is defined', async () => {
463-
const helperItems = [{ Id: 1, Name: 'Item1', DisplayName: 'Display Item1', Path: '/Root/Content/EN/Blog/Posts' }]
462+
it('should render helper Current Conten based on SelectionRoot', async () => {
463+
let helperItems = { Id: 1, Name: 'Item1', DisplayName: 'Display Item1', Path: '/Root/Content/EN/Blog/Posts' }
464464

465465
let wrapper: any
466466
await act(async () => {
467467
wrapper = mount(
468468
<Picker
469-
selectionRoots={['/Root/Content/EN/Blog/Posts', '/Root/Content/img']}
469+
selectionRoots={['/Root/Content/EN/Blog/Posts']}
470470
contextPath="/Root/Content/EN/Blog/Posts"
471471
repository={repository(genericContentItems, helperItems) as any}
472472
/>,
@@ -477,11 +477,89 @@ describe('Picker component', () => {
477477

478478
//data-test current-content should be rendered
479479

480-
expect(wrapper.find("[data-test='current-content']").exists()).toBeTruthy()
480+
expect(
481+
wrapper
482+
.find(Link)
483+
.filterWhere((n) => n.prop('data-test') === 'current-content')
484+
.exists(),
485+
).toBeTruthy()
486+
487+
helperItems = { Id: 1, Name: 'Item1', DisplayName: 'Display Item1', Path: '/Root/Content/HU/Blog/Posts' }
488+
489+
await act(async () => {
490+
wrapper = mount(
491+
<Picker
492+
selectionRoots={['/Root/Content/HU/Blog/Posts']}
493+
contextPath="/Root/Content/EN/Blog/Posts"
494+
repository={repository(genericContentItems, helperItems) as any}
495+
/>,
496+
)
497+
})
498+
499+
wrapper.update()
500+
501+
expect(
502+
wrapper
503+
.find(Link)
504+
.filterWhere((n) => n.prop('data-test') === 'current-content')
505+
.exists(),
506+
).toBeFalsy()
507+
})
508+
509+
it('Should render Selection Roots', async () => {
510+
let wrapper: any
481511

482512
// helper items should be rendered
483513

484-
expect(wrapper.find("[data-test='path-helpers']").exists()).toBeTruthy()
514+
const helperItems = {
515+
'/Root/Content/EN/Blog/Posts': {
516+
Id: 1,
517+
Name: 'Item1',
518+
DisplayName: 'Display Item1',
519+
Path: '/Root/Content/EN/Blog/Posts',
520+
},
521+
'/Root/Content/HU/Blog/Posts': {
522+
Id: 1,
523+
Name: 'Item1',
524+
DisplayName: 'Display Item1',
525+
Path: '/Root/Content/HU/Blog/Posts',
526+
},
527+
}
528+
const repositoryHandle = (loadCollectionValue?: unknown) => {
529+
return {
530+
loadCollection: () => {
531+
return {
532+
d: {
533+
results: loadCollectionValue,
534+
},
535+
}
536+
},
537+
load: (item) => {
538+
return {
539+
d: helperItems[item.idOrPath],
540+
}
541+
},
542+
}
543+
}
544+
545+
await act(async () => {
546+
wrapper = mount(
547+
<Picker
548+
selectionRoots={['/Root/Content/HU/Blog/Posts']}
549+
contextPath="/Root/Content/EN/Blog/Posts"
550+
repository={repositoryHandle(genericContentItems) as any}
551+
/>,
552+
)
553+
})
554+
555+
wrapper.update()
556+
557+
expect(
558+
wrapper
559+
.find(Link)
560+
.filterWhere((n) => n.prop('data-test') === `path-helper-${helperItems['/Root/Content/HU/Blog/Posts'].Path}`)
561+
.exists(),
562+
).toBeTruthy()
485563

486564
//find helper items length of Link inside data-test path-helpers
487565
})

0 commit comments

Comments
 (0)