Skip to content

Commit

Permalink
Fixup: Move debounce logic out of ViewfinderModel
Browse files Browse the repository at this point in the history
  • Loading branch information
ianguerin committed Mar 19, 2024
1 parent 784b531 commit 5aa928e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 22 deletions.
9 changes: 1 addition & 8 deletions src/js/models/maps/viewfinder/ViewfinderModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,13 @@ define(
initialize({ mapModel }) {
this.geocoderSearch = new GeocoderSearch();
this.mapModel = mapModel;

this.autocompleteSearch = _.debounce(
this.notDebouncedAutocompleteSearch,
250, // milliseconds
);
},

/**
* Get autocompletion predictions from the GeocoderSearch model.
* This function should be debounced to prevent sending many requests to
* the API while the user is still typing.
* @param {string} rawQuery is the user's search query with spaces.
*/
async notDebouncedAutocompleteSearch(rawQuery) {
async autocompleteSearch(rawQuery) {
const query = rawQuery.trim();
if (this.get('query') === query) {
return;
Expand Down
5 changes: 4 additions & 1 deletion src/js/templates/maps/viewfinder/viewfinder-prediction.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<div class="<%= classNames.content %> <% if(isFocused) { %>viewfinder-prediction__focused<% } %>">
<div class="<%= classNames.content %> <%
if(isFocused) {
%>viewfinder-prediction__focused<%
} %>">
<i class="icon-map-marker"></i>
<div class="viewfinder-prediction__main" title="<%= description %>">
<%= description %>
Expand Down
3 changes: 1 addition & 2 deletions src/js/views/maps/viewfinder/PredictionView.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ define(
* sibling list elements.
*/
select(event) {
event.stopPropagation();
this.viewfinderModel.selectPrediction(this.predictionModel);
},

Expand All @@ -104,7 +103,7 @@ define(
* */
render() {
const focusIndex = this.viewfinderModel.get('focusIndex');
this.templateVars.isFocused = focusIndex !== -1 && focusIndex === this.index;
this.templateVars.isFocused = focusIndex === this.index;

this.el.innerHTML = _.template(Template)(this.templateVars);
},
Expand Down
11 changes: 0 additions & 11 deletions test/js/specs/unit/models/maps/viewfinder/ViewfinderModel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ define(
describe('ViewfinderModel Test Suite', () => {
const state = cleanState(() => {
const sandbox = sinon.createSandbox();
// sinon.useFakeTimers() doesn't work with _.debounce, so stubbing instead.
const debounceStub = sandbox.stub(_, 'debounce').callsFake(function (fnToDebounce) {
return function (...args) {
fnToDebounce.apply(this, args);
};
});
const model = new ViewfinderModel({ mapModel: new Map() });
const zoomSpy = sinon.spy(model.mapModel, 'zoomTo');
const autocompleteSpy = sandbox.stub(model.geocoderSearch, 'autocomplete').returns([])
Expand All @@ -46,7 +40,6 @@ define(

return {
autocompleteSpy,
debounceStub,
geocodeSpy,
model,
predictions,
Expand Down Expand Up @@ -118,10 +111,6 @@ define(
expect(state.model.get('focusIndex')).to.equal(-1);
});

it('debounces autocomplete search using underscor\'s debounce', () => {
expect(state.debounceStub.callCount).to.equal(1);
});

it('sets predictions from autocomplete search', async () => {
const predictions = [new Prediction({
description: 'Some Other Location',
Expand Down

0 comments on commit 5aa928e

Please sign in to comment.