Skip to content

Instantly share code, notes, and snippets.

@zimejin
Forked from manivelarjunan/googleData.ts
Created March 24, 2019 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimejin/37de76a6bd1be8187b4fec00525544ea to your computer and use it in GitHub Desktop.
Save zimejin/37de76a6bd1be8187b4fec00525544ea to your computer and use it in GitHub Desktop.
export interface GoogleData {
id: number;
country: string;
zipCode: string;
}
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { SharedService } from './sharedService.service';
describe('Shared service:', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SharedService],
imports: [HttpClientTestingModule]
});
// HttpClientTestingModule - Extended interactions between a data service and the HttpClient can be complex
// and difficult to mock with spies.
// The HttpClientTestingModule can make these testing scenarios more manageable.
});
describe(':', () => {
function setup() {
const sharedService = TestBed.get(SharedService);
const httpTestingController = TestBed.get(HttpTestingController);
return { sharedService, httpTestingController };
}
it('should call the google\'s map data', () => {
const { sharedService, httpTestingController } = setup();
const mockGoogleMapData = {id: 1, country : 'United states of america', zipcode: '56743'};
sharedService.getGoogleMapData().subscribe(data => {
expect(data.mapData).toEqual(mockGoogleMapData);
});
const req = httpTestingController.expectOne('https:www.google.com/googleMapData');
expect(req.request.method).toBe('GET');
req.flush({
mapData: mockGoogleMapData
});
});
afterEach(() => {
const { httpTestingController } = setup();
httpTestingController.verify();
});
});
});
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { GoogleData } from './googleData';
@Injectable({ providedIn: 'root' })
export class SharedService {
constructor(private httpClient: HttpClient) { }
public getGoogleMapData(): Observable<GoogleData> {
const options = {
headers: new HttpHeaders({
'content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
return this.httpClient.get<GoogleData>('https:www.google.com/googleMapData', options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment