Skip to content

Commit

Permalink
Fix validation for empty queryString
Browse files Browse the repository at this point in the history
  • Loading branch information
mpothier committed Sep 13, 2024
1 parent 5dc7125 commit 6d0bd42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,12 @@ MapboxEventManager.prototype = {
* @returns
*/
objectHasRequiredProps: function(obj, requiredProps) {
return requiredProps.every(prop => obj[prop] !== undefined);
return requiredProps.every(function(prop) {
if (prop === 'queryString') {
return typeof obj[prop] === 'string' && obj[prop].length > 0;
}
return obj[prop] !== undefined;
});
},

/**
Expand Down
21 changes: 21 additions & 0 deletions test/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,27 @@ test('should properly handle keypress events', function(assert){
assert.end();
});

test('does not send event when queryString empty', function(assert){
var testEvent = {key: 'Backspace', code :'Backspace', metaKey: false, keyCode: 8, shiftKey: false};
var eventsManager = new MapboxEventsManager({
accessToken: 'abc123'
})
var sendMethod = sinon.spy(eventsManager, "send");
var pushMethod = sinon.spy(eventsManager, "push");
var requestMethod = sinon.stub(eventsManager, "request").yields(null, {statusCode: 204});
var geocoder = new MapboxGeocoder({accessToken: 'abc123'});
geocoder.inputString = "";
eventsManager.keyevent(testEvent, geocoder);
assert.ok(requestMethod.notCalled, 'the http request was not initated');
assert.ok(sendMethod.notCalled, "the send method was not called");
assert.ok(pushMethod.notCalled, 'the event was NOT pushed to the event queue');
assert.equals(eventsManager.eventQueue.length, 0, 'the right number of events is in the queue');
sendMethod.restore();
pushMethod.restore();
requestMethod.restore();
assert.end();
});

test('it should properly flush events after the queue is full', function(assert){
var eventsManager = new MapboxEventsManager({
accessToken: 'abc123',
Expand Down

0 comments on commit 6d0bd42

Please sign in to comment.