Skip to content

Instantly share code, notes, and snippets.

@cjolly
Last active December 24, 2015 14:29
Show Gist options
  • Save cjolly/6812934 to your computer and use it in GitHub Desktop.
Save cjolly/6812934 to your computer and use it in GitHub Desktop.
How to test javascript geolocation with jasmine
#= require support/jasmine-given
class Geolocation
getCurrentPosition: ->
options =
maximumAge: 0
timeout: 1*10*1000
enableHighAccuracy: true
navigator.geolocation.getCurrentPosition(@success, @failure, options)
success: (position) =>
@coordinates = position.coords
failure: (error) =>
@coordinates = null
describe "Geolocation", ->
Given -> @subject = new Geolocation()
Given -> @spy = spyOn(navigator.geolocation, 'getCurrentPosition')
When -> @subject.getCurrentPosition()
describe "#getCurrentPosition", ->
describe "success", ->
Given -> @spy.andCallFake(-> arguments[0](coords: { latitude: 1, longitude: 2 }))
Then -> expect(@subject.coordinates).toEqual({latitude: 1, longitude: 2})
describe "failure", ->
Given -> @spy.andCallFake(-> arguments[1]({error: "FAILED"}))
Then -> @subject.coordinates is null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment