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

Scale update units #6138

Closed
wants to merge 9 commits into from
Closed

Scale update units #6138

wants to merge 9 commits into from

Conversation

ryanhamley
Copy link
Contributor

Potential fix for #6137

Launch Checklist

  • briefly describe the changes in this PR
    Adds a method to the ScaleControl to update the scale's unit of distance on the fly
  • write tests for all new functionality
  • document any changes to public APIs
  • manually test the debug page

screen capture on 2018-02-13 at 00-32-33

Copy link
Contributor

@anandthakker anandthakker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @ryanhamley !

This looks great to me, with just a couple of small changes needed in the unit test.


function createMap() {
const container = window.document.createElement('div');
config.ACCESS_TOKEN = 'pk.123';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dummy access token shouldn't be necessary, and nor should the owner or id properties below. (I think it exists in the AttributionControl tests because that specific control needs it.)


t.equal(map.getContainer().querySelectorAll('.mapboxgl-ctrl-top-left .mapboxgl-ctrl-scale').length, 1);
t.end();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇 thank you for adding these tests! Could you also add one that tests the new updateUnit() method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @anandthakker I will work on this in the next day or so. Thanks for the feedback.

@andrewharvey
Copy link
Collaborator

👍 to implementing this feature.

an open question, should the naming of this method be setUnit to be consistent with the GL JS API which already extensivly uses get and set?

@anandthakker
Copy link
Contributor

an open question, should the naming of this method be setUnit to be consistent with the GL JS API which already extensivly uses get and set?

👍 yep, agreed @andrewharvey -- I meant to mention that in the review, in fact!

@ryanhamley
Copy link
Contributor Author

I'll update the name of the method to setUnit @anandthakker @andrewharvey

@ryanhamley
Copy link
Contributor Author

@anandthakker @andrewharvey I pushed the requested changes.

/**
* Set the scale's unit of the distance
*
* @param {String} unit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String is causing the CI to fail, I believe it should be string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it would be helpful to include the documentation for unit, listing out what the supported values are.

@ryanhamley
Copy link
Contributor Author

I fixed the param documentation and it looks like the build ci is passing now @andrewharvey

@ryanhamley
Copy link
Contributor Author

@anandthakker i added the requested test and everything is building now

Copy link
Contributor

@anandthakker anandthakker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ryanhamley ! One more (hopefully small) testing change 🙏


t.equal(scale.options.unit, 'metric');
scale.setUnit('imperial');
t.equal(scale.options.unit, 'imperial');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the unit is stored in scale.options.unit is an implementation detail that could be changed in a future refactor. Let's update this test so that it wouldn't be affected by such a change by having these assertions actually check the content of the rendered scale control, e.g. maybe by searching for an appropriate string within map.getContainer().querySelectorAll('.mapboxgl-ctrl-top-left .mapboxgl-ctrl-scale').innerHTML

@ryanhamley
Copy link
Contributor Author

I pushed a change to the scale test that reads the unit from the scale's HTML @anandthakker Does that look good? I'm pretty sure it's not best practice to abstract getting the unit from the map into a function but I wasn't sure.

t.equal(scale.options.unit, 'metric');
let unit = map.getContainer().querySelectorAll('.mapboxgl-ctrl-bottom-left .mapboxgl-ctrl-scale')[0].innerHTML.match(/[a-zA-Z]+|[0-9]+/g)[1];

t.equal(unit, 'km');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanhamley small nitpick: let's simplify this just a tad to:

let contents = map.getContainer().querySelector('.mapboxgl-ctrl-bottom-left .mapboxgl-ctrl-scale').innerText; // (not 100% sure jsdom supports innerText, but worth a shot
t.match(contents, /\bkm\b/);

@ryanhamley
Copy link
Contributor Author

@anandthakker I pushed a simpler version of the test. it doesn't appear that jsdom has innerText so I used innerHTML and the word boundary character in the regex doesn't distinguish between letters and numbers so I had to remove that from the regex to match the correct characters.

Copy link
Contributor

@anandthakker anandthakker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again, @ryanhamley !!

@anandthakker
Copy link
Contributor

--> #6274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants