diff --git a/lib/content-services/src/lib/category/mock/category-mock.service.ts b/lib/content-services/src/lib/category/mock/category-mock.service.ts index 9644ad6b67..e6c11a2056 100644 --- a/lib/content-services/src/lib/category/mock/category-mock.service.ts +++ b/lib/content-services/src/lib/category/mock/category-mock.service.ts @@ -32,6 +32,16 @@ export class CategoryServiceMock { return parentNodeId ? of(this.getChildrenLevelResponse(skipCount, maxItems)) : of(this.getRootLevelResponse(skipCount, maxItems)); } + public getCategory(): Observable { + return of({ + entry: { + name: 'some name', + id: 'some id', + hasChildren: true + } + }); + } + public searchCategories(): Observable { const result = new ResultSetPaging(); result.list = new ResultSetPagingList(); diff --git a/lib/content-services/src/lib/category/services/category-tree-datasource.service.spec.ts b/lib/content-services/src/lib/category/services/category-tree-datasource.service.spec.ts index 424fd277ae..c6a664d661 100644 --- a/lib/content-services/src/lib/category/services/category-tree-datasource.service.spec.ts +++ b/lib/content-services/src/lib/category/services/category-tree-datasource.service.spec.ts @@ -21,7 +21,7 @@ import { CategoryService } from '../services/category.service'; import { CategoryNode, CategoryTreeDatasourceService } from '@alfresco/adf-content-services'; import { CategoryServiceMock } from '../mock/category-mock.service'; import { TreeNodeType, TreeResponse } from '../../tree'; -import { EMPTY } from 'rxjs'; +import { EMPTY, of } from 'rxjs'; import { Pagination } from '@alfresco/js-api'; describe('CategoryTreeDatasourceService', () => { @@ -83,7 +83,45 @@ describe('CategoryTreeDatasourceService', () => { expect(categoryService.searchCategories).toHaveBeenCalledWith(name, skipCount, maxItems); }); + it('should call getCategory for every instance if value of name parameter is defined', (done) => { + spyOn(categoryService, 'getCategory').and.returnValues(of({ + entry: { + name: 'name', + id: 'some id 1', + hasChildren: true + } + }), + of({ + entry: { + name: 'Language/some other name', + id: 'some id 2', + hasChildren: false + } + })); + categoryTreeDatasourceService.getSubNodes('id', undefined, undefined, 'name') + .subscribe(() => { + + expect(categoryService.getCategory).toHaveBeenCalledWith('some id 1'); + expect(categoryService.getCategory).toHaveBeenCalledWith('some id 2'); + done(); + }); + }); + it('should return observable which emits correct categories', (done) => { + spyOn(categoryService, 'getCategory').and.returnValues(of({ + entry: { + name: 'some name', + id: 'some id 1', + hasChildren: true + } + }), + of({ + entry: { + name: 'Language/some other name', + id: 'some id 2', + hasChildren: false + } + })); categoryTreeDatasourceService.getSubNodes('id', undefined, undefined, 'name') .subscribe((response) => { const pagination = new Pagination(); @@ -96,7 +134,7 @@ describe('CategoryTreeDatasourceService', () => { parentId: 'parent id 1', level: 0, nodeType: TreeNodeType.RegularNode, - hasChildren: false, + hasChildren: true, isLoading: false }, { id: 'some id 2', diff --git a/lib/content-services/src/lib/category/services/category-tree-datasource.service.ts b/lib/content-services/src/lib/category/services/category-tree-datasource.service.ts index a15c18af07..80f5885b6d 100644 --- a/lib/content-services/src/lib/category/services/category-tree-datasource.service.ts +++ b/lib/content-services/src/lib/category/services/category-tree-datasource.service.ts @@ -20,8 +20,8 @@ import { TreeNodeType, TreeResponse, TreeService } from '../../tree'; import { CategoryNode } from '../models/category-node.interface'; import { CategoryService } from './category.service'; import { CategoryEntry, CategoryPaging } from '@alfresco/js-api'; -import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { from, Observable } from 'rxjs'; +import { map, mergeMap, toArray } from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class CategoryTreeDatasourceService extends TreeService { @@ -56,25 +56,29 @@ export class CategoryTreeDatasourceService extends TreeService { } const treeResponse: TreeResponse = {entries: nodesList, pagination: response.list.pagination}; return treeResponse; - })) : this.categoryService.searchCategories(name, skipCount, maxItems).pipe(map((pagingResult) => { + })) : this.categoryService.searchCategories(name, skipCount, maxItems).pipe(mergeMap((pagingResult) => { const nextAfterGeneralPathPartIndex = 3; const pathSeparator = '/'; - return { - entries: pagingResult.list.entries.map((category) => { - const path = category.entry.path.name.split(pathSeparator).slice(nextAfterGeneralPathPartIndex) - .join(pathSeparator); - return { - id: category.entry.id, - nodeName: path ? `${path}/${category.entry.name}` : category.entry.name, - parentId: category.entry.parentId, - level: 0, - nodeType: TreeNodeType.RegularNode, - hasChildren: false, - isLoading: false - }; - }), - pagination: pagingResult.list.pagination - }; + return from(pagingResult.list.entries).pipe(mergeMap((category) => { + const path = category.entry.path.name.split(pathSeparator).slice(nextAfterGeneralPathPartIndex) + .join(pathSeparator); + + return this.categoryService.getCategory(category.entry.id).pipe( + map((res) => { + return { + id: category.entry.id, + nodeName: path ? `${path}/${category.entry.name}` : category.entry.name, + parentId: category.entry.parentId, + level: 0, + nodeType: TreeNodeType.RegularNode, + hasChildren: res.entry.hasChildren, + isLoading: false + }; + }) + ); + }), + toArray(), + map(res => ({entries: res, pagination: pagingResult.list.pagination}))); })); } }