From 7cb18bc19a3010aa7dbe5f71eae3d301d3a9c4f8 Mon Sep 17 00:00:00 2001 From: dominikiwanekhyland <141320833+dominikiwanekhyland@users.noreply.github.com> Date: Thu, 20 Mar 2025 09:21:44 +0100 Subject: [PATCH] Ng18 migration fix unit tests (#4468) * Fix details unit tests --- .../details/details.component.spec.ts | 244 +++++++++--------- 1 file changed, 118 insertions(+), 126 deletions(-) diff --git a/projects/aca-content/src/lib/components/details/details.component.spec.ts b/projects/aca-content/src/lib/components/details/details.component.spec.ts index d66bedd2d..fe58cc453 100644 --- a/projects/aca-content/src/lib/components/details/details.component.spec.ts +++ b/projects/aca-content/src/lib/components/details/details.component.spec.ts @@ -57,14 +57,7 @@ describe('DetailsComponent', () => { select: () => mockStream }; - const extensionsServiceMock = { - getAllowedSidebarActions: jasmine.createSpy('getAllowedSidebarActions') - }; - - const mockAspectActions: ContentActionRef[] = []; - - const mockAspectActionsSubject$ = new BehaviorSubject(mockAspectActions); - extensionsServiceMock.getAllowedSidebarActions.and.returnValue(mockAspectActionsSubject$.asObservable()); + const mockAspectActionsSubject$ = new BehaviorSubject>([]); const getBreadcrumb = (): BreadcrumbComponent => fixture.debugElement.query(By.directive(BreadcrumbComponent)).componentInstance; @@ -99,6 +92,9 @@ describe('DetailsComponent', () => { schemas: [NO_ERRORS_SCHEMA] }); + const appExtensionService = TestBed.inject(AppExtensionService); + spyOn(appExtensionService, 'getAllowedSidebarActions').and.returnValue(mockAspectActionsSubject$.asObservable()); + fixture = TestBed.createComponent(DetailsComponent); component = fixture.componentInstance; contentApiService = TestBed.inject(ContentApiService); @@ -130,138 +126,134 @@ describe('DetailsComponent', () => { fixture.destroy(); }); - it('should get node id from router', () => { - fixture.detectChanges(); - expect(component.nodeId).toBe('someId'); - }); - - it('should set active tab from router', () => { - fixture.detectChanges(); - expect(component.activeTab).toBe(2); - }); - - it('should get node info after setting node from router', () => { - fixture.detectChanges(); - expect(contentApiService.getNode).toHaveBeenCalled(); - }); - - it('should dispatch navigation to a given folder', () => { - const pathElement: PathElement = { - id: 'fake-id' - }; - getBreadcrumb().navigate.emit(pathElement); - fixture.detectChanges(); - - expect(store.dispatch).toHaveBeenCalledWith(new NavigateToFolder({ entry: pathElement } as NodeEntry)); - }); - - it('should pass different node as folderNode to breadcrumb when nodeUpdated from nodesApiService is triggered', () => { - fixture.detectChanges(); - const breadcrumbComponent = getBreadcrumb(); - const updatedNode = { - name: 'other node' - } as Node; - - nodesApiService.nodeUpdated.next(updatedNode); - fixture.detectChanges(); - expect(breadcrumbComponent.folderNode).toEqual(updatedNode); - expect(breadcrumbComponent.folderNode).not.toBe(updatedNode); - expect(updatedNode).not.toEqual(node.entry); - }); - - it('should dispatch node selection', () => { - fixture.detectChanges(); - expect(store.dispatch).toHaveBeenCalledWith(new SetSelectedNodesAction([node])); - }); - - it('should set aspectActions from extensions', async () => { - extensionsServiceMock.getAllowedSidebarActions.and.returnValue(of(mockAspectActions)); - fixture.detectChanges(); - await fixture.whenStable().then(() => { - expect(component.aspectActions).toEqual(mockAspectActions); - }); - }); - - it('should return the icon when getNodeIcon is called', () => { - const expectedIcon = 'assets/images/ft_ic_folder'; - spyOn(contentService, 'getNodeIcon').and.returnValue(expectedIcon); - fixture.detectChanges(); - component.ngOnInit(); - expect(contentService.getNodeIcon).toHaveBeenCalled(); - expect(component.nodeIcon).toContain(expectedIcon); - }); - it('should set aspectActions from extension mock', () => { - const extensionMock = { - getAllowedSidebarActions: () => - of([ - { - id: 'app.sidebar.close', - order: 100, - title: 'close', - icon: 'highlight_off' - } - ]) - }; + const extensionMock = [ + { + id: 'app.sidebar.close', + order: 100, + title: 'close', + icon: 'highlight_off' + } as ContentActionRef + ]; + + mockAspectActionsSubject$.next(extensionMock); - extensionsServiceMock.getAllowedSidebarActions.and.returnValue(of(extensionMock)); fixture.detectChanges(); - fixture - .whenStable() - .then(() => { - expect(component.aspectActions).toEqual([ - { - id: 'app.sidebar.close', - order: 100, - title: 'close', - icon: 'highlight_off' - } as ContentActionRef - ]); - }) - .catch((error) => { - fail(`An error occurred: ${error}`); + expect(component.aspectActions).toEqual([ + { + id: 'app.sidebar.close', + order: 100, + title: 'close', + icon: 'highlight_off' + } as ContentActionRef + ]); + }); + + describe('', () => { + beforeEach(() => { + mockAspectActionsSubject$.next([]); + }); + + it('should get node id from router', () => { + fixture.detectChanges(); + expect(component.nodeId).toBe('someId'); + }); + + it('should set active tab from router', () => { + fixture.detectChanges(); + expect(component.activeTab).toBe(2); + }); + + it('should get node info after setting node from router', () => { + fixture.detectChanges(); + expect(contentApiService.getNode).toHaveBeenCalled(); + }); + + it('should dispatch navigation to a given folder', () => { + const pathElement: PathElement = { + id: 'fake-id' + }; + getBreadcrumb().navigate.emit(pathElement); + fixture.detectChanges(); + + expect(store.dispatch).toHaveBeenCalledWith(new NavigateToFolder({ entry: pathElement } as NodeEntry)); + }); + + it('should pass different node as folderNode to breadcrumb when nodeUpdated from nodesApiService is triggered', () => { + fixture.detectChanges(); + const breadcrumbComponent = getBreadcrumb(); + const updatedNode = { + name: 'other node' + } as Node; + + nodesApiService.nodeUpdated.next(updatedNode); + fixture.detectChanges(); + expect(breadcrumbComponent.folderNode).toEqual(updatedNode); + expect(breadcrumbComponent.folderNode).not.toBe(updatedNode); + expect(updatedNode).not.toEqual(node.entry); + }); + + it('should dispatch node selection', () => { + fixture.detectChanges(); + expect(store.dispatch).toHaveBeenCalledWith(new SetSelectedNodesAction([node])); + }); + + it('should set aspectActions from extensions', async () => { + fixture.detectChanges(); + await fixture.whenStable().then(() => { + expect(component.aspectActions).toEqual([]); }); - }); + }); - it('should disable the permissions tab for smart folders based on aspects', () => { - node.entry.isFolder = true; - node.entry.aspectNames = ['smf:customConfigSmartFolder']; - fixture.detectChanges(); - component.ngOnInit(); - expect(component.canManagePermissions).toBeFalse(); - expect(component.activeTab).not.toBe(2); - }); + it('should return the icon when getNodeIcon is called', () => { + const expectedIcon = 'assets/images/ft_ic_folder'; + spyOn(contentService, 'getNodeIcon').and.returnValue(expectedIcon); + fixture.detectChanges(); + component.ngOnInit(); + expect(contentService.getNodeIcon).toHaveBeenCalled(); + expect(component.nodeIcon).toContain(expectedIcon); + }); - it('should enable the permissions tab for regular folders based on aspects', () => { - node.entry.isFolder = true; - node.entry.aspectNames = []; - fixture.detectChanges(); - component.ngOnInit(); + it('should disable the permissions tab for smart folders based on aspects', () => { + node.entry.isFolder = true; + node.entry.aspectNames = ['smf:customConfigSmartFolder']; + fixture.detectChanges(); + component.ngOnInit(); + expect(component.canManagePermissions).toBeFalse(); + expect(component.activeTab).not.toBe(2); + }); - expect(component.canManagePermissions).toBeTrue(); - }); + it('should enable the permissions tab for regular folders based on aspects', () => { + node.entry.isFolder = true; + node.entry.aspectNames = []; + fixture.detectChanges(); + component.ngOnInit(); - it('should change active tab based on canManagePermissions and tabName', () => { - component.nodeId = 'someNodeId'; - component.activeTab = 0; + expect(component.canManagePermissions).toBeTrue(); + }); - node.entry.isFolder = true; - node.entry.aspectNames = []; + it('should change active tab based on canManagePermissions and tabName', () => { + component.nodeId = 'someNodeId'; + component.activeTab = 0; - fixture.detectChanges(); - component.ngOnInit(); + node.entry.isFolder = true; + node.entry.aspectNames = []; - component.setActiveTab('permissions'); - expect(component.activeTab).toBe(2); + fixture.detectChanges(); + component.ngOnInit(); - node.entry.isFolder = true; - node.entry.aspectNames = ['smf:customConfigSmartFolder']; + component.setActiveTab('permissions'); + expect(component.activeTab).toBe(2); - fixture.detectChanges(); - component.ngOnInit(); + node.entry.isFolder = true; + node.entry.aspectNames = ['smf:customConfigSmartFolder']; - component.setActiveTab('permissions'); - expect(component.activeTab).not.toBe(2); + fixture.detectChanges(); + component.ngOnInit(); + + component.setActiveTab('permissions'); + expect(component.activeTab).not.toBe(2); + }); }); it('should navigate back when nodesDeleted event is triggered', () => {