From 5aa0c6f21d2eb19be79e1d7f3baaaa4708893075 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Fri, 22 Mar 2019 13:06:06 +0000 Subject: [PATCH] [ACA-2299] fix search error translation (#1030) * fix search error translation * fix translation fallback * remove fdescribe --- .../search-results.component.spec.ts | 57 ++++++++++++++++++- .../search-results.component.ts | 32 ++++++++--- 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/app/components/search/search-results/search-results.component.spec.ts b/src/app/components/search/search-results/search-results.component.spec.ts index c8c5936cf..cb195a761 100644 --- a/src/app/components/search/search-results/search-results.component.spec.ts +++ b/src/app/components/search/search-results/search-results.component.spec.ts @@ -13,7 +13,8 @@ import { CoreModule, AppConfigService, AlfrescoApiService, - AlfrescoApiServiceMock + AlfrescoApiServiceMock, + TranslationService } from '@alfresco/adf-core'; import { Store } from '@ngrx/store'; import { NavigateToFolder, SnackbarErrorAction } from '../../../store/actions'; @@ -28,6 +29,7 @@ describe('SearchComponent', () => { let store: Store; let queryBuilder: SearchQueryBuilderService; let alfrescoApi: AlfrescoApiService; + let translate: TranslationService; beforeEach(async(() => { TestBed.configureTestingModule({ @@ -60,6 +62,7 @@ describe('SearchComponent', () => { store = TestBed.get(Store); queryBuilder = TestBed.get(SearchQueryBuilderService); alfrescoApi = TestBed.get(AlfrescoApiService); + translate = TestBed.get(TranslationService); fixture = TestBed.createComponent(SearchResultsComponent); component = fixture.componentInstance; @@ -83,7 +86,57 @@ describe('SearchComponent', () => { tick(); expect(store.dispatch).toHaveBeenCalledWith( - new SnackbarErrorAction('APP.BROWSE.SEARCH.ERRORS.500') + new SnackbarErrorAction('APP.BROWSE.SEARCH.ERRORS.GENERIC') + ); + })); + + it('should raise a known error if search fails', fakeAsync(() => { + spyOn(translate, 'instant').and.callFake((key: string) => { + if (key === 'APP.BROWSE.SEARCH.ERRORS.401') { + return 'Known Error'; + } + return key; + }); + + spyOn(alfrescoApi.searchApi, 'search').and.returnValue( + Promise.reject({ + message: `{ "error": { "statusCode": 401 } } ` + }) + ); + + spyOn(queryBuilder, 'buildQuery').and.returnValue({}); + spyOn(store, 'dispatch').and.stub(); + + queryBuilder.execute(); + tick(); + + expect(store.dispatch).toHaveBeenCalledWith( + new SnackbarErrorAction('Known Error') + ); + })); + + it('should raise a generic error if search fails', fakeAsync(() => { + spyOn(translate, 'instant').and.callFake((key: string) => { + if (key === 'APP.BROWSE.SEARCH.ERRORS.GENERIC') { + return 'Generic Error'; + } + return key; + }); + + spyOn(alfrescoApi.searchApi, 'search').and.returnValue( + Promise.reject({ + message: `{ "error": { "statusCode": 401 } } ` + }) + ); + + spyOn(queryBuilder, 'buildQuery').and.returnValue({}); + spyOn(store, 'dispatch').and.stub(); + + queryBuilder.execute(); + tick(); + + expect(store.dispatch).toHaveBeenCalledWith( + new SnackbarErrorAction('Generic Error') ); })); diff --git a/src/app/components/search/search-results/search-results.component.ts b/src/app/components/search/search-results/search-results.component.ts index f812d5dfe..569df6e2e 100644 --- a/src/app/components/search/search-results/search-results.component.ts +++ b/src/app/components/search/search-results/search-results.component.ts @@ -37,7 +37,11 @@ import { AppStore } from '../../../store/states/app.state'; import { NavigateToFolder } from '../../../store/actions'; import { AppExtensionService } from '../../../extensions/extension.service'; import { ContentManagementService } from '../../../services/content-management.service'; -import { AppConfigService, AlfrescoApiService } from '@alfresco/adf-core'; +import { + AppConfigService, + AlfrescoApiService, + TranslationService +} from '@alfresco/adf-core'; import { Observable, Subject } from 'rxjs'; import { showFacetFilter } from '../../../store/selectors/app.selectors'; import { SnackbarErrorAction } from '../../../store/actions'; @@ -72,7 +76,8 @@ export class SearchResultsComponent extends PageComponent implements OnInit { private config: AppConfigService, store: Store, extensions: AppExtensionService, - content: ContentManagementService + content: ContentManagementService, + private translationService: TranslationService ) { super(store, extensions, content); @@ -124,13 +129,7 @@ export class SearchResultsComponent extends PageComponent implements OnInit { }), this.searchQueryError.subscribe(error => { - const { statusCode } = JSON.parse(error.message).error; - - this.store.dispatch( - new SnackbarErrorAction( - `APP.BROWSE.SEARCH.ERRORS.${statusCode || 'GENERIC'}` - ) - ); + this.onSearchError(error); }) ); @@ -154,6 +153,21 @@ export class SearchResultsComponent extends PageComponent implements OnInit { } } + onSearchError(error: { message: any }) { + const { statusCode } = JSON.parse(error.message).error; + + const messageKey = `APP.BROWSE.SEARCH.ERRORS.${statusCode}`; + let translated = this.translationService.instant(messageKey); + + if (translated === messageKey) { + translated = this.translationService.instant( + `APP.BROWSE.SEARCH.ERRORS.GENERIC` + ); + } + + this.store.dispatch(new SnackbarErrorAction(translated)); + } + private isOperator(input: string): boolean { if (input) { input = input.trim().toUpperCase();