|
| 1 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
| 2 | +import { |
| 3 | + ComponentFixture, |
| 4 | + TestBed, |
| 5 | + waitForAsync, |
| 6 | +} from '@angular/core/testing'; |
| 7 | +import { ActivatedRoute } from '@angular/router'; |
| 8 | +import { StoreModule } from '@ngrx/store'; |
| 9 | +import { TranslateModule } from '@ngx-translate/core'; |
| 10 | +import { of as observableOf } from 'rxjs'; |
| 11 | + |
| 12 | +import { environment } from '../../../environments/environment'; |
| 13 | +import { buildPaginatedList } from '../../core/data/paginated-list.model'; |
| 14 | +import { PageInfo } from '../../core/shared/page-info.model'; |
| 15 | +import { SearchService } from '../../core/shared/search/search.service'; |
| 16 | +import { SearchConfigurationService } from '../../core/shared/search/search-configuration.service'; |
| 17 | +import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils'; |
| 18 | +import { FacetValue } from '../../shared/search/models/facet-value.model'; |
| 19 | +import { FilterType } from '../../shared/search/models/filter-type.model'; |
| 20 | +import { PaginatedSearchOptions } from '../../shared/search/models/paginated-search-options.model'; |
| 21 | +import { SearchFilterConfig } from '../../shared/search/models/search-filter-config.model'; |
| 22 | +import { SearchServiceStub } from '../../shared/testing/search-service.stub'; |
| 23 | +import { BrowseByGeospatialDataComponent } from './browse-by-geospatial-data.component'; |
| 24 | + |
| 25 | +// create route stub |
| 26 | +const scope = 'test scope'; |
| 27 | +const activatedRouteStub = { |
| 28 | + queryParams: observableOf({ |
| 29 | + scope: scope, |
| 30 | + }), |
| 31 | +}; |
| 32 | + |
| 33 | +// Mock search filter config |
| 34 | +const mockFilterConfig = Object.assign(new SearchFilterConfig(), { |
| 35 | + name: 'point', |
| 36 | + type: FilterType.text, |
| 37 | + hasFacets: true, |
| 38 | + isOpenByDefault: false, |
| 39 | + pageSize: 2, |
| 40 | + minValue: 200, |
| 41 | + maxValue: 3000, |
| 42 | +}); |
| 43 | + |
| 44 | +// Mock facet values with and without point data |
| 45 | +const facetValue: FacetValue = { |
| 46 | + label: 'test', |
| 47 | + value: 'test', |
| 48 | + count: 20, |
| 49 | + _links: { |
| 50 | + self: { href: 'selectedValue-self-link2' }, |
| 51 | + search: { href: `` }, |
| 52 | + }, |
| 53 | +}; |
| 54 | +const pointFacetValue: FacetValue = { |
| 55 | + label: 'test point', |
| 56 | + value: 'Point ( +174.000000 -042.000000 )', |
| 57 | + count: 20, |
| 58 | + _links: { |
| 59 | + self: { href: 'selectedValue-self-link' }, |
| 60 | + search: { href: `` }, |
| 61 | + }, |
| 62 | +}; |
| 63 | +const mockValues = createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), [facetValue])); |
| 64 | +const mockPointValues = createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), [pointFacetValue])); |
| 65 | + |
| 66 | +// Expected search options used in getFacetValuesFor call |
| 67 | +const expectedSearchOptions: PaginatedSearchOptions = Object.assign({ |
| 68 | + 'configuration': environment.geospatialMapViewer.spatialFacetDiscoveryConfiguration, |
| 69 | + 'scope': scope, |
| 70 | + 'facetLimit': 99999, |
| 71 | +}); |
| 72 | + |
| 73 | +// Mock search config service returns mock search filter config on getConfig() |
| 74 | +const mockSearchConfigService = jasmine.createSpyObj('searchConfigurationService', { |
| 75 | + getConfig: createSuccessfulRemoteDataObject$([mockFilterConfig]), |
| 76 | +}); |
| 77 | +let searchService: SearchServiceStub = new SearchServiceStub(); |
| 78 | + |
| 79 | +// initialize testing environment |
| 80 | +describe('BrowseByGeospatialDataComponent', () => { |
| 81 | + let component: BrowseByGeospatialDataComponent; |
| 82 | + let fixture: ComponentFixture<BrowseByGeospatialDataComponent>; |
| 83 | + |
| 84 | + beforeEach(waitForAsync(() => { |
| 85 | + TestBed.configureTestingModule({ |
| 86 | + imports: [ TranslateModule.forRoot(), StoreModule.forRoot(), BrowseByGeospatialDataComponent], |
| 87 | + providers: [ |
| 88 | + { provide: SearchService, useValue: searchService }, |
| 89 | + { provide: SearchConfigurationService, useValue: mockSearchConfigService }, |
| 90 | + { provide: ActivatedRoute, useValue: activatedRouteStub }, |
| 91 | + ], |
| 92 | + schemas: [NO_ERRORS_SCHEMA], |
| 93 | + }) |
| 94 | + .compileComponents(); |
| 95 | + })); |
| 96 | + |
| 97 | + beforeEach(() => { |
| 98 | + fixture = TestBed.createComponent(BrowseByGeospatialDataComponent); |
| 99 | + component = fixture.componentInstance; |
| 100 | + }); |
| 101 | + |
| 102 | + it('component should be created successfully', () => { |
| 103 | + expect(component).toBeTruthy(); |
| 104 | + }); |
| 105 | + describe('BrowseByGeospatialDataComponent component with valid facet values', () => { |
| 106 | + beforeEach(() => { |
| 107 | + fixture = TestBed.createComponent(BrowseByGeospatialDataComponent); |
| 108 | + component = fixture.componentInstance; |
| 109 | + spyOn(searchService, 'getFacetValuesFor').and.returnValue(mockPointValues); |
| 110 | + component.scope$ = observableOf(''); |
| 111 | + component.ngOnInit(); |
| 112 | + fixture.detectChanges(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should call searchConfigService.getConfig() after init', waitForAsync(() => { |
| 116 | + expect(mockSearchConfigService.getConfig).toHaveBeenCalled(); |
| 117 | + })); |
| 118 | + |
| 119 | + it('should call searchService.getFacetValuesFor() with expected parameters', waitForAsync(() => { |
| 120 | + component.getFacetValues().subscribe(() => { |
| 121 | + expect(searchService.getFacetValuesFor).toHaveBeenCalledWith(mockFilterConfig, 1, expectedSearchOptions, null, true); |
| 122 | + }); |
| 123 | + })); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('BrowseByGeospatialDataComponent component with invalid facet values (no point data)', () => { |
| 127 | + beforeEach(() => { |
| 128 | + fixture = TestBed.createComponent(BrowseByGeospatialDataComponent); |
| 129 | + component = fixture.componentInstance; |
| 130 | + spyOn(searchService, 'getFacetValuesFor').and.returnValue(mockValues); |
| 131 | + component.scope$ = observableOf(''); |
| 132 | + component.ngOnInit(); |
| 133 | + fixture.detectChanges(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should call searchConfigService.getConfig() after init', waitForAsync(() => { |
| 137 | + expect(mockSearchConfigService.getConfig).toHaveBeenCalled(); |
| 138 | + })); |
| 139 | + |
| 140 | + it('should call searchService.getFacetValuesFor() with expected parameters', waitForAsync(() => { |
| 141 | + component.getFacetValues().subscribe(() => { |
| 142 | + expect(searchService.getFacetValuesFor).toHaveBeenCalledWith(mockFilterConfig, 1, expectedSearchOptions, null, true); |
| 143 | + }); |
| 144 | + })); |
| 145 | + }); |
| 146 | + |
| 147 | +}); |
0 commit comments