Skip to content

Instantly share code, notes, and snippets.

@jplew
Created March 7, 2018 18:53
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 jplew/140c069c48cf48e8ad48e285d7b4cdd0 to your computer and use it in GitHub Desktop.
Save jplew/140c069c48cf48e8ad48e285d7b4cdd0 to your computer and use it in GitHub Desktop.
import { TestBed, inject, fakeAsync, tick } from '@angular/core/testing'
import { WordbankService } from './wordbank.service'
import { HttpClient, HttpClientModule } from '@angular/common/http'
import {
HttpTestingController,
HttpClientTestingModule,
} from '@angular/common/http/testing'
import { Observable } from 'rxjs/Observable'
import { of } from 'rxjs/observable/of'
let service
// let controllerService
let httpMock
describe('WordbankService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, HttpClientTestingModule],
providers: [WordbankService],
})
service = TestBed.get(WordbankService)
// controllerService = TestBed.get(ControllerService)
httpMock = TestBed.get(HttpTestingController)
})
it(
'should be created',
inject([WordbankService], (wordbankService: WordbankService) => {
expect(service).toBeTruthy()
})
)
fit(`should check server if fragment is valid`, (done: DoneFn) => {
const frag = 'delt'
const spyCheckIfValid = spyOn(
service,
'checkIfValidFragment'
).and.callThrough()
const spyCacheResult = spyOn(service, 'cacheResult').and.callThrough()
const fakeResponse: Observable<boolean> = of(true)
service.checkIfValidFragment(frag).subscribe(res => {
console.log('res is', res)
expect(res.value).toBe(true)
done()
})
const req = httpMock.expectOne({ method: 'GET' })
const regex: any = new RegExp('isvalid/' + frag + '$')
expect(req.request.url).toMatch(regex)
req.flush(fakeResponse)
expect(spyCacheResult.calls.count()).toBe(1, 'calls to cacheResult')
expect(spyCheckIfValid.calls.count()).toBe(
1,
'calls to checkIfValidFragment'
)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment