diff --git a/e2e/search-page/search-page.e2e-spec.ts b/e2e/search-page/search-page.e2e-spec.ts new file mode 100644 index 0000000000..421781f585 --- /dev/null +++ b/e2e/search-page/search-page.e2e-spec.ts @@ -0,0 +1,49 @@ +import { ProtractorPage } from './search-page.po'; +import { browser } from 'protractor'; +import { promise } from 'selenium-webdriver'; + +describe('protractor SearchPage', () => { + let page: ProtractorPage; + + beforeEach(() => { + page = new ProtractorPage(); + }); + + it('should contain query value when navigating to page with query parameter', () => { + const queryString = 'Interesting query string'; + page.navigateToSearchWithQueryParameter(queryString); + page.getCurrentQuery().then((query: string) => { + expect(query).toEqual(queryString); + }); + }); + + it('should have right scope selected when navigating to page with scope parameter', () => { + const scope: promise.Promise = page.getRandomScopeOption(); + scope.then((scopeString: string) => { + page.navigateToSearchWithScopeParameter(scopeString); + page.getCurrentScope().then((s: string) => { + expect(s).toEqual(scopeString); + }); + }); + }); + + it('should redirect to the correct url when scope was set and submit button was triggered', () => { + const scope: promise.Promise = page.getRandomScopeOption(); + scope.then((scopeString: string) => { + page.setCurrentScope(scopeString); + page.submitSearchForm(); + browser.getCurrentUrl().then((url: string) => { + expect(url.indexOf('scope=' + encodeURI(scopeString))).toBeGreaterThanOrEqual(0); + }); + }); + }); + + it('should redirect to the correct url when query was set and submit button was triggered', () => { + const queryString = 'Another interesting query string'; + page.setCurrentQuery(queryString); + page.submitSearchForm(); + browser.getCurrentUrl().then((url: string) => { + expect(url.indexOf('query=' + encodeURI(queryString))).toBeGreaterThanOrEqual(0); + }); + }); +}); diff --git a/e2e/search-page/search-page.po.ts b/e2e/search-page/search-page.po.ts new file mode 100644 index 0000000000..b5e43dcf82 --- /dev/null +++ b/e2e/search-page/search-page.po.ts @@ -0,0 +1,47 @@ +import { browser, element, by } from 'protractor'; +import { promise } from 'selenium-webdriver'; + +export class ProtractorPage { + SEARCH = '/search'; + + navigateToSearch() { + return browser.get(this.SEARCH); + } + + navigateToSearchWithQueryParameter(query: string) { + return browser.get(this.SEARCH + '?query=' + query); + } + + navigateToSearchWithScopeParameter(scope: string) { + return browser.get(this.SEARCH + '?scope=' + scope); + } + + getCurrentScope(): promise.Promise { + return element(by.tagName('select')).getAttribute('value'); + } + + getCurrentQuery(): promise.Promise { + return element(by.tagName('input')).getAttribute('value'); + } + + setCurrentScope(scope: string) { + element(by.css('option[value="' + scope + '"]')).click(); + } + + setCurrentQuery(query: string) { + element(by.css('input[name="query"]')).sendKeys(query); + } + + submitSearchForm() { + element(by.css('button.search-button')).click(); + } + + getRandomScopeOption(): promise.Promise { + const options = element(by.css('select[name="scope"]')).all(by.tagName('option')); + return options.count().then((c: number) => { + const index: number = Math.floor(Math.random() * c); + return options.get(index).getAttribute('value'); + }); + } + +} diff --git a/src/app/+search-page/search-page.component.spec.ts b/src/app/+search-page/search-page.component.spec.ts new file mode 100644 index 0000000000..07f70cc648 --- /dev/null +++ b/src/app/+search-page/search-page.component.spec.ts @@ -0,0 +1,110 @@ +import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { ActivatedRoute, Router } from '@angular/router'; +import { Observable } from 'rxjs/Observable'; +import { CommunityDataService } from '../core/data/community-data.service'; +import { SearchPageComponent } from './search-page.component'; +import { SearchService } from './search.service'; +import { Community } from '../core/shared/community.model'; +import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model'; +import { SortDirection, SortOptions } from '../core/cache/models/sort-options.model'; + +describe('SearchPageComponent', () => { + let comp: SearchPageComponent; + let fixture: ComponentFixture; + let searchServiceObject: SearchService; + const mockResults = ['test', 'data']; + const searchServiceStub = { + search: () => mockResults + }; + const queryParam = 'test query'; + const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f'; + const activatedRouteStub = { + queryParams: Observable.of({ + query: queryParam, + scope: scopeParam + }) + }; + const mockCommunityList = []; + const communityDataServiceStub = { + findAll: () => mockCommunityList, + findById: () => new Community() + }; + + class RouterStub { + navigateByUrl(url: string) { + return url; + } + } + + beforeEach(async(() => { + TestBed.configureTestingModule({ + // imports: [ SearchPageModule ], + declarations: [SearchPageComponent], + providers: [ + { provide: SearchService, useValue: searchServiceStub }, + { provide: ActivatedRoute, useValue: activatedRouteStub }, + { provide: CommunityDataService, useValue: communityDataServiceStub }, + { provide: Router, useClass: RouterStub } + ], + schemas: [NO_ERRORS_SCHEMA] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(SearchPageComponent); + comp = fixture.componentInstance; // SearchPageComponent test instance + fixture.detectChanges(); + searchServiceObject = (comp as any).service; + }); + + it('should set the scope and query based on the route parameters', () => { + expect(comp.query).toBe(queryParam); + expect((comp as any).scope).toBe(scopeParam); + }); + + describe('when update search results is called', () => { + let pagination; + let sort; + beforeEach(() => { + pagination = Object.assign( + {}, + new PaginationComponentOptions(), + { + currentPage: 5, + pageSize: 15 + } + ); + sort = Object.assign({}, + new SortOptions(), + { + direction: SortDirection.Ascending, + field: 'test-field' + } + ); + }); + + it('should call the search function of the search service with the right parameters', () => { + spyOn(searchServiceObject, 'search').and.callThrough(); + + (comp as any).updateSearchResults({ + pagination: pagination, + sort: sort + }); + + expect(searchServiceObject.search).toHaveBeenCalledWith(queryParam, scopeParam, { + pagination: pagination, + sort: sort + }); + }); + + it('should update the results', () => { + spyOn(searchServiceObject, 'search').and.callThrough(); + + (comp as any).updateSearchResults({}); + + expect(comp.results as any).toBe(mockResults); + }); + + }); +}); diff --git a/src/app/+search-page/search-page.module.ts b/src/app/+search-page/search-page.module.ts index a4fe47f8ba..7cb5f6cbe6 100644 --- a/src/app/+search-page/search-page.module.ts +++ b/src/app/+search-page/search-page.module.ts @@ -7,7 +7,7 @@ import { TranslateModule } from '@ngx-translate/core'; import { SharedModule } from '../shared/shared.module'; import { SearchPageRoutingModule } from './search-page-routing.module'; import { SearchPageComponent } from './search-page.component'; -import { SearchResultsComponent } from './search-results/search-results.compontent'; +import { SearchResultsComponent } from './search-results/search-results.component'; import { ItemSearchResultListElementComponent } from '../object-list/search-result-list-element/item-search-result/item-search-result-list-element.component'; import { CollectionSearchResultListElementComponent } from '../object-list/search-result-list-element/collection-search-result/collection-search-result-list-element.component'; import { CommunitySearchResultListElementComponent } from '../object-list/search-result-list-element/community-search-result/community-search-result-list-element.component'; diff --git a/src/app/+search-page/search-results/search-results.component.spec.ts b/src/app/+search-page/search-results/search-results.component.spec.ts new file mode 100644 index 0000000000..4f299c5c50 --- /dev/null +++ b/src/app/+search-page/search-results/search-results.component.spec.ts @@ -0,0 +1,142 @@ +import { ComponentFixture, TestBed, async, tick, fakeAsync } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core'; +import { ResourceType } from '../../core/shared/resource-type'; +import { Community } from '../../core/shared/community.model'; +import { TranslateModule } from '@ngx-translate/core'; +import { SearchResultsComponent } from './search-results.component'; + +describe('SearchResultsComponent', () => { + let comp: SearchResultsComponent; + let fixture: ComponentFixture; + let heading: DebugElement; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [TranslateModule.forRoot()], + declarations: [SearchResultsComponent], + schemas: [NO_ERRORS_SCHEMA] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(SearchResultsComponent); + comp = fixture.componentInstance; // SearchFormComponent test instance + heading = fixture.debugElement.query(By.css('heading')); + }); + + it('should display heading when results are not empty', fakeAsync(() => { + (comp as any).searchResults = 'test'; + (comp as any).searchConfig = {pagination: ''}; + fixture.detectChanges(); + tick(); + expect(heading).toBeDefined(); + })); + + it('should not display heading when results is empty', () => { + expect(heading).toBeNull(); + }); +}); + +export const objects = [ + Object.assign(new Community(), { + handle: '10673/11', + logo: { + self: { + _isScalar: true, + value: 'https://dspace7.4science.it/dspace-spring-rest/api/core/bitstreams/10b636d0-7890-4968-bcd6-0d83bf4e2b42', + scheduler: null + } + }, + collections: { + self: { + _isScalar: true, + value: '1506937433727', + scheduler: null + } + }, + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/communities/7669c72a-3f2a-451f-a3b9-9210e7a4c02f', + id: '7669c72a-3f2a-451f-a3b9-9210e7a4c02f', + uuid: '7669c72a-3f2a-451f-a3b9-9210e7a4c02f', + type: ResourceType.Community, + name: 'OR2017 - Demonstration', + metadata: [ + { + key: 'dc.description', + language: null, + value: '' + }, + { + key: 'dc.description.abstract', + language: null, + value: 'This is a test community to hold content for the OR2017 demostration' + }, + { + key: 'dc.description.tableofcontents', + language: null, + value: '' + }, + { + key: 'dc.rights', + language: null, + value: '' + }, + { + key: 'dc.title', + language: null, + value: 'OR2017 - Demonstration' + } + ] + }), + Object.assign(new Community(), + { + handle: '10673/1', + logo: { + self: { + _isScalar: true, + value: 'https://dspace7.4science.it/dspace-spring-rest/api/core/bitstreams/f446c17d-6d51-45ea-a610-d58a73642d40', + scheduler: null + } + }, + collections: { + self: { + _isScalar: true, + value: '1506937433727', + scheduler: null + } + }, + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/communities/9076bd16-e69a-48d6-9e41-0238cb40d863', + id: '9076bd16-e69a-48d6-9e41-0238cb40d863', + uuid: '9076bd16-e69a-48d6-9e41-0238cb40d863', + type: ResourceType.Community, + name: 'Sample Community', + metadata: [ + { + key: 'dc.description', + language: null, + value: '

This is the introductory text for the Sample Community on the DSpace Demonstration Site. It is editable by System or Community Administrators (of this Community).

\r\n

DSpace Communities may contain one or more Sub-Communities or Collections (of Items).

\r\n

This particular Community has its own logo (the DuraSpace logo).

' + }, + { + key: 'dc.description.abstract', + language: null, + value: 'This is a sample top-level community' + }, + { + key: 'dc.description.tableofcontents', + language: null, + value: '

This is the news section for this Sample Community. System or Community Administrators (of this Community) can edit this News field.

' + }, + { + key: 'dc.rights', + language: null, + value: '

If this Community had special copyright text to display, it would be displayed here.

' + }, + { + key: 'dc.title', + language: null, + value: 'Sample Community' + } + ] + } + ) +]; diff --git a/src/app/+search-page/search-results/search-results.compontent.ts b/src/app/+search-page/search-results/search-results.component.ts similarity index 100% rename from src/app/+search-page/search-results/search-results.compontent.ts rename to src/app/+search-page/search-results/search-results.component.ts diff --git a/src/app/shared/search-form/search-form.component.spec.ts b/src/app/shared/search-form/search-form.component.spec.ts new file mode 100644 index 0000000000..0da50658a5 --- /dev/null +++ b/src/app/shared/search-form/search-form.component.spec.ts @@ -0,0 +1,204 @@ +import { ComponentFixture, TestBed, async, tick, fakeAsync } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { DebugElement } from '@angular/core'; +import { SearchFormComponent } from './search-form.component'; +import { Observable } from 'rxjs/Observable'; +import { FormsModule } from '@angular/forms'; +import { ResourceType } from '../../core/shared/resource-type'; +import { RouterTestingModule } from '@angular/router/testing'; +import { Community } from '../../core/shared/community.model'; +import { TranslateModule } from '@ngx-translate/core'; + +describe('SearchFormComponent', () => { + let comp: SearchFormComponent; + let fixture: ComponentFixture; + let de: DebugElement; + let el: HTMLElement; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [FormsModule, RouterTestingModule, TranslateModule.forRoot()], + declarations: [SearchFormComponent] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(SearchFormComponent); + comp = fixture.componentInstance; // SearchFormComponent test instance + de = fixture.debugElement.query(By.css('form')); + el = de.nativeElement; + }); + + it('should display scopes when available with default and all scopes', () => { + comp.scopes = Observable.of(objects); + fixture.detectChanges(); + const select: HTMLElement = de.query(By.css('select')).nativeElement; + expect(select).toBeDefined(); + const options: HTMLCollection = select.children; + const defOption: Element = options.item(0); + expect(defOption.getAttribute('value')).toBe(''); + + let index = 1; + objects.forEach((object) => { + expect(options.item(index).textContent).toBe(object.name); + expect(options.item(index).getAttribute('value')).toBe(object.uuid); + index++; + }); + }); + + it('should not display scopes when empty', () => { + fixture.detectChanges(); + const select = de.query(By.css('select')); + expect(select).toBeNull(); + }); + + it('should display set query value in input field', fakeAsync(() => { + const testString = 'This is a test query'; + comp.query = testString; + + fixture.detectChanges(); + tick(); + const queryInput = de.query(By.css('input')).nativeElement; + + expect(queryInput.value).toBe(testString); + })); + + it('should select correct scope option in scope select', fakeAsync(() => { + comp.scopes = Observable.of(objects); + fixture.detectChanges(); + + const testCommunity = objects[1]; + comp.scope = testCommunity; + + fixture.detectChanges(); + tick(); + const scopeSelect = de.query(By.css('select')).nativeElement; + + expect(scopeSelect.value).toBe(testCommunity.id); + })); + // it('should call updateSearch when clicking the submit button with correct parameters', fakeAsync(() => { + // comp.query = 'Test String' + // fixture.detectChanges(); + // spyOn(comp, 'updateSearch').and.callThrough(); + // fixture.detectChanges(); + // + // const submit = de.query(By.css('button.search-button')).nativeElement; + // const scope = '123456'; + // const query = 'test'; + // const select = de.query(By.css('select')).nativeElement; + // const input = de.query(By.css('input')).nativeElement; + // + // tick(); + // select.value = scope; + // input.value = query; + // + // fixture.detectChanges(); + // + // submit.click(); + // + // expect(comp.updateSearch).toHaveBeenCalledWith({ scope: scope, query: query }); + // })); +}); + +export const objects = [ + Object.assign(new Community(), { + handle: '10673/11', + logo: { + self: { + _isScalar: true, + value: 'https://dspace7.4science.it/dspace-spring-rest/api/core/bitstreams/10b636d0-7890-4968-bcd6-0d83bf4e2b42', + scheduler: null + } + }, + collections: { + self: { + _isScalar: true, + value: '1506937433727', + scheduler: null + } + }, + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/communities/7669c72a-3f2a-451f-a3b9-9210e7a4c02f', + id: '7669c72a-3f2a-451f-a3b9-9210e7a4c02f', + uuid: '7669c72a-3f2a-451f-a3b9-9210e7a4c02f', + type: ResourceType.Community, + name: 'OR2017 - Demonstration', + metadata: [ + { + key: 'dc.description', + language: null, + value: '' + }, + { + key: 'dc.description.abstract', + language: null, + value: 'This is a test community to hold content for the OR2017 demostration' + }, + { + key: 'dc.description.tableofcontents', + language: null, + value: '' + }, + { + key: 'dc.rights', + language: null, + value: '' + }, + { + key: 'dc.title', + language: null, + value: 'OR2017 - Demonstration' + } + ] + }), + Object.assign(new Community(), + { + handle: '10673/1', + logo: { + self: { + _isScalar: true, + value: 'https://dspace7.4science.it/dspace-spring-rest/api/core/bitstreams/f446c17d-6d51-45ea-a610-d58a73642d40', + scheduler: null + } + }, + collections: { + self: { + _isScalar: true, + value: '1506937433727', + scheduler: null + } + }, + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/communities/9076bd16-e69a-48d6-9e41-0238cb40d863', + id: '9076bd16-e69a-48d6-9e41-0238cb40d863', + uuid: '9076bd16-e69a-48d6-9e41-0238cb40d863', + type: ResourceType.Community, + name: 'Sample Community', + metadata: [ + { + key: 'dc.description', + language: null, + value: '

This is the introductory text for the Sample Community on the DSpace Demonstration Site. It is editable by System or Community Administrators (of this Community).

\r\n

DSpace Communities may contain one or more Sub-Communities or Collections (of Items).

\r\n

This particular Community has its own logo (the DuraSpace logo).

' + }, + { + key: 'dc.description.abstract', + language: null, + value: 'This is a sample top-level community' + }, + { + key: 'dc.description.tableofcontents', + language: null, + value: '

This is the news section for this Sample Community. System or Community Administrators (of this Community) can edit this News field.

' + }, + { + key: 'dc.rights', + language: null, + value: '

If this Community had special copyright text to display, it would be displayed here.

' + }, + { + key: 'dc.title', + language: null, + value: 'Sample Community' + } + ] + } + ) +];