Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
* feat(viewer): resolve geoname id and show name * feat(viewer): remove label input from geoname val comp * test(viewer): add specs to geoname service * test(app): mock component
- Loading branch information
1 parent
0b03781
commit 18ea19b
Showing
8 changed files
with
190 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { GeonameService } from './geoname.service'; | ||
import { AppInitService } from '../../core'; | ||
|
||
describe('GeonameService', () => { | ||
let service: GeonameService; | ||
let httpTestingController: HttpTestingController; | ||
|
||
const appInitSpy = { | ||
config: { | ||
geonameToken: 'token' | ||
} | ||
}; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientTestingModule | ||
], | ||
providers: [ | ||
{ | ||
provide: AppInitService, | ||
useValue: appInitSpy | ||
} | ||
] | ||
}); | ||
|
||
service = TestBed.inject(GeonameService); | ||
httpTestingController = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should resolve a given geoname id', done => { | ||
|
||
service.resolveGeonameID('2661604').subscribe( | ||
name => { | ||
expect(name).toEqual('Basel'); | ||
done(); | ||
} | ||
); | ||
|
||
const httpRequest = httpTestingController.expectOne('https://ws.geonames.net/getJSON?geonameId=2661604&username=token&style=short'); | ||
|
||
expect(httpRequest.request.method).toEqual('GET'); | ||
|
||
const expectedResponse = { name: 'Basel' }; | ||
|
||
httpRequest.flush(expectedResponse); | ||
|
||
}); | ||
|
||
it('should use the given geoname id as a fallback value if the requests fails', done => { | ||
|
||
service.resolveGeonameID('2661604').subscribe( | ||
name => { | ||
expect(name).toEqual('2661604'); | ||
done(); | ||
} | ||
); | ||
|
||
const httpRequest = httpTestingController.expectOne('https://ws.geonames.net/getJSON?geonameId=2661604&username=token&style=short'); | ||
|
||
expect(httpRequest.request.method).toEqual('GET'); | ||
|
||
const mockErrorResponse = {status: 400, statusText: 'Bad Request'}; | ||
|
||
httpRequest.flush(mockErrorResponse); | ||
|
||
}); | ||
|
||
it('should use the given geoname id as a fallback value if the requests response does not contain the expected information', done => { | ||
|
||
service.resolveGeonameID('2661604').subscribe( | ||
name => { | ||
expect(name).toEqual('2661604'); | ||
done(); | ||
} | ||
); | ||
|
||
const httpRequest = httpTestingController.expectOne('https://ws.geonames.net/getJSON?geonameId=2661604&username=token&style=short'); | ||
|
||
expect(httpRequest.request.method).toEqual('GET'); | ||
|
||
const expectedResponse = { place: 'Basel' }; | ||
|
||
httpRequest.flush(expectedResponse); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, of } from 'rxjs'; | ||
import { catchError, map } from 'rxjs/operators'; | ||
import { AppInitService } from '../../core'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class GeonameService { | ||
|
||
constructor( | ||
private readonly _http: HttpClient, | ||
private _appInitService: AppInitService | ||
) { | ||
} | ||
|
||
resolveGeonameID(id: string): Observable<string> { | ||
|
||
return this._http.get<object>('https://ws.geonames.net/getJSON?geonameId=' + id + '&username=' + this._appInitService.config['geonameToken'] + '&style=short').pipe( | ||
map( | ||
(geo: { name: string }) => { | ||
|
||
if (!('name' in geo)) { | ||
throw 'no name property'; | ||
} | ||
|
||
return geo.name; | ||
} | ||
), | ||
catchError(error => { | ||
// an error occurred, just return the id | ||
return of(id); | ||
}) | ||
); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters