[ACS-8398] Unit tests part 2 (#3989)

* ACS-8398 Unit tests for search ai input container

* ACS-8398 Unit tests for search ai navigation service and rest tests for search ai input container component

* ACS-8398 Added missing type
This commit is contained in:
AleksanderSklorz
2024-08-01 15:13:20 +02:00
committed by Aleksander Sklorz
parent 26393fee9c
commit a577285cd9
5 changed files with 323 additions and 3 deletions

View File

@@ -0,0 +1,186 @@
/*!
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
import { SearchAiInputContainerComponent } from './search-ai-input-container.component';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SearchAiInputComponent } from '../search-ai-input/search-ai-input.component';
import { By } from '@angular/platform-browser';
import { AgentService, ContentTestingModule, SearchAiService } from '@alfresco/adf-content-services';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { getAppSelection } from '@alfresco/aca-shared/store';
import { of, Subject } from 'rxjs';
import { MatDivider } from '@angular/material/divider';
import { DebugElement } from '@angular/core';
import { MatIconButton } from '@angular/material/button';
import { MatIcon } from '@angular/material/icon';
import { SearchAiNavigationService } from '../../../../services/search-ai-navigation.service';
import { NavigationEnd, NavigationStart, Router, RouterEvent } from '@angular/router';
describe('SearchAiInputContainerComponent', () => {
let component: SearchAiInputContainerComponent;
let fixture: ComponentFixture<SearchAiInputContainerComponent>;
let routingEvents$: Subject<RouterEvent>;
let searchAiService: SearchAiService;
let store: MockStore;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [SearchAiInputContainerComponent, ContentTestingModule],
providers: [
provideMockStore(),
{
provide: AgentService,
useValue: {
getAgents: () =>
of({
list: {
entries: [
{
entry: {
id: '1',
name: 'HR Agent'
}
}
]
}
})
}
}
]
});
fixture = TestBed.createComponent(SearchAiInputContainerComponent);
component = fixture.componentInstance;
store = TestBed.inject(MockStore);
searchAiService = TestBed.inject(SearchAiService);
store.overrideSelector(getAppSelection, {
nodes: [],
isEmpty: true,
count: 0,
libraries: []
});
component.agentId = '1';
routingEvents$ = new Subject<RouterEvent>();
spyOnProperty(TestBed.inject(Router), 'events').and.returnValue(routingEvents$);
fixture.detectChanges();
});
describe('Search ai input', () => {
let inputComponent: SearchAiInputComponent;
beforeEach(() => {
inputComponent = fixture.debugElement.query(By.directive(SearchAiInputComponent)).componentInstance;
});
it('should have assigned correct default placeholder', () => {
expect(inputComponent.placeholder).toBe('KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.DEFAULT_PLACEHOLDER');
});
it('should have assigned correct placeholder if placeholder has been changed', () => {
component.placeholder = 'Some placeholder';
fixture.detectChanges();
expect(inputComponent.placeholder).toBe(component.placeholder);
});
it('should have assigned correct agentId', () => {
expect(inputComponent.agentId).toBe(component.agentId);
});
it('should have assigned correct useStoredNodes flag', () => {
component.useStoredNodes = true;
fixture.detectChanges();
expect(inputComponent.useStoredNodes).toBeTrue();
});
it('should call updateSearchAiInputState on SearchAiService when triggered searchSubmitted event', () => {
spyOn(searchAiService, 'updateSearchAiInputState');
inputComponent.searchSubmitted.emit();
expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({
active: false
});
});
});
describe('Divider', () => {
it('should have a vertical divider', () => {
fixture.detectChanges();
expect(fixture.debugElement.query(By.directive(MatDivider)).componentInstance.vertical).toBeTrue();
});
});
describe('Leaving button', () => {
let button: DebugElement;
beforeEach(() => {
button = fixture.debugElement.query(By.directive(MatIconButton));
});
it('should have correct title', () => {
expect(button.nativeElement.title).toBe('KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.HIDE_INPUT');
});
it('should contain close icon', () => {
expect(button.query(By.directive(MatIcon)).nativeElement.textContent).toBe('close');
});
it('should call updateSearchAiInputState on SearchAiService when clicked', () => {
spyOn(searchAiService, 'updateSearchAiInputState');
button.nativeElement.click();
expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({
active: false
});
});
it('should call navigateToPreviousRoute on SearchAiNavigationService when clicked', () => {
const searchNavigationService = TestBed.inject(SearchAiNavigationService);
spyOn(searchNavigationService, 'navigateToPreviousRoute');
button.nativeElement.click();
expect(searchNavigationService.navigateToPreviousRoute).toHaveBeenCalled();
});
});
describe('Navigation', () => {
it('should call updateSearchAiInputState on SearchAiService when navigation starts', () => {
spyOn(searchAiService, 'updateSearchAiInputState');
routingEvents$.next(new NavigationStart(1, ''));
expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({
active: false
});
});
it('should not call updateSearchAiInputState on SearchAiService when there is different event than navigation starts', () => {
spyOn(searchAiService, 'updateSearchAiInputState');
routingEvents$.next(new NavigationEnd(1, '', ''));
expect(searchAiService.updateSearchAiInputState).not.toHaveBeenCalled();
});
});
});

View File

@@ -0,0 +1,134 @@
/*!
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
import { SearchAiNavigationService } from './search-ai-navigation.service';
import { Params, Router } from '@angular/router';
import { TestBed } from '@angular/core/testing';
import { ContentTestingModule } from '@alfresco/adf-content-services';
describe('SearchAiNavigationService', () => {
let service: SearchAiNavigationService;
let router: Router;
const knowledgeRetrievalUrl = '/knowledge-retrieval';
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ContentTestingModule]
});
service = TestBed.inject(SearchAiNavigationService);
router = TestBed.inject(Router);
});
describe('navigateToPreviousRoute', () => {
let urlSpy: jasmine.Spy<() => string>;
let navigateByUrlSpy: jasmine.Spy<(url: string) => Promise<boolean>>;
const sourceUrl = '/some-url';
const personalFilesUrl = '/personal-files';
beforeEach(() => {
navigateByUrlSpy = spyOn(router, 'navigateByUrl');
urlSpy = spyOnProperty(router, 'url');
});
it('should navigate to personal files if there is not previous route and actual route is knowledge retrieval', () => {
urlSpy.and.returnValue(knowledgeRetrievalUrl);
service.navigateToPreviousRoute();
expect(navigateByUrlSpy).toHaveBeenCalledWith(personalFilesUrl);
});
it('should not navigate if there is not previous route and actual route is not knowledge retrieval', () => {
urlSpy.and.returnValue('/some-url');
service.navigateToPreviousRoute();
expect(navigateByUrlSpy).not.toHaveBeenCalled();
});
it('should navigate to previous route if there is some previous route and actual route is knowledge retrieval', () => {
urlSpy.and.returnValue(sourceUrl);
service.navigateToSearchAi({
agentId: 'some agent id'
});
urlSpy.and.returnValue(knowledgeRetrievalUrl);
navigateByUrlSpy.calls.reset();
service.navigateToPreviousRoute();
expect(navigateByUrlSpy).toHaveBeenCalledWith(sourceUrl);
});
it('should not navigate to previous route if there is some previous route but actual route is not knowledge retrieval', () => {
urlSpy.and.returnValue(sourceUrl);
service.navigateToSearchAi({
agentId: 'some agent id'
});
urlSpy.and.returnValue('/some-different-url');
navigateByUrlSpy.calls.reset();
service.navigateToPreviousRoute();
expect(navigateByUrlSpy).not.toHaveBeenCalled();
});
it('should navigate to personal files if previous route is knowledge retrieval and actual route is knowledge retrieval', () => {
urlSpy.and.returnValue(knowledgeRetrievalUrl);
service.navigateToSearchAi({
agentId: 'some agent id'
});
navigateByUrlSpy.calls.reset();
service.navigateToPreviousRoute();
expect(navigateByUrlSpy).toHaveBeenCalledWith(personalFilesUrl);
});
it('should not navigate if previous route is knowledge retrieval and actual route is different than knowledge retrieval', () => {
urlSpy.and.returnValue(knowledgeRetrievalUrl);
service.navigateToSearchAi({
agentId: 'some agent id'
});
urlSpy.and.returnValue(sourceUrl);
navigateByUrlSpy.calls.reset();
service.navigateToPreviousRoute();
expect(navigateByUrlSpy).not.toHaveBeenCalled();
});
});
describe('navigateToSearchAi', () => {
beforeEach(() => {
spyOn(router, 'navigate');
});
it('should navigate to search ai results page', () => {
const queryParams: Params = {
agentId: 'some agent id'
};
service.navigateToSearchAi(queryParams);
expect(router.navigate).toHaveBeenCalledWith([knowledgeRetrievalUrl], {
queryParams
});
});
});
});

View File

@@ -43,6 +43,6 @@ export class SearchAiNavigationService {
if (!this.router.url.includes(this.knowledgeRetrievalRoute)) { if (!this.router.url.includes(this.knowledgeRetrievalRoute)) {
this.previousRoute = this.router.url; this.previousRoute = this.router.url;
} }
void this.router.navigate([this.knowledgeRetrievalRoute], { queryParams: queryParams }); void this.router.navigate([this.knowledgeRetrievalRoute], { queryParams });
} }
} }

View File

@@ -28,6 +28,7 @@ import { SearchAiActionTypes, SearchByTermAiAction, ToggleAISearchInput } from '
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { SearchAiNavigationService } from '../../services/search-ai-navigation.service'; import { SearchAiNavigationService } from '../../services/search-ai-navigation.service';
import { SearchAiService } from '@alfresco/adf-content-services'; import { SearchAiService } from '@alfresco/adf-content-services';
import { Params } from '@angular/router';
@Injectable() @Injectable()
export class SearchAiEffects { export class SearchAiEffects {
@@ -38,7 +39,7 @@ export class SearchAiEffects {
this.actions$.pipe( this.actions$.pipe(
ofType<SearchByTermAiAction>(SearchAiActionTypes.SearchByTermAi), ofType<SearchByTermAiAction>(SearchAiActionTypes.SearchByTermAi),
map((action) => { map((action) => {
const queryParams = { const queryParams: Params = {
query: encodeURIComponent(action.payload.searchTerm), query: encodeURIComponent(action.payload.searchTerm),
agentId: action.payload.agentId agentId: action.payload.agentId
}; };

View File

@@ -84,7 +84,6 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges {
protected searchAiService: SearchAiService = inject(SearchAiService); protected searchAiService: SearchAiService = inject(SearchAiService);
protected subscriptions: Subscription[] = []; protected subscriptions: Subscription[] = [];
private fileAutoDownloadService = inject(AcaFileAutoDownloadService, { optional: true });
private _searchAiInputState: SearchAiInputState = { private _searchAiInputState: SearchAiInputState = {
active: false active: false
}; };