typeahead-addresspicker
Version:
Example of adding google map API to typeahead jquery plugin to display address autocomplete suggestions.
146 lines (107 loc) • 6.25 kB
text/coffeescript
describe 'TypyaheadAddressPicker', ->
beforeEach ->
loadFixtures 'fragment.html'
# Verify that twitter typeahead is defined
describe 'prerequisite', ->
it 'should have typeahead defined', ->
expect($.fn.typeahead).toBeDefined()
it 'should have google maps defined', ->
expect(google.maps.Map).toBeDefined()
it 'should have google maps places AutocompleteService defined', ->
expect(google.maps.places.AutocompleteService).toBeDefined()
describe 'AddressPicker without map options', ->
beforeEach ->
= getJSONFixture('paris-autocomplete-service.json')
mockGoogleMapAutocompleteService()
= new AddressPicker()
it 'should instance a new AddressPicker object', ->
expect( instanceof AddressPicker).toBe(true)
it 'should get autocomplete value', ->
callback = jasmine.createSpy()
.get("Paris", callback)
expect(callback).toHaveBeenCalledWith()
it 'should not have a google map instance', ->
expect(.getGMap()).not.toBeDefined()
it 'should not have a google marker instance', ->
expect(.getGMarker()).not.toBeDefined()
it 'should have default autocompleteService options', ->
expect(.options.autocompleteService).toEqual(types: ["geocode"])
it 'should set autocompleteService options', ->
addressPicker = new AddressPicker(autocompleteService: {types: ["cities"]})
expect(addressPicker.options.autocompleteService).toEqual(types: ["cities"])
describe 'AddressPicker with map options', ->
beforeEach (done) ->
= new AddressPicker(map: {id: '#map'})
$('#typeahead').typeahead null,
displayKey: 'description'
source: .ttAdapter()
google.maps.event.addListenerOnce(.getGMap(), 'idle', done)
it 'should instance a new AddressPicker object', ->
expect( instanceof AddressPicker).toBe(true)
it 'should return google map instance', ->
expect(.getGMap()).toBeDefined()
it 'should return google marker instance', ->
expect(.getGMarker()).toBeDefined()
it 'should have google marker hidden by default', ->
expect(.getGMarker().getVisible()).toBe(false)
it 'should create a google map ', (done) ->
expect($('#map')).toContainElement('.gm-style')
done()
it 'should bind default typeahead events', ->
spyOn(, 'updateMap')
.bindDefaultTypeaheadEvent($('#typeahead'))
$('#typeahead').trigger('typeahead:selected')
$('#typeahead').trigger('typeahead:cursorchanged')
expect(.updateMap.calls.count()).toEqual(2)
describe 'AddressPickerResult', ->
beforeEach ->
= getJSONFixture('paris-place-result.json')
.geometry.location = new google.maps.LatLng(.geometry.location_text.k, .geometry.location_text.A)
= new AddressPickerResult()
it 'should instance a new AddressPickerResult object', ->
expect( instanceof AddressPickerResult).toBe(true)
it 'should get list of addressTypes', ->
expect(.addressTypes()).toEqual(['locality', 'political', 'administrative_area_level_2', 'administrative_area_level_1', 'country'])
it 'should get addressComponents', ->
expect(.addressComponents().length).toEqual(4)
it 'should get latitude value', ->
expect(.lat()).toEqual(48.856614)
it 'should get longitude value', ->
expect(.lng()).toEqual(2.3522219000000177)
it 'should not be from reverse geocoding by default', ->
expect(.isReverseGeocoding()).toBe(false)
it 'should be from reverse geocoding is set on constructor', ->
= new AddressPickerResult(, true)
expect(.isReverseGeocoding()).toBe(true)
it 'should get name for type if available', ->
expect(.nameForType("country")).toEqual('France')
expect(.nameForType("administrative_area_level_1")).toEqual('Île-de-France')
expect(.nameForType("administrative_area_level_2")).toEqual('Paris')
expect(.nameForType("locality")).toEqual('Paris')
it 'should get null value for type if not available', ->
expect(.nameForType("sublocality")).toBe(null)
it 'should get short name for type if available', ->
expect(.nameForType("country", true)).toEqual('FR')
expect(.nameForType("administrative_area_level_1", true)).toEqual('IDF')
expect(.nameForType("administrative_area_level_2", true)).toEqual('75')
expect(.nameForType("locality", true)).toEqual('Paris')
it 'should not be accurate if geometry has a viewport', ->
expect(.isAccurate()).toBe(false)
it 'should be accurate if geometry has no viewport', ->
fixture = getJSONFixture('accurate-place-result.json')
fixture.geometry.location = new google.maps.LatLng(fixture.geometry.location.k, fixture.geometry.location.A)
addressPickerResult = new AddressPickerResult(fixture)
expect(addressPickerResult.isAccurate()).toBe(true)
it 'should change lat/lng', ->
.setLatLng(12, 13)
expect(.lat()).toEqual(12)
expect(.lng()).toEqual(13)
describe 'AddressPickerResult with only lat/lng', ->
beforeEach ->
= new AddressPickerResult(geometry: {location: new google.maps.LatLng(12,13)})
it 'should get latitude value', ->
expect(.lat()).toEqual(12)
it 'should get longitude value', ->
expect(.lng()).toEqual(13)
it 'should not have addressComponents', ->
expect(.addressComponents().length).toEqual(0)