import { HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions } from '@angular/http'; import {MockBackend, MockConnection} from '@angular/http/testing'; import {provide} from '@angular/core'; import {beforeEach, beforeEachProviders, inject} from '@angular/core/testing'; import {AuthHttp, AuthConfig} from 'angular2-jwt'; import { ES_SEARCH_CONFIG, EsSearchConfig } from '../../dist/app/config/es-search.config'; import {EsSearchService} from '../../dist/app/search/es-search.service'; import {mockProductionResponse} from '../../dist/app/mocks/search/response/elastic-search-production.mock'; import {RecordCollection} from '../../dist/app/records/record-collection'; import {mockProductionResult} from '../../dist/app/mocks/search/result/elastic-search-production.mock'; import {EsSearchRecordTransformService} from '../../dist/app/records/transformers/es-search-record-transformer.service'; const mockAuthConfig = new AuthConfig({ noJwtError: true }); const geoJsonAoi: GeoJSON.Feature = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[1, 1], [1, 2], [2, 2], [2, 1], [1, 1]]] }, properties: {} }; const YEAR_DELTA = 1; const validRequestBody = { app: 'atla', datasets: ['ok_production'], search: { 'filter': { 'bool': { 'must': [ { 'geo_shape': { 'geoJson': { 'shape': { 'type': 'polygon', 'coordinates': geoJsonAoi.geometry.coordinates } } } }, { 'range': { 'lastReportedMonth': { 'gte': `now-${YEAR_DELTA}y` } } } ] } }, size: 10000 } }; describe('Es Search Service', () => { let searchService: EsSearchService; beforeEachProviders(() => { return [ HTTP_PROVIDERS, AuthHttp, provide(ES_SEARCH_CONFIG, {useClass: EsSearchConfig}), provide(AuthConfig, {useValue: mockAuthConfig}), provide(XHRBackend, {useClass: MockBackend}), EsSearchService, EsSearchRecordTransformService ]; }); beforeEach(inject([XHRBackend, EsSearchService], (backend, service) => { searchService = service; backend.connections.subscribe((conn: MockConnection) => { expect(conn.request.text()).toEqual(JSON.stringify(validRequestBody)); conn.mockRespond(new Response(new ResponseOptions({ body: mockProductionResponse }))); }); })); it('should get a result from elastic search', done => { const docType = 'production'; searchService.get( docType, {aoi: geoJsonAoi, dateQuery: `now-${YEAR_DELTA}y`} ).subscribe({ next: (col: RecordCollection) => { expect(col.fields).toEqual(mockProductionResult.fields); expect(col.records).toEqual(mockProductionResult.records); expect(col.name).toEqual(docType); expect(col.geoTransformer).toBeDefined('geo transformer not defined'); done(); } }); }); });