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 55f543f77..3c22a2478 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 @@ -1,24 +1,109 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { SearchResultsComponent } from './search-results.component'; +import { AppTestingModule } from '../../../testing/app-testing.module'; +import { AppSearchResultsModule } from '../search-results.module'; +import { CoreModule, AppConfigService } from '@alfresco/adf-core'; +import { Store } from '@ngrx/store'; +import { NavigateToFolder } from '../../../store/actions'; +import { Pagination } from '@alfresco/js-api'; +import { SearchQueryBuilderService } from '@alfresco/adf-content-services'; describe('SearchComponent', () => { let component: SearchResultsComponent; let fixture: ComponentFixture; + let config: AppConfigService; + let store: Store; + let queryBuilder: SearchQueryBuilderService; beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [SearchResultsComponent] - }).compileComponents(); - })); + imports: [CoreModule.forRoot(), AppTestingModule, AppSearchResultsModule] + }); + + config = TestBed.get(AppConfigService); + store = TestBed.get(Store); + queryBuilder = TestBed.get(SearchQueryBuilderService); - beforeEach(() => { fixture = TestBed.createComponent(SearchResultsComponent); component = fixture.componentInstance; fixture.detectChanges(); + })); + + it('should return null if formatting invalid query', () => { + expect(component.formatSearchQuery(null)).toBeNull(); + expect(component.formatSearchQuery('')).toBeNull(); }); - xit('should create', () => { - expect(component).toBeTruthy(); + it('should use original user input if content contains colons', () => { + const query = 'TEXT:test OR TYPE:folder'; + expect(component.formatSearchQuery(query)).toBe(query); + }); + + it('should format user input according to the configuration fields', () => { + config.config = { + search: { + 'aca:fields': ['cm:name', 'cm:title'] + } + }; + + const query = component.formatSearchQuery('hello'); + expect(query).toBe(`cm:name:"hello*" OR cm:title:"hello*"`); + }); + + it('should format user input as cm:name if configuration not provided', () => { + config.config = { + search: { + 'aca:fields': undefined + } + }; + + const query = component.formatSearchQuery('hello'); + expect(query).toBe(`cm:name:"hello*"`); + }); + + it('should navigate to folder on double click', () => { + const node: any = { + entry: { + isFolder: true + } + }; + + spyOn(store, 'dispatch').and.stub(); + + component.onNodeDoubleClick(node); + + expect(store.dispatch).toHaveBeenCalledWith(new NavigateToFolder(node)); + }); + + it('should preview file node on double click', () => { + const node: any = { + entry: { + isFolder: false + } + }; + + spyOn(component, 'showPreview').and.stub(); + + component.onNodeDoubleClick(node); + + expect(component.showPreview).toHaveBeenCalledWith(node); + }); + + it('should re-run search on pagination change', () => { + const page = new Pagination({ + maxItems: 10, + skipCount: 0 + }); + + spyOn(queryBuilder, 'update').and.stub(); + + component.onPaginationChanged(page); + + expect(queryBuilder.paging).toEqual({ + maxItems: 10, + skipCount: 0 + }); + expect(queryBuilder.update).toHaveBeenCalled(); }); }); 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 f197c0633..8892cc7c3 100644 --- a/src/app/components/search/search-results/search-results.component.ts +++ b/src/app/components/search/search-results/search-results.component.ts @@ -114,11 +114,17 @@ export class SearchResultsComponent extends PageComponent implements OnInit { } } - private formatSearchQuery(userInput: string) { + formatSearchQuery(userInput: string) { if (!userInput) { return null; } + userInput = userInput.trim(); + + if (userInput.includes(':')) { + return userInput; + } + const fields = this.config.get('search.aca:fields', ['cm:name']); const query = fields.map(field => `${field}:"${userInput}*"`).join(' OR ');