From 924ceff6476aa7ca10f062f25b467b379233bae6 Mon Sep 17 00:00:00 2001 From: Aleksander Sklorz Date: Tue, 21 Nov 2023 13:08:10 +0100 Subject: [PATCH] ACS-6252 Unit tests for changes for AppExtensionService --- .../services/app.extension.service.spec.ts | 49 +++++++++++++++++-- .../src/lib/services/app.extension.service.ts | 8 +-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/projects/aca-shared/src/lib/services/app.extension.service.spec.ts b/projects/aca-shared/src/lib/services/app.extension.service.spec.ts index 6f6af0ceab..632eb238cb 100644 --- a/projects/aca-shared/src/lib/services/app.extension.service.spec.ts +++ b/projects/aca-shared/src/lib/services/app.extension.service.spec.ts @@ -1049,13 +1049,14 @@ describe('AppExtensionService', () => { }); describe('search', () => { + let config: ExtensionConfig; + beforeEach(() => { extensions.setEvaluators({ visible: () => true, notVisible: () => false }); - - applyConfig({ + config = { $id: 'test', $name: 'test', $version: '1.0.0', @@ -1105,22 +1106,64 @@ describe('AppExtensionService', () => { } ] } - }); + }; }); it('should load the search extension', () => { + applyConfig(config); expect(service.search.length).toBe(2); expect(service.search[0].id).toBe('app.search'); expect(service.search[1].id).toBe('app.search-1'); }); it('should not load the disabled search extension', () => { + applyConfig(config); expect(service.search.find(({ id }) => id === 'app.search-2')).toBe(undefined, 'disabled configuration shown in the result'); }); it('should not load the not visible search extension', () => { + applyConfig(config); expect(service.search.find(({ id }) => id === 'app.search-3')).toBe(undefined, 'not visible configuration shown in the result'); }); + + it('should contain category if it has no rules field', () => { + applyConfig(config); + const search = service.search[0]; + expect(search.categories.length).toBe(1); + expect(search.categories[0].id).toBe('size'); + }); + + it('should contain category if it has no visible field in rules', () => { + config.features.search[0].categories[0].rules = {}; + + applyConfig(config); + const search = service.search[0]; + expect(search.categories.length).toBe(1); + expect(search.categories[0].id).toBe('size'); + }); + + it('should contain category if it has visible field and extensions.evaluateRule returns true', () => { + spyOn(extensions, 'evaluateRule').and.returnValue(true); + const visible = 'test'; + config.features.search[0].categories[0].rules = { visible }; + + applyConfig(config); + const search = service.search[0]; + expect(extensions.evaluateRule).toHaveBeenCalledWith(visible, service); + expect(search.categories.length).toBe(1); + expect(search.categories[0].id).toBe('size'); + }); + + it('should not contain category if it has visible field and extensions.evaluateRule returns false', () => { + spyOn(extensions, 'evaluateRule').and.returnValue(false); + const visible = 'test'; + config.features.search[0].categories[0].rules = { visible }; + + applyConfig(config); + const search = service.search[0]; + expect(extensions.evaluateRule).toHaveBeenCalledWith(visible, service); + expect(search.categories.length).toBe(0); + }); }); describe('rules', () => { diff --git a/projects/aca-shared/src/lib/services/app.extension.service.ts b/projects/aca-shared/src/lib/services/app.extension.service.ts index 633c317c88..15ada1b5b9 100644 --- a/projects/aca-shared/src/lib/services/app.extension.service.ts +++ b/projects/aca-shared/src/lib/services/app.extension.service.ts @@ -56,7 +56,7 @@ import { ViewerRules } from '../models/viewer.rules'; import { Badge, SettingsGroupRef } from '../models/types'; import { NodePermissionService } from '../services/node-permission.service'; import { filter, map } from 'rxjs/operators'; -import { SearchCategory, SearchConfiguration } from '@alfresco/adf-content-services'; +import { SearchCategory } from '@alfresco/adf-content-services'; @Injectable({ providedIn: 'root' @@ -67,7 +67,7 @@ export class AppExtensionService implements RuleContext { navbar: Array = []; sidebarTabs: Array = []; contentMetadata: any; - search: SearchConfiguration[]; + search: any; viewerRules: ViewerRules = {}; settingGroups: Array = []; @@ -168,8 +168,8 @@ export class AppExtensionService implements RuleContext { this.sidebarTabs = this.loader.getElements(config, 'features.sidebar.tabs'); this.contentMetadata = this.loadContentMetadata(config); this.search = this.loadSearchForms(config); - this.search.forEach((searchSet) => { - searchSet.categories = searchSet.categories.filter((category) => this.filterVisible(category)); + this.search?.forEach((searchSet) => { + searchSet.categories = searchSet.categories?.filter((category) => this.filterVisible(category)); }); this.documentListPresets = {