Skip to content

Instantly share code, notes, and snippets.

@manivelarjunan
Last active March 24, 2019 17:58
Show Gist options
  • Select an option

  • Save manivelarjunan/7a67023415bf72b5c9ddd76a5cdb89d2 to your computer and use it in GitHub Desktop.

Select an option

Save manivelarjunan/7a67023415bf72b5c9ddd76a5cdb89d2 to your computer and use it in GitHub Desktop.

Revisions

  1. manivelarjunan revised this gist Dec 29, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions sharedService.service.spec.ts
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@ describe('Shared service:', () => {
    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(':', () => {

  2. manivelarjunan created this gist Dec 29, 2018.
    5 changes: 5 additions & 0 deletions googleData.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    export interface GoogleData {
    id: number;
    country: string;
    zipCode: string;
    }
    43 changes: 43 additions & 0 deletions sharedService.service.spec.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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]
    });
    });
    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();
    });
    });
    });
    22 changes: 22 additions & 0 deletions sharedService.service.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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);
    }

    }