Last active
January 3, 2017 18:49
-
-
Save xlozinguez/da9e3ed837b0cf500a661d672dc163f9 to your computer and use it in GitHub Desktop.
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
import { TestBed, inject } from '@angular/core/testing'; | |
import { FakeBackend } from 'ngx-http-test'; | |
import { Observable } from 'rxjs/Observable'; | |
import { CompanyApiService } from './company.api.service'; | |
import { company_mock } from 'assets/mock-data/company.mock'; | |
import { company_offices_mock } from 'assets/mock-data/company-offices.mock'; | |
import { company_projects_mock } from 'assets/mock-data/company-projects.mock'; | |
import { company_transactions_mock } from 'assets/mock-data/company-transactions.mock'; | |
describe('CompanyApiService', () => { | |
let subject: CompanyApiService = null; | |
let backend: FakeBackend; | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
providers: [ | |
CompanyApiService, | |
FakeBackend.getProviders() | |
] | |
}); | |
}); | |
beforeEach(inject([CompanyApiService, FakeBackend], (companyApiService, fakeBackend) => { | |
subject = companyApiService; | |
backend = fakeBackend; | |
})); | |
afterEach(() => { | |
backend.verifyNoPendingRequests(); | |
backend.verifyNoPendingExpectations(); | |
}); | |
describe('#fetchCompanyDetails: ', () => { | |
it('should call endpoint and return a company when succesful', (done) => { | |
backend | |
.expectGet(`${process.env.API_URL}/accounts/foo`) | |
.respond(company_mock); | |
subject.fetchCompanyDetails('foo').subscribe((response) => { | |
expect(response).toEqual(company_mock); | |
done(); | |
}); | |
}); | |
}); | |
describe('#fetchCompanyDetailsBySlug: ', () => { | |
it('should call endpoint and return a company when succesful', (done) => { | |
backend | |
.expectGet(`${process.env.API_URL}/accounts/\?slug=foo`) | |
.respond([company_mock]); | |
subject.fetchCompanyDetailsBySlug('foo').subscribe((response) => { | |
expect(response).toEqual(company_mock); | |
done(); | |
}); | |
}); | |
fit('should call endpoint and trigger an exception when returning less or more than one result', (done) => { | |
backend | |
.expectGet(`${process.env.API_URL}/accounts/\?slug=foo`) | |
.respond([]); | |
// subject.fetchCompanyDetailsBySlug('foo').subscribe((response) => { | |
// expect(response).toEqual(Observable.throw('Slug returned an invalid number of results')); | |
// done(); | |
// }); | |
expect(subject.fetchCompanyDetailsBySlug('foo')).toThrow(); | |
}); | |
}); | |
}); |
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
import { Injectable } from '@angular/core'; | |
import { Http } from '@angular/http'; | |
import { Observable } from 'rxjs/Observable'; | |
import { Company } from '../models/company.model'; | |
@Injectable() | |
export class CompanyApiService { | |
private API_PATH: string = `${process.env.API_URL}/accounts`; | |
constructor(private http: Http) { } | |
fetchCompanyDetails(id: string): Observable<Company> { | |
return this.http.get(`${this.API_PATH}/${id}`) | |
.map(res => res.json()); | |
} | |
fetchCompanyDetailsBySlug(slug: string): Observable<Company> { | |
return this.http.get(`${this.API_PATH}/?slug=${slug}`) | |
.map(res => { | |
let result = res.json(); | |
// Since slug is unique, only succeed when 1 result is returned | |
if (result.length === 1) { | |
return result[0]; | |
} else { | |
// Result was 0 and so it is implied there was not a match | |
throw Observable.throw('No company matched your criteria'); | |
} | |
}); | |
} | |
} |
simpulton
commented
Jan 3, 2017
•
// Depending on how you feel about ternary operators
fetchCompanyDetailsBySlug(slug: string): Observable<Company> {
return this.http.get(`${this.API_PATH}/?slug=${slug}`)
.map(res => res.json())
.map(companies => {
// Since slug is unique, only succeed when 1 company is returned
return (companies.length === 1)
? companies[0] : Observable.throw('No company matched your criteria');
});
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment