Skip to content

Commit

Permalink
[ACS-8833] Displaying error page when there is no selected nodes (#4142)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksanderSklorz authored Sep 27, 2024
1 parent 34e494e commit a93722b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { SearchAiResultsComponent } from './search-ai-results.component';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { of, Subject, throwError } from 'rxjs';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { EmptyContentComponent, UserPreferencesService } from '@alfresco/adf-core';
import { EmptyContentComponent, UnsavedChangesGuard, UserPreferencesService } from '@alfresco/adf-core';
import { MatDialogModule } from '@angular/material/dialog';
import { AppTestingModule } from '../../../../testing/app-testing.module';
import { MatIconTestingModule } from '@angular/material/icon/testing';
Expand All @@ -40,6 +40,7 @@ import { SearchAiInputComponent } from '../search-ai-input/search-ai-input.compo
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { getAppSelection, getCurrentFolder, ViewNodeAction } from '@alfresco/aca-shared/store';
import { ViewerService } from '@alfresco/aca-content/viewer';
import { DebugElement } from '@angular/core';

const questionMock: QuestionModel = { question: 'test', questionId: 'testId', restrictionQuery: { nodesIds: [] } };
const aiAnswerMock: AiAnswer = { answer: 'Some answer', questionId: 'some id', references: [] };
Expand All @@ -57,6 +58,7 @@ describe('SearchAiResultsComponent', () => {
let searchAiService: SearchAiService;
let store: MockStore;
let viewerService: ViewerService;
let unsavedChangesGuard: UnsavedChangesGuard;

afterEach(() => {
store.resetSelectors();
Expand Down Expand Up @@ -92,6 +94,7 @@ describe('SearchAiResultsComponent', () => {
searchAiService = TestBed.inject(SearchAiService);
userPreferencesService = TestBed.inject(UserPreferencesService);
viewerService = TestBed.inject(ViewerService);
unsavedChangesGuard = TestBed.inject(UnsavedChangesGuard);
store = TestBed.inject(MockStore);
store.overrideSelector(getAppSelection, {
nodes: [],
Expand All @@ -107,6 +110,8 @@ describe('SearchAiResultsComponent', () => {
});

describe('query params change', () => {
const getEmptyContentElement = (): DebugElement => fixture.debugElement.query(By.directive(EmptyContentComponent));

it('should perform ai search and sets agents on query params change', () => {
spyOn(userPreferencesService, 'get').and.returnValue(knowledgeRetrievalNodes);
mockQueryParams.next({ query: 'test', agentId: 'agentId1' });
Expand Down Expand Up @@ -212,6 +217,27 @@ describe('SearchAiResultsComponent', () => {
expect(component.hasAnsweringError).toBeFalse();
}));

it('should render empty content when there are not selected nodes', () => {
mockQueryParams.next({
query: 'test',
agentId: 'agentId1'
});

fixture.detectChanges();
expect(getEmptyContentElement()).not.toBeNull();
});

it('should not render empty content when there are selected nodes', () => {
spyOn(userPreferencesService, 'get').and.returnValue(knowledgeRetrievalNodes);
mockQueryParams.next({
query: 'test',
agentId: 'agentId1'
});

fixture.detectChanges();
expect(getEmptyContentElement()).toBeNull();
});

describe('when query params contains location', () => {
let params: Params;

Expand All @@ -236,7 +262,7 @@ describe('SearchAiResultsComponent', () => {
});

fixture.detectChanges();
expect(fixture.debugElement.query(By.directive(EmptyContentComponent))).toBeNull();
expect(getEmptyContentElement()).toBeNull();
});

it('should not display search query', () => {
Expand Down Expand Up @@ -368,5 +394,26 @@ describe('SearchAiResultsComponent', () => {

expect(nodesOrder).toEqual(['node1', 'node2']);
});

it('should set unsaved on UnsavedChangesGuard to false when there are no selected nodes', () => {
mockQueryParams.next({
query: 'test',
agentId: 'agentId1'
});

component.ngOnInit();
expect(unsavedChangesGuard.unsaved).toBeFalse();
});

it('should set unsaved on UnsavedChangesGuard to true when there are selected nodes', () => {
spyOn(userPreferencesService, 'get').and.returnValue(knowledgeRetrievalNodes);
mockQueryParams.next({
query: 'test',
agentId: 'agentId1'
});

component.ngOnInit();
expect(unsavedChangesGuard.unsaved).toBeTrue();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,17 @@ export class SearchAiResultsComponent extends PageComponent implements OnInit, O
.subscribe((params) => {
this._agentId = params.agentId;
this._searchQuery = params.query ? decodeURIComponent(params.query) : '';
if (!this.searchQuery || !this.agentId) {
const selectedNodesState = this.userPreferencesService.get('knowledgeRetrievalNodes');
if (!this.searchQuery || !this.agentId || !selectedNodesState) {
this._hasError = true;
return;
}
this._selectedNodesState = JSON.parse(this.userPreferencesService.get('knowledgeRetrievalNodes'));
this._selectedNodesState = JSON.parse(selectedNodesState);
this.performAiSearch();
});
super.ngOnInit();

this.unsavedChangesGuard.unsaved = this.route.snapshot?.queryParams?.query?.length > 0;
this.unsavedChangesGuard.unsaved = this.route.snapshot?.queryParams?.query?.length > 0 && !this.hasError;
this.unsavedChangesGuard.data = {
descriptionText: 'KNOWLEDGE_RETRIEVAL.SEARCH.DISCARD_CHANGES.CONVERSATION_DISCARDED',
confirmButtonText: 'KNOWLEDGE_RETRIEVAL.SEARCH.DISCARD_CHANGES.OKAY',
Expand Down

0 comments on commit a93722b

Please sign in to comment.