Skip to content

Instantly share code, notes, and snippets.

@jplew
Last active March 8, 2018 02:11
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/8cb79202126c4ebaaa9697d2273b4c40 to your computer and use it in GitHub Desktop.
Save jplew/8cb79202126c4ebaaa9697d2273b4c40 to your computer and use it in GitHub Desktop.
import { TestBed, inject } from '@angular/core/testing'
import { WordService } from './word.service'
import { PlayerService } from '@app/services/player.service'
import { ControllerService } from '@app/services/controller.service'
import { WordbankService } from '@app/services/wordbank.service'
import { HttpClientModule } from '@angular/common/http'
import {
HttpTestingController,
HttpClientTestingModule,
} from '@angular/common/http/testing'
import { SampleDefinition } from '@interfaces/SampleOxfordResponses'
import { Observable } from 'rxjs/Observable'
import { OptionsService } from '@app/services/options.service'
import { CookieService } from 'ngx-cookie-service'
import { WordProblem } from '@interfaces/WordProblem'
import { of } from 'rxjs/observable/of'
let service
let controllerService
let httpMock
describe('WordService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, HttpClientTestingModule],
providers: [
WordService,
PlayerService,
ControllerService,
WordbankService,
OptionsService,
CookieService,
],
})
service = TestBed.get(WordService)
controllerService = TestBed.get(ControllerService)
httpMock = TestBed.get(HttpTestingController)
})
afterEach(() => {
httpMock.verify()
})
it(
'should be created',
inject([WordService], (wordService: WordService) => {
expect(wordService).toBeTruthy()
})
)
it('should set attempted word for long fragment', () => {
service.theWord = 'arpy'
const attemptArray = ['harpy', 'parthenocarpy', 'sharpy']
const getAttemptedWordSpy = spyOn(
service,
'getAttemptedWord'
).and.callThrough()
controllerService.attemptedWord$.subscribe(word => {
console.log('word is', word)
expect(attemptArray).toContain(word)
})
service.setAttemptedWord('arpy')
const req = httpMock.expectOne({ method: 'GET' })
expect(req.request.url).toMatch(
/api\/wordbank\/fragment\/arpy\/level\/\d+$/
)
expect(getAttemptedWordSpy.calls.count()).toBe(
1,
'calls to getAttemptedWord'
)
req.flush({ words: attemptArray })
})
it('should set attempted word for short fragment', () => {
service.theWord = 'wamj'
const attemptArray = ['circumjacent', 'jamjars', 'jamjar', 'jimjams']
controllerService.attemptedWord$.subscribe(word => {
expect(attemptArray).toContain(word)
})
service.setAttemptedWord('amj')
const req = httpMock.expectOne({ method: 'GET' })
expect(req.request.url).toMatch(/api\/wordbank\/fragment\/amj\/level\/\d+$/)
req.flush({ words: attemptArray })
})
it('should produce a one letter fragment, given no letters', () => {
service.theWord = ''
service.getAiWord()
expect(service.theWord).toMatch(/\w/, 'single random letter')
})
it('should produce a two-letter fragment, given a single letter', () => {
service.theWord = 'e'
const newFragment = 're'
const spyGetAiWord = spyOn(service, 'getAiWord').and.callThrough()
const spyTwoLetter = spyOn(service, 'getTwoLetterFragment').and.returnValue(
Observable.of(newFragment)
)
service.getAiWord()
const req = httpMock.expectOne({ method: 'GET' })
expect(spyTwoLetter.calls.count()).toBe(1, 'getTwoLetterFragment')
expect(service.theWord).toBe(newFragment)
})
it('should produce a three-letter fragment, given two letters', () => {
service.theWord = 're'
const wordArray = ['area', 'retold', 'tree', 'presbyterian']
const randomIndex = Math.floor(wordArray.length * Math.random())
const spyGetAiWord = spyOn(service, 'getAiWord').and.callThrough()
const spyThreeLetters = spyOn(
service,
'_sendThreeOrMoreLetters'
).and.callThrough()
const spyGetPossibleWords = spyOn(
service,
'getPossibleWords'
).and.returnValue(Observable.of(wordArray))
const spyExtractFragment = spyOn(
service,
'extractFragment'
).and.returnValue(Observable.of(wordArray[randomIndex]))
service.getAiWord()
const req = httpMock.expectOne({ method: 'GET' })
const regex: any = new RegExp('fragment/\\w+/level/\\d+$')
expect(req.request.url).toMatch(regex)
req.flush({ words: ['hello'] })
expect(spyGetAiWord.calls.count()).toBe(1, 'get ai word')
expect(spyThreeLetters.calls.count()).toBe(1, 'get three letters')
expect(spyGetPossibleWords.calls.count()).toBe(1, 'get possible words')
expect(spyExtractFragment.calls.count()).toBe(1, 'extract fragment')
expect(wordArray).toContain(service.theWord)
})
it('should produce a long fragment, given lots of letters', () => {
service.theWord = 'owlin'
const wordArray = ['bowling', 'growling', 'howling', 'prowling']
const randomIndex = Math.floor(wordArray.length * Math.random())
const spyGetAiWord = spyOn(service, 'getAiWord').and.callThrough()
const spyThreeLetters = spyOn(
service,
'_sendThreeOrMoreLetters'
).and.callThrough()
const spyGetPossibleWords = spyOn(
service,
'getPossibleWords'
).and.returnValue(Observable.of(wordArray))
const spyExtractFragment = spyOn(
service,
'extractFragment'
).and.returnValue(Observable.of(wordArray[randomIndex]))
service.getAiWord()
const req = httpMock.expectOne({ method: 'GET' })
const regex: any = new RegExp('fragment/\\w+/level/\\d+$')
expect(req.request.url).toMatch(regex)
req.flush({ words: ['hello'] })
expect(spyGetAiWord.calls.count()).toBe(1, 'get ai word')
expect(spyThreeLetters.calls.count()).toBe(1, 'get three letters')
expect(spyGetPossibleWords.calls.count()).toBe(1, 'get possible words')
expect(spyExtractFragment.calls.count()).toBe(1, 'extract fragment')
expect(wordArray).toContain(service.theWord)
})
it(`should handle error if this level of AI can't find any matching words`, () => {
const fragment = 'mononucle'
const finalWord = 'mononucleosis'
service.theWord = fragment
const regex = new RegExp('(w?)' + fragment + '(w?)')
const error = new WordProblem(
`AI says: That doesn't sound like any word I've ever heard of.`,
''
)
const spyGetAiWord = spyOn(service, 'getAiWord').and.callThrough()
const spyThreeLetters = spyOn(
service,
'_sendThreeOrMoreLetters'
).and.callThrough()
const spyGetPossibleWords = spyOn(
service,
'getPossibleWords'
).and.returnValue(Observable.throw(error))
const getAttemptedWordSpy = spyOn(
service,
'getAttemptedWord'
).and.returnValue(of(finalWord))
service.getAiWord()
// const req = httpMock.expectOne({ method: 'GET' })
// const regex2: any = new RegExp('fragment/\\w+/level/\\d+$')
// expect(req.request.url).toMatch(regex2)
// req.flush({ words: ['hello'] })
expect(spyGetAiWord.calls.count()).toBe(1, 'get ai word')
expect(spyThreeLetters.calls.count()).toBe(1, 'get three letters')
expect(spyGetPossibleWords.calls.count()).toBe(1, 'get possible words')
expect(getAttemptedWordSpy.calls.count()).toBe(1, 'get attempted word')
expect(service.theWord).toMatch(regex)
expect(service.attemptedWord).toMatch(regex)
})
it(`should handle error if possible words found but they will lose the game`, () => {
service.theWord = 'quagmir'
const nextWord = 'quagmire'
const error = new WordProblem('None of my words work!', nextWord)
const spyGetAiWord = spyOn(service, 'getAiWord').and.callThrough()
const spyThreeLetters = spyOn(
service,
'_sendThreeOrMoreLetters'
).and.callThrough()
const spyGetPossibleWords = spyOn(
service,
'getPossibleWords'
).and.returnValue(Observable.of([nextWord]))
const spyExtractFragment = spyOn(
service,
'extractFragment'
).and.returnValue(Observable.throw(error))
service.getAiWord()
const req = httpMock.expectOne({ method: 'GET' })
const regex: any = new RegExp('fragment/\\w+/level/\\d+$')
expect(req.request.url).toMatch(regex)
req.flush({ words: ['hello'] })
expect(spyGetAiWord.calls.count()).toBe(1, 'get ai word')
expect(spyThreeLetters.calls.count()).toBe(1, 'get three letters')
expect(spyGetPossibleWords.calls.count()).toBe(1, 'get possible words')
expect(spyExtractFragment.calls.count()).toBe(1, 'extract fragment')
expect(service.theWord).toMatch(nextWord)
})
it(`should do AI word search after human plays letter`, () => {
service.theWord = 'e'
const newFragment = 'ef'
const _sendTwoLettersSpy = spyOn(
service,
'_sendTwoLetters'
).and.callThrough()
const getTwoLetterFragmentSpy = spyOn(
service,
'getTwoLetterFragment'
).and.returnValue(of(newFragment))
service.getAiWord()
const req = httpMock.expectOne({ method: 'GET' })
const regex: any = new RegExp('fragment/\\w+/level/\\d+$')
expect(req.request.url).toMatch(regex)
req.flush({ words: ['hello'] })
expect(_sendTwoLettersSpy.calls.count()).toBe(1, 'calls to _sendTwoLetters')
expect(getTwoLetterFragmentSpy.calls.count()).toBe(
1,
'calls to getTwoLetterFragment'
)
expect(service.theWord.length).toBe(2, 'word length')
expect(service.theWord).toBe(newFragment, 'equal to new fragment')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment