Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geocoding error handling #345

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/components/geocoder/GeocoderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ export class GeocoderController {
ajax.callbackName = parameters.callbackName;
}

try {
const response = await json(ajax);
return this.provider.handleResponse(response)
} catch (err) {
return err
}
const response = await json(ajax);
return this.provider.handleResponse(response)

// // Do the query via Ajax XHR, returning JSON. Async callback via Promise.
// json(ajax)
Expand Down
82 changes: 79 additions & 3 deletions tests/unit/specs/components/geocoder/Geocoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,49 @@ describe('geocoder/Geocoder.vue', () => {
describe('methods - search', () => {
let comp;
let vm;
let onQueryResultsSpy;
let onQueryErrorSpy;
const fetchResults = JSON.stringify([
{
lon: '7.0928944',
lat: '50.7400352',
boundingbox: ['50.7399852', '50.7400852', '7.0928444', '7.0929444'],
display_name: 'John Barleycorn, 52, Heerstraße, Altstadt, Nordstadt, Stadtbezirk Bonn, Bonn, North Rhine-Westphalia, 53111, Germany',
address: {
amenity: 'John Barleycorn',
house_number: '52',
road: 'Heerstraße',
neighbourhood: 'Altstadt',
quarter: 'Nordstadt',
city_district: 'Stadtbezirk Bonn',
city: 'Bonn',
state: 'North Rhine-Westphalia',
'ISO3166-2-lvl4': 'DE-NW',
postcode: '53111',
country: 'Germany',
country_code: 'de'
}
},
{
lon: '7.092862308035045',
lat: '50.7400625',
boundingbox: ['50.7399456', '50.7402129', '7.0926186', '7.0931026'],
display_name: '52, Heerstraße, Altstadt, Nordstadt, Stadtbezirk Bonn, Bonn, North Rhine-Westphalia, 53111, Germany',
address: {
house_number: '52',
road: 'Heerstraße',
neighbourhood: 'Altstadt',
quarter: 'Nordstadt',
city_district: 'Stadtbezirk Bonn',
city: 'Bonn',
state: 'North Rhine-Westphalia',
'ISO3166-2-lvl4': 'DE-NW',
postcode: '53111',
country: 'Germany',
country_code: 'de'
}
}
])
// let fakeXhr;
// let clock;
// let requests = [];
Expand All @@ -133,6 +176,8 @@ describe('geocoder/Geocoder.vue', () => {
});
vm = comp.vm;

onQueryResultsSpy = sinon.replace(vm, 'onQueryResults', sinon.fake(vm.onQueryResults))
onQueryErrorSpy = sinon.replace(vm, 'onQueryError', sinon.fake(vm.onQueryError))
// TODO: Sinon Fake XMLHttpRequest and Fake Timers did not work for us...
// clock = sinon.useFakeTimers();
// global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
Expand All @@ -157,14 +202,20 @@ describe('geocoder/Geocoder.vue', () => {
});

it('search watcher assigns last query string', done => {
sinon.replace(window, 'fetch', sinon.fake.resolves(new Response(fetchResults)))

vm.search = queryString;
vm.$nextTick(() => {
expect(vm.lastQueryStr === queryString).to.equal(true);
done();
setTimeout(function () {
expect(vm.lastQueryStr === queryString).to.equal(true);
done();
}, 50)
});
});

it('search watcher query with results', done => {
sinon.replace(window, 'fetch', sinon.fake.resolves(new Response(fetchResults)))

vm.search = queryString;
vm.$nextTick(() => {
expect(vm.lastQueryStr === queryString).to.equal(true);
Expand All @@ -182,7 +233,7 @@ describe('geocoder/Geocoder.vue', () => {
expect(selectionItems === undefined).to.equal(false);
expect(selectionItems.length === vm.results.length).to.equal(true);
done();
}, 1800);
}, 50);
});
});

Expand All @@ -203,7 +254,32 @@ describe('geocoder/Geocoder.vue', () => {
});
});

it('calls onQueryResults if fetch successful', done => {
sinon.replace(window, 'fetch', sinon.fake.resolves(new Response(fetchResults)))

vm.search = queryString;
vm.$nextTick(() => {
setTimeout(function () {
expect(onQueryResultsSpy).to.have.been.called;
done();
}, 50);
});
})

it('calls onQueryError if error during fetch', done => {
sinon.replace(window, 'fetch', sinon.fake.rejects())

vm.search = queryString;
vm.$nextTick(() => {
setTimeout(function () {
expect(onQueryErrorSpy).to.have.been.called;
done();
}, 50);
});
})

afterEach(function () {
sinon.restore()
// Like before we must clean up when tampering with globals.
// global.XMLHttpRequest.restore();
// clock.restore();
Expand Down
Loading