Files
openfoodnetwork/spec/services/address_geocoder_spec.rb
Cillian O'Ruanaidh f20cea7e4f Allow people to set enterprise latitude/longitude manually or automatically.
This for new changes to the enterprise registration/signup flow where a map will be displayed when people are filling in their address. On this map people can check the geocoder has geocoded their address correctly and if not they can manually adjust their latitude/longitude on the map.

But currently every time someone changes their address in the Admin > Enterprise > Address section the address would automatically be geocoded so this could overwrite the latitude/longitude that was set during sign up. To prevent the latitude/longitude from being overwritten this add's a checkbox which people need to explicity click if they want their address to be automatically geocoded, otherwise it will just use the manually configured latitude/longitude.

Note this new feature which allows people to select their location on a map during registration only works with Google maps so far. So if an instance is using Open Street Map this change also adds support for passing a :use_geocoder parameter to the Api::EnterprisesController during registration so that the address will be geocoded on the backend without the use of a map.
2021-03-19 21:43:29 +00:00

48 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe AddressGeocoder do
let(:australia) { Spree::Country.find_or_create_by!(name: "Australia") }
let(:victoria) { Spree::State.find_or_create_by(name: "Victoria", country: australia) }
let(:address) do
create(:address,
address1: "12 Galvin Street",
address2: "Unit 1",
city: "Altona",
country: australia,
state: victoria,
zipcode: 3018,
latitude: nil,
longitude: nil)
end
it "formats the address into a single comma separated string when passing it to the geocoder" do
expect(Geocoder).to receive(:coordinates).with("12 Galvin Street, Unit 1, 3018, Altona, Australia, Victoria")
AddressGeocoder.new(address).geocode
end
describe "when the geocoder can determine the latitude and longitude" do
it "updates the address's latitude and longitude" do
allow(Geocoder).to receive(:coordinates).and_return([-37.47, 144.78])
AddressGeocoder.new(address).geocode
expect(address.latitude).to eq(-37.47)
expect(address.longitude).to eq(144.78)
end
end
describe "when the geocoder cannot determine the latitude and longitude" do
it "doesn't update the address's latitude and longitude" do
allow(Geocoder).to receive(:coordinates).and_return([nil, nil])
AddressGeocoder.new(address).geocode
expect(address.latitude).to be_nil
expect(address.longitude).to be_nil
end
end
end