|
| 1 | +/*! |
| 2 | + * Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Alfresco Example Content Application |
| 5 | + * |
| 6 | + * This file is part of the Alfresco Example Content Application. |
| 7 | + * If the software was purchased under a paid Alfresco license, the terms of |
| 8 | + * the paid license agreement will prevail. Otherwise, the software is |
| 9 | + * provided under the following open source license terms: |
| 10 | + * |
| 11 | + * The Alfresco Example Content Application is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Lesser General Public License as published by |
| 13 | + * the Free Software Foundation, either version 3 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * The Alfresco Example Content Application is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * from Hyland Software. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + */ |
| 24 | + |
| 25 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 26 | +import { Router } from '@angular/router'; |
| 27 | +import { UnitTestingUtils, UserPreferencesService } from '@alfresco/adf-core'; |
| 28 | +import { DocumentListComponent, SitesService } from '@alfresco/adf-content-services'; |
| 29 | +import { LibraryListComponent } from './library-list.component'; |
| 30 | +import { AppTestingModule } from '../../testing/app-testing.module'; |
| 31 | +import { AppHookService } from '@alfresco/aca-shared'; |
| 32 | +import { provideEffects } from '@ngrx/effects'; |
| 33 | +import { RouterEffects } from '@alfresco/aca-shared/store'; |
| 34 | +import { of, throwError } from 'rxjs'; |
| 35 | +import { LibraryEffects } from '../../store/effects'; |
| 36 | +import { getTitleElementText } from '../../testing/test-utils'; |
| 37 | +import { MatSnackBarModule } from '@angular/material/snack-bar'; |
| 38 | +import { Pagination, SiteEntry, SitePaging } from '@alfresco/js-api'; |
| 39 | + |
| 40 | +describe('LibraryListComponent', () => { |
| 41 | + let fixture: ComponentFixture<LibraryListComponent>; |
| 42 | + let component: LibraryListComponent; |
| 43 | + let userPreference: UserPreferencesService; |
| 44 | + let sitesService: SitesService; |
| 45 | + let router: Router; |
| 46 | + let appHookService: AppHookService; |
| 47 | + let getSitesSpy: jasmine.Spy; |
| 48 | + let unitTestingUtils: UnitTestingUtils; |
| 49 | + |
| 50 | + const paging: SitePaging = { |
| 51 | + list: { |
| 52 | + entries: [ |
| 53 | + { entry: { id: '1', guid: '1', title: 'Library 1', visibility: 'public' } }, |
| 54 | + { entry: { id: '2', guid: '2', title: 'Library 2', visibility: 'private' } } |
| 55 | + ], |
| 56 | + pagination: { count: 25, skipCount: 0 } |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + beforeEach(async () => { |
| 61 | + TestBed.configureTestingModule({ |
| 62 | + imports: [AppTestingModule, LibraryListComponent, MatSnackBarModule], |
| 63 | + providers: [provideEffects([RouterEffects, LibraryEffects])] |
| 64 | + }); |
| 65 | + |
| 66 | + fixture = TestBed.createComponent(LibraryListComponent); |
| 67 | + component = fixture.componentInstance; |
| 68 | + |
| 69 | + unitTestingUtils = new UnitTestingUtils(fixture.debugElement); |
| 70 | + sitesService = TestBed.inject(SitesService); |
| 71 | + userPreference = TestBed.inject(UserPreferencesService); |
| 72 | + appHookService = TestBed.inject(AppHookService); |
| 73 | + router = TestBed.inject(Router); |
| 74 | + |
| 75 | + getSitesSpy = spyOn(sitesService, 'getSites'); |
| 76 | + getSitesSpy.and.returnValue(of(paging)); |
| 77 | + fixture.detectChanges(); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('on initialization', () => { |
| 81 | + it('should set data', () => { |
| 82 | + expect(component.list).toBe(paging); |
| 83 | + expect(component.pagination).toBe(paging.list.pagination); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should get data with user preference pagination size', () => { |
| 87 | + userPreference.paginationSize = 1; |
| 88 | + component.ngOnInit(); |
| 89 | + expect(sitesService.getSites).toHaveBeenCalledWith({ maxItems: userPreference.paginationSize }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should set data on error', () => { |
| 93 | + getSitesSpy.and.returnValue(throwError(() => 'error')); |
| 94 | + component.ngOnInit(); |
| 95 | + |
| 96 | + expect(component.list).toBe(null); |
| 97 | + expect(component.pagination).toBe(null); |
| 98 | + expect(component.isLoading).toBe(false); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should set title based on selectedRowItemsCount', () => { |
| 102 | + expect(getTitleElementText(fixture)).toBe('APP.BROWSE.LIBRARIES.MENU.ALL_LIBRARIES.TITLE'); |
| 103 | + |
| 104 | + component.selectedRowItemsCount = 5; |
| 105 | + fixture.detectChanges(); |
| 106 | + |
| 107 | + expect(getTitleElementText(fixture)).toBe('APP.HEADER.SELECTED'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should handle no columns preset in extensions', () => { |
| 111 | + component['extensions'].documentListPresets.libraries = undefined; |
| 112 | + component.ngOnInit(); |
| 113 | + expect(component.columns.length).toBe(0); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('Node navigation', () => { |
| 118 | + it('should not navigate when node is null or missing guid', () => { |
| 119 | + spyOn(router, 'navigate').and.stub(); |
| 120 | + component.navigateTo(null); |
| 121 | + expect(router.navigate).not.toHaveBeenCalled(); |
| 122 | + |
| 123 | + component.navigateTo({ entry: {} } as any); |
| 124 | + expect(router.navigate).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should dispatch navigation action when node has guid', () => { |
| 128 | + spyOn(component['store'], 'dispatch').and.stub(); |
| 129 | + component.navigateTo({ entry: { guid: 'guid' } } as SiteEntry); |
| 130 | + expect(component['store'].dispatch).toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle node double click', () => { |
| 134 | + spyOn(component, 'navigateTo').and.stub(); |
| 135 | + const documentList = unitTestingUtils.getByDirective(DocumentListComponent); |
| 136 | + documentList.triggerEventHandler('node-dblclick', { detail: { node: { entry: { guid: 'guid' } } } }); |
| 137 | + expect(component.navigateTo).toHaveBeenCalledWith({ entry: { guid: 'guid' } } as SiteEntry); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle name click', () => { |
| 141 | + spyOn(component, 'navigateTo').and.stub(); |
| 142 | + const documentList = unitTestingUtils.getByDirective(DocumentListComponent); |
| 143 | + documentList.triggerEventHandler('name-click', { detail: { node: { entry: { guid: 'guid' } } } }); |
| 144 | + expect(component.navigateTo).toHaveBeenCalledWith({ entry: { guid: 'guid' } } as SiteEntry); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('Reload on actions', () => { |
| 149 | + it('should reload on libraryDeleted action', () => { |
| 150 | + appHookService.libraryDeleted.next(''); |
| 151 | + expect(sitesService.getSites).toHaveBeenCalled(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should reload on libraryUpdated action', () => { |
| 155 | + appHookService.libraryUpdated.next(paging.list.entries[0]); |
| 156 | + expect(sitesService.getSites).toHaveBeenCalled(); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should reload on libraryJoined action', () => { |
| 160 | + appHookService.libraryJoined.next(); |
| 161 | + expect(sitesService.getSites).toHaveBeenCalled(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should reload on libraryLeft action', () => { |
| 165 | + appHookService.libraryLeft.next(''); |
| 166 | + expect(sitesService.getSites).toHaveBeenCalled(); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('Pagination', () => { |
| 171 | + const pagination: Pagination = { |
| 172 | + count: 100, |
| 173 | + hasMoreItems: true, |
| 174 | + totalItems: 300, |
| 175 | + skipCount: 25, |
| 176 | + maxItems: 25 |
| 177 | + }; |
| 178 | + |
| 179 | + it('should get list with pagination data onChange event', () => { |
| 180 | + component.onChange(pagination); |
| 181 | + expect(sitesService.getSites).toHaveBeenCalledWith(pagination); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should get list with pagination data onChangePageSize event', () => { |
| 185 | + component.onChangePageSize(pagination); |
| 186 | + expect(sitesService.getSites).toHaveBeenCalledWith(pagination); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should set preference page size onChangePageSize event', () => { |
| 190 | + component.onChangePageSize(pagination); |
| 191 | + expect(userPreference.paginationSize).toBe(pagination.maxItems); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments