When a name match is found, calculate distances from that enterprise, rather than from the geocoded lookup

This commit is contained in:
Rohan Mitchell
2015-07-02 15:04:16 +10:00
parent 6e3ca3f90f
commit 7f2508eeaa
3 changed files with 46 additions and 21 deletions

View File

@@ -101,35 +101,52 @@ describe "Enterprises service", ->
Enterprises.flagMatching ''
expect(e.matches_name_query).toBe false for e in enterprises
describe "updating distance of enterprises from a location", ->
it "calculates the distance when a query is provided", ->
spyOn(Enterprises, "calculateDistance")
Enterprises.updateDistance "asdf"
expect(Enterprises.calculateDistance).toHaveBeenCalledWith("asdf")
describe "finding the first enterprise matched by a query", ->
it "returns the enterprise when one exists", ->
enterprises[0].matches_name_query = false
enterprises[1].matches_name_query = true
expect(Enterprises.firstMatching()).toEqual enterprises[1]
it "returns undefined otherwise", ->
e.matches_name_query = false for e in enterprises
expect(Enterprises.firstMatching()).toBeUndefined()
describe "calculating the distance of enterprises from a location", ->
describe "when a query is provided", ->
it "sets the distance from the enterprise when a name match is available", ->
spyOn(Enterprises, "firstMatching").andReturn('match')
spyOn(Enterprises, "setDistanceFrom")
Enterprises.calculateDistance "asdf"
expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith('match')
it "calculates the distance from the geocoded query otherwise", ->
spyOn(Enterprises, "calculateDistanceGeo")
Enterprises.calculateDistance "asdf"
expect(Enterprises.calculateDistanceGeo).toHaveBeenCalledWith("asdf")
it "resets the distance when query is null", ->
spyOn(Enterprises, "resetDistance")
Enterprises.updateDistance null
Enterprises.calculateDistance null
expect(Enterprises.resetDistance).toHaveBeenCalled()
it "resets the distance when query is blank", ->
spyOn(Enterprises, "resetDistance")
Enterprises.updateDistance ""
Enterprises.calculateDistance ""
expect(Enterprises.resetDistance).toHaveBeenCalled()
describe "calculating the distance of enterprises from a location", ->
describe "calculating the distance of enterprises from a location by geocoding", ->
beforeEach ->
spyOn(Enterprises, "setDistanceFrom")
it "calculates distance for all enterprises when geocoding succeeds", ->
Geo.succeed = true
Enterprises.calculateDistance('query')
Enterprises.calculateDistanceGeo('query')
expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith("location")
it "resets distance when geocoding fails", ->
Geo.succeed = false
spyOn(Enterprises, "resetDistance")
Enterprises.calculateDistance('query')
Enterprises.calculateDistanceGeo('query')
expect(Enterprises.setDistanceFrom).not.toHaveBeenCalled()
expect(Enterprises.resetDistance).toHaveBeenCalled()