From f2c77a0e62d383f2c173018a2d908c99a532079d Mon Sep 17 00:00:00 2001 From: Wouter van der Plas Date: Thu, 9 Sep 2021 19:21:30 +0200 Subject: [PATCH] Nominatim --- test/spec/services/nominatim.js | 151 ++++++++++++++++---------------- 1 file changed, 75 insertions(+), 76 deletions(-) diff --git a/test/spec/services/nominatim.js b/test/spec/services/nominatim.js index f28982cd0..efc5f80be 100644 --- a/test/spec/services/nominatim.js +++ b/test/spec/services/nominatim.js @@ -1,43 +1,41 @@ -describe('iD.serviceNominatim', function() { - var server, nominatim; +describe('iD.serviceNominatim', function () { + var nominatim; - - before(function() { + before(function () { iD.services.geocoder = iD.serviceNominatim; }); - after(function() { + after(function () { delete iD.services.geocoder; }); - beforeEach(function() { - server = window.fakeFetch().create(); + beforeEach(function () { nominatim = iD.services.geocoder; nominatim.reset(); }); - afterEach(function() { - server.restore(); + afterEach(function () { + fetchMock.reset(); }); - function query(url) { + function parseQueryString(url) { return iD.utilStringQs(url.substring(url.indexOf('?'))); } - - describe('#countryCode', function() { - it('calls the given callback with the results of the country code query', function(done) { + describe('#countryCode', function () { + it('calls the given callback with the results of the country code query', function (done) { var callback = sinon.spy(); + fetchMock.mock(new RegExp('https://nominatim.openstreetmap.org/reverse'), { + body: '{"address":{"country_code":"at"}}', + status: 200, + headers: { 'Content-Type': 'application/json' } + }); + nominatim.countryCode([16, 48], callback); - server.respondWith('GET', new RegExp('https://nominatim.openstreetmap.org/reverse'), - [200, { 'Content-Type': 'application/json' }, - '{"address":{"country_code":"at"}}']); - server.respond(); - - window.setTimeout(function() { - expect(query(server.requests()[0].url)).to.eql( - {zoom: '13', format: 'json', addressdetails: '1', lat: '48', lon: '16'} + window.setTimeout(function () { + expect(parseQueryString(fetchMock.calls()[0][0])).to.eql( + { zoom: '13', format: 'json', addressdetails: '1', lat: '48', lon: '16' } ); expect(callback).to.have.been.calledWithExactly(null, 'at'); done(); @@ -45,84 +43,85 @@ describe('iD.serviceNominatim', function() { }); }); - describe('#reverse', function() { - it('should not cache distant result', function(done) { + describe('#reverse', function () { + it('should not cache distant result', function (done) { var callback = sinon.spy(); + fetchMock.mock(new RegExp('https://nominatim.openstreetmap.org/reverse'), { + body: '{"address":{"country_code":"at"}}', + status: 200, + headers: { 'Content-Type': 'application/json' } + }); + nominatim.reverse([16, 48], callback); - server.respondWith('GET', new RegExp('https://nominatim.openstreetmap.org/reverse'), - [200, { 'Content-Type': 'application/json' }, '{"address":{"country_code":"at"}}']); - server.respond(); - - window.setTimeout(function() { - expect(query(server.requests()[0].url)).to.eql( - {zoom: '13', format: 'json', addressdetails: '1', lat: '48', lon: '16'} + window.setTimeout(function () { + expect(parseQueryString(fetchMock.calls()[0][0])).to.eql( + { zoom: '13', format: 'json', addressdetails: '1', lat: '48', lon: '16' } ); - expect(callback).to.have.been.calledWithExactly(null, {address: {country_code:'at'}}); + expect(callback).to.have.been.calledWithExactly(null, { address: { country_code: 'at' } }); - server.restore(); - server = window.fakeFetch().create(); + fetchMock.reset(); + fetchMock.mock(new RegExp('https://nominatim.openstreetmap.org/reverse'), { + body: '{"address":{"country_code":"cz"}}', + status: 200, + headers: { 'Content-Type': 'application/json' } + }); callback = sinon.spy(); nominatim.reverse([17, 49], callback); - server.respondWith('GET', new RegExp('https://nominatim.openstreetmap.org/reverse'), - [200, { 'Content-Type': 'application/json' }, '{"address":{"country_code":"cz"}}']); - server.respond(); - - window.setTimeout(function() { - expect(query(server.requests()[0].url)).to.eql( - {zoom: '13', format: 'json', addressdetails: '1', lat: '49', lon: '17'} + window.setTimeout(function () { + expect(parseQueryString(fetchMock.calls()[0][0])).to.eql( + { zoom: '13', format: 'json', addressdetails: '1', lat: '49', lon: '17' } ); - expect(callback).to.have.been.calledWithExactly(null, {address: {country_code:'cz'}}); + expect(callback).to.have.been.calledWithExactly(null, { address: { country_code: 'cz' } }); done(); }, 50); }, 50); }); - it('should cache nearby result', function(done) { + it('should cache nearby result', function (done) { var callback = sinon.spy(); + fetchMock.mock(new RegExp('https://nominatim.openstreetmap.org/reverse'), { + body: '{"address":{"country_code":"at"}}', + status: 200, + headers: { 'Content-Type': 'application/json' } + }); + nominatim.reverse([16, 48], callback); - server.respondWith('GET', new RegExp('https://nominatim.openstreetmap.org/reverse'), - [200, { 'Content-Type': 'application/json' }, '{"address":{"country_code":"at"}}']); - server.respond(); - - window.setTimeout(function() { - expect(query(server.requests()[0].url)).to.eql( - {zoom: '13', format: 'json', addressdetails: '1', lat: '48', lon: '16'} + window.setTimeout(function () { + expect(parseQueryString(fetchMock.calls()[0][0])).to.eql( + { zoom: '13', format: 'json', addressdetails: '1', lat: '48', lon: '16' } ); - expect(callback).to.have.been.calledWithExactly(null, {address: {country_code:'at'}}); + expect(callback).to.have.been.calledWithExactly(null, { address: { country_code: 'at' } }); - server.restore(); - server = window.fakeFetch().create(); + fetchMock.resetHistory(); callback = sinon.spy(); nominatim.reverse([16.000001, 48.000001], callback); - server.respondWith('GET', new RegExp('https://nominatim.openstreetmap.org/reverse'), - [200, { 'Content-Type': 'application/json' }, '{"address":{"country_code":"cz"}}']); - server.respond(); - - window.setTimeout(function() { - expect(callback).to.have.been.calledWithExactly(null, {address: {country_code:'at'}}); + window.setTimeout(function () { + expect(callback).to.have.been.calledWithExactly(null, { address: { country_code: 'at' } }); done(); }, 50); }, 50); }); - it('calls the given callback with an error', function(done) { + it('calls the given callback with an error', function (done) { var callback = sinon.spy(); + fetchMock.mock(new RegExp('https://nominatim.openstreetmap.org/reverse'), { + body: '{"error":"Unable to geocode"}', + status: 200, + headers: { 'Content-Type': 'application/json' } + }); + nominatim.reverse([1000, 1000], callback); - server.respondWith('GET', new RegExp('https://nominatim.openstreetmap.org/reverse'), - [200, { 'Content-Type': 'application/json' }, - '{"error":"Unable to geocode"}']); - server.respond(); - window.setTimeout(function() { - expect(query(server.requests()[0].url)).to.eql( - {zoom: '13', format: 'json', addressdetails: '1', lat: '1000', lon: '1000'} + window.setTimeout(function () { + expect(parseQueryString(fetchMock.calls()[0][0])).to.eql( + { zoom: '13', format: 'json', addressdetails: '1', lat: '1000', lon: '1000' } ); expect(callback).to.have.been.calledWithExactly('Unable to geocode'); done(); @@ -131,19 +130,19 @@ describe('iD.serviceNominatim', function() { }); - describe('#search', function() { - it('calls the given callback with the results of the search query', function(done) { + describe('#search', function () { + it('calls the given callback with the results of the search query', function (done) { var callback = sinon.spy(); + fetchMock.mock(new RegExp('https://nominatim.openstreetmap.org/search'), { + body: '[{"place_id":"158484588","osm_type":"relation","osm_id":"188022","boundingbox":["39.867005","40.1379593","-75.2802976","-74.9558313"],"lat":"39.9523993","lon":"-75.1635898","display_name":"Philadelphia, Philadelphia County, Pennsylvania, United States of America","class":"place","type":"city","importance":0.83238050437778}]', + status: 200, + headers: { 'Content-Type': 'application/json' } + }); + nominatim.search('philadelphia', callback); - server.respondWith('GET', new RegExp('https://nominatim.openstreetmap.org/search'), - [200, { 'Content-Type': 'application/json' }, - '[{"place_id":"158484588","osm_type":"relation","osm_id":"188022","boundingbox":["39.867005","40.1379593","-75.2802976","-74.9558313"],"lat":"39.9523993","lon":"-75.1635898","display_name":"Philadelphia, Philadelphia County, Pennsylvania, United States of America","class":"place","type":"city","importance":0.83238050437778}]' - ]); - server.respond(); - - window.setTimeout(function() { - expect(query(server.requests()[0].url)).to.eql({format: 'json', limit: '10'}); + window.setTimeout(function () { + expect(parseQueryString(fetchMock.calls()[0][0])).to.eql({ format: 'json', limit: '10' }); expect(callback).to.have.been.calledOnce; done(); }, 50);